0

Please Help Why its output is FALSE

Please read the code attached to this question and please explain me why FALSE is the output why not TRUE??? https://code.sololearn.com/c16vpI1NxnBi/?ref=app

23rd Feb 2019, 10:40 AM
Dastageer
Dastageer - avatar
8 Respostas
+ 5
Because floats are less precise than doubles. For humans, 0.1 is a round number. For a computer, it isn't. The value 0.1 is impossible to store in a float. It will contain something like 0.0997865 instead, which is not equal to the double representation of 0.1 (which is more precise). If you compare your float variable to 0.1f (a floating point literal), you compare a float and a float. Both are equally imprecise, but they do have the same value (0.0997865 or whatever). That's why they're equal.
23rd Feb 2019, 12:36 PM
Anna
Anna - avatar
+ 6
Yes. Different data types can't be compared. 0.1 (without -f) is a double, so if you compare 0.1f (float) and 0.1 (double), 0.1f will be "upgraded" to (double)0.1f which is something like 0.0997865, so you compare (double)0.0997865 with (double)0.1 => not equal.
23rd Feb 2019, 12:48 PM
Anna
Anna - avatar
+ 3
Oh, I was wrong it seems? ^^' Kodak, thanks for pointing that out!
23rd Feb 2019, 11:36 AM
HonFu
HonFu - avatar
+ 3
Ah, so I was half right after all. 😏🤔😞
23rd Feb 2019, 12:37 PM
HonFu
HonFu - avatar
+ 2
Please take a look here: It's the same issue. https://www.sololearn.com/Discuss/1477626/?ref=app
23rd Feb 2019, 10:44 AM
HonFu
HonFu - avatar
+ 2
Its because that 0.1 is considered a double instead of a float.. Just add 'f' to 0.1 then it will work..eg 0.1f
23rd Feb 2019, 11:20 AM
Mensch
Mensch - avatar
+ 1
Kodak You are right it worked But please explain me why adding f does the trick
23rd Feb 2019, 12:28 PM
Dastageer
Dastageer - avatar
0
@Anant Nigam I APPRECIATE YOUR ANSWER BUT I NEED ANSWER!!!
23rd Feb 2019, 10:46 AM
Dastageer
Dastageer - avatar