Any one can help me why >> .1 *3 = 0.30000000000000004 when executed in python not .3? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Any one can help me why >> .1 *3 = 0.30000000000000004 when executed in python not .3?

Please feedback

20th Mar 2018, 12:28 PM
Solo learner
Solo learner - avatar
3 Answers
+ 10
It's in every language that uses standard floating-point arithmetic. Computers can't store some decimals (floating-points) perfectly, like how we can't store 0.333333333... perfectly in our number system. In binary, 0.1 is one of those repeating decimals. It's quite an interesting topic, actually. I'd suggest reading more if you're interested.
20th Mar 2018, 12:30 PM
LunarCoffee
LunarCoffee - avatar
+ 5
Since computers use binary number system instead of decimal, they lose some precision while operating with floats and doubles because 0.1 in decimal will be 0.0001111.... in binary system, i.e., it loses some precision while converting from decimal to binary. Just like LunarCoffee said, it is difficult to store 0.3333333..... perfectly in decimal system, likewise, 0.1 is not in binary. If you store 0.33333..... in base-3 format, it would store perfectly as 0.1 in that base.
20th Mar 2018, 1:29 PM
Naveen Maurya
Naveen Maurya - avatar
+ 2
it's the precision
22nd Mar 2018, 2:47 PM
Zeta Riemann
Zeta Riemann - avatar