[llvm-dev] [question] с-double: 35.7 * 100 vs 3570.0
Matthias Braun via llvm-dev
llvm-dev at lists.llvm.org
Tue Mar 21 10:18:49 PDT 2017
Floating point arithmetic is not intuitive. http://www.lsi.upc.edu/~robert/teaching/master/material/p5-goldberg.pdf is a good introduction to the common pitfalls.
TL;DR 35.7 cannot be represented exactly as a floating point number.
- Matthias
> On Mar 21, 2017, at 10:10 AM, Daniil Troshkov via llvm-dev <llvm-dev at lists.llvm.org> wrote:
>
> Hi all!
>
> There is a simple c code:
>
> double a = 3570.0;
> double b = 35.7 * 100;
> int main ()
> {
> if (b != a) return 1;
> return 0;
> }
>
> It returns 1 due to:
> 40abe400 00000000 //a = 3570.0
> 40abe400 00000001 //b = 35.7 * 100;
>
> gcc do the same thing, so I think it's ok but why?
>
> For floats:
>
> float a = 3570.0;
> float b = 35.7 * 100;
> int main ()
> {
> if (b != a) return 1;
> return 0;
> }
>
> It returns 0...
> 455f2000 //a = 3570.0
> 455f2000 //b = 35.7 * 100;
>
> I will be grateful to get any explanations.
>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
More information about the llvm-dev
mailing list