Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect floating point behavior

Tags:

People also ask

Why are floating point calculations so inaccurate?

Because often-times, they are approximating rationals that cannot be represented finitely in base 2 (the digits repeat), and in general they are approximating real (possibly irrational) numbers which may not be representable in finitely many digits in any base.

Why do computers mess up floating point math?

The main cause of the error in floating point division is the division algorithms used to calculate the quotient. Most computer systems calculate division using multiplication by an inverse, mainly in Z=X/Y , Z = X * (1/Y) .

What is a floating point rounding error?

Because floating-point numbers have a limited number of digits, they cannot represent all real numbers accurately: when there are more digits than the format allows, the leftover ones are omitted - the number is rounded.


When I run the below C++ program in a 32-bit powerpc kernel which supports software floating emulation (hardware floating point disabled), I get a incorrect conditional evaluation. Can some tell me what's the potential problem here?

#include <stdio.h>

int main() {
   int newmax = 1;
   if ((newmax + 0.0) > 256) {
       printf("\nShouldn't be here\n");
   } else {
       printf("\nShould be here\n");
   }
}

Compile:

powerpc-linux-g++ -msoft-float -c floating.cxx
powerpc-linux-g++  -o floating floating.o

Output in target system:

[linux:/]$ ./floating
Shouldn't be here