+ 3
Explain the output please?
12 odpowiedzi
+ 10
The problem is that decimals like 0.1 can't be precisely stored. As you probably know, an integer like 5 is 0b101 in binary, which is (from the right to the left) 1*2^0 + 0*2^1 + 1*2^2. Decimals can be precisely stored if they can be expressed as x*(1/2)^1 + y*(1/2)^2 + z*(1/2)^4 etc. which is the case for decimals like 0.5 or 0.625, but not for 0.1. This is why if you change 0.1 to 0.5 in your code, it will show that both numbers are equal.
+ 8
A similar case a few hours back:
https://www.sololearn.com/Discuss/1676083/?ref=app
+ 7
Ankit ah yes, I forgot that double has nore precision.
+ 4
Use double it'll work fine
+ 4
U mean 9.0 is float and 0.1 is double...... something like that
+ 4
Because all the trouble happens with decimal places, 9.0 has a decimal place but just a 0, so it is sort-of a whole number float.
+ 3
Ok but why first if statement is true then if(a==9.0)
+ 2
Strange. It seems to me that b is apperently -1610612736, instead of 0.1.
+ 2
Because it’s a whole number, and nothing will go wrong with that.
+ 1
My suggestion is that never use == when comparing to float numbers, even with double precision, it will be wrong in some cases.
And yes, 9.0 actually shortened to integer in this code, so it works fine
- 1
I can't understand zerodivisionerror please help
- 1
THE RESULT IS not equal NOTEQUAL BECAUSE THE VALUES MENTIONED 9.0 and 0.1 BOTH ARE DOUBLE VALUES
IT IS NOT POSSIBLE TO COMPARE A DOUBLE VALUE WITH FLOAT
IT IS SIMILAR TO INCOMPATIBLE TYPES