+ 1

Why do I get wrong result?

I tried 10.05 +0.05 and it evaluated to 10.100000000000001! Why can't I get 10.1 instead? Can anyone tell me the reason please? https://code.sololearn.com/cpwRdAu5ZLK9/?ref=app https://code.sololearn.com/cpwRdAu5ZLK9/?ref=app

24th Aug 2017, 9:02 AM
Joel Bennett
Joel Bennett - avatar
1 Answer
+ 10
Because there's no exact representation of decimal numbers like 0.1, 0.2, 0.3 etc as binary number inside the computer. you can even compare two float and double precision number with the same value and get an unexpected result just because the precision of approximated binary number float f = 10.1f; double d = 10.1; if ( f == d) // yes else // no And guess what, you get no. 10.9999999999 or 10.1000000001 Vs. 10.9999999999999999998 or 10.1000000000000000001
24th Aug 2017, 10:04 AM
Babak
Babak - avatar