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
8 Respuestas
+ 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.
+ 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.
+ 3
Oh, I was wrong it seems? ^^'
Kodak, thanks for pointing that out!
+ 3
Ah, so I was half right after all.
😏🤔😞
+ 2
Please take a look here: It's the same issue.
https://www.sololearn.com/Discuss/1477626/?ref=app
+ 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
+ 1
Kodak
You are right it worked
But please explain me why adding f does the trick
0
@Anant Nigam
I APPRECIATE YOUR ANSWER BUT I NEED ANSWER!!!