Operations in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Operations in python

Hello, I am making operations and I notice that if I make 1-273.15+273.15 it returns 1, but if I make 0.9-273.15+273.15 it doesn't return 0.9. Someone knows why that happens?

10th Dec 2017, 8:50 PM
Luís Afonso
Luís Afonso - avatar
2 Answers
+ 1
I solve it using the built-in module decimal: for decimal import * Decimal("0.9")-Decimal("273.15")+ Decimal("273.15") Here is the tip for everyone having this problem as we can see here: https://docs. python.org/3/library/decimal.html
12th Dec 2017, 8:07 PM
Luís Afonso
Luís Afonso - avatar
+ 4
The returned result is essentially 0.9, it's just how fractions are stored and represented. Read more about it under here: https://en.m.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems
10th Dec 2017, 9:08 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar