3rd Feb 2019, 5:10 AM
Saurabh B
Saurabh B - avatar
12 ответов
+ 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.
3rd Feb 2019, 6:58 AM
Anna
Anna - avatar
+ 8
3rd Feb 2019, 6:06 AM
Ipang
+ 7
Ankit ah yes, I forgot that double has nore precision.
3rd Feb 2019, 5:37 AM
Rowsej
Rowsej - avatar
+ 4
Use double it'll work fine
3rd Feb 2019, 5:28 AM
Random
Random - avatar
+ 4
U mean 9.0 is float and 0.1 is double...... something like that
3rd Feb 2019, 6:28 AM
Saurabh B
Saurabh B - avatar
+ 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.
3rd Feb 2019, 6:30 AM
Rowsej
Rowsej - avatar
+ 3
Ok but why first if statement is true then if(a==9.0)
3rd Feb 2019, 6:25 AM
Saurabh B
Saurabh B - avatar
+ 2
Strange. It seems to me that b is apperently -1610612736, instead of 0.1.
3rd Feb 2019, 5:17 AM
Rowsej
Rowsej - avatar
+ 2
Because it’s a whole number, and nothing will go wrong with that.
3rd Feb 2019, 6:27 AM
Rowsej
Rowsej - avatar
+ 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
3rd Feb 2019, 8:23 PM
Y D
- 1
I can't understand zerodivisionerror please help
3rd Feb 2019, 9:21 AM
bala subramanian
bala subramanian - avatar
- 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
4th Feb 2019, 11:46 AM
sree harsha
sree harsha - avatar