+ 3

Python Fraction Isuue

I noticed that (n*3) * 0.1, where n = 0.25 to 2 results highly accurate fraction values, like 0.0750000001 or 0.3000000004. However, when n = 3 i.e. 9 * 0.1 gives a proper 0.9! Why this sort of discrepancy?

5th Oct 2018, 2:40 PM
Tamoghna Saha
3 Respuestas
+ 8
https://docs.python.org/3/tutorial/floatingpoint.html
5th Oct 2018, 3:09 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 1
As a note, if you want to work with real fractions, you can use the fractions module. from fractions import Fraction print(Fraction(1,2) + Fraction(1,3)) # 5/6
7th Oct 2018, 7:03 AM
Adam Blažek
Adam Blažek - avatar