Explain the output please? | Sololearn: Learn to code for FREE!
+ 3

# Explain the output please?

https://code.sololearn.com/c4ET1iGwULOi/?ref=app

3rd Feb 2019, 5:10 AM
Saurabh B
12 Answers
+ 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
+ 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
+ 4
Use double it'll work fine
3rd Feb 2019, 5:28 AM
Random
+ 4
U mean 9.0 is float and 0.1 is double...... something like that
3rd Feb 2019, 6:28 AM
Saurabh B
+ 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
+ 3
Ok but why first if statement is true then if(a==9.0)
3rd Feb 2019, 6:25 AM
Saurabh B
+ 2
Strange. It seems to me that b is apperently -1610612736, instead of 0.1.
3rd Feb 2019, 5:17 AM
Rowsej
+ 2
Because itâs a whole number, and nothing will go wrong with that.
3rd Feb 2019, 6:27 AM
Rowsej
+ 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
- 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