+ 5

Python: 3 * 0.4 = 1.2000000000000002

Why is multiplying 3 or 13 or 23 etc. with floats giving such wierd results?

17th Dec 2019, 5:54 PM
Nor'wester 🌪️ 🇧🇩 (INACTIVE)
Nor'wester 🌪️ 🇧🇩 (INACTIVE) - avatar
5 Respuestas
+ 7
That's because computers handle numbers in binary. You can't convert 1.2 into binary. It would be 1.00110011001100110011... (continues infinitely) computer needs to round that, and the result that is converted back to decimal would be inaccurate.
17th Dec 2019, 7:40 PM
Seb TheS
Seb TheS - avatar
+ 4
It's called floating point arithmetic error, it is not related to python only, but to most (all?) programming languages. The reason is because computer cannot represent floats with high precision, therefore sometimes there can be an error while calculating them. For example if you try: 0.1+0.1+0.1 == 0.3 This will return False, so it can really affect the program if you don't know about it. You can read more about it here: http://effbot.org/pyfaq/why-are-floating-point-calculations-so-inaccurate.htm
17th Dec 2019, 6:00 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
https://m.youtube.com/watch?v=wPBjd-vb9eI
17th Dec 2019, 6:07 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
Ярослав Вернигора (Yaroslav Vernigora) Iam watching the video, great link 👌
17th Dec 2019, 6:16 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
0
sorry, but I didn't watch this video. I found it in a search engine just now. really great?
17th Dec 2019, 6:18 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar