Keil™, An ARM® Company

Cx51 User's Guide

Floating-point Errors

The 8051 does not contain an interrupt vector to trap floating-point errors; therefore, your software must respond appropriately to these error conditions.

In addition to the normal floating-point values, a floating-point number may contain a binary error value. These values are defined as a part of the IEEE standard and are used whenever an error occurs during normal processing of floating-point operations. Your code should check for possible arithmetic errors at the end of each floating-point operation.

NameValueMeaning
NaN0xFFFFFFFNot a number
+INF0x7F80000Positive infinity (positive overflow)
-INF0xFF80000Negative infinity (negative overflow)

The C51 library function _chkfloat_ allows you to quickly check floating-point status.

As an alternative, you may use the following union to store floating-point values.

union f  {
  float          f;          /* Floating-point value */
  unsigned long ul;          /* Unsigned long value */
};

This union contains a float and an unsigned long in order to perform floating-point math operations and to respond to the IEEE error states.

For example:

#define NaN       0xFFFFFFFF    /* Not a number (error) */
#define plusINF   0x7F800000    /* Positive overflow    */
#define minusINF  0xFF800000    /* Negative overflow    */

union f {
  float           f;            /* Floating-point value */
  unsigned long   ul;           /* Unsigned long value */
};

void main (void)  {
  float a, b;
  union f x;

  x.f = a * b;
  if (x.ul == NaN  || x.ul == plusINF  || x.ul == minusINF)  {
    /* handle the error  */
  }
  else  {
    /* result is correct */
  }
}