Home > database >  Incorrect result of arithmetic operations using LLVM-based compilers [closed]
Incorrect result of arithmetic operations using LLVM-based compilers [closed]

Time:10-03

Code:

float fff = 255.0f;
float a0 = (fff / 255.0f) * 100.0f;

If I use LLVM-based compilers (for example Intel C compiler or clang) variable a0 equals 100.000008, but if I use g compiler I get the right result - 100. Why do LLVM-based compilers return the wrong results? How to fix that? I can't just switch to g 'cos this one has other errors which LLVM compilers have not.

CodePudding user response:

I don't know much about LLVM compilers, it is odd that you get 100.000008 though, since you have only whole numbers in there, even in the intermediate results. However even with G , you should still not rely to get an accurate result on floating point types calculations due to the (binary) rounding errors in the calculation, which is not the case for regular types. For example, try using 0.1 in g , you will notice that the result is not 0.1 (if you look thru the debugger or compare 0.1f with 0.1, cout or printf will not show enough decimals) Rounding error

  • Related