While doing exercise on the topic of float type to Fraction type conversion in Python 3.52, I found the difference between the two different ways of conversion.
The first method is:
>>> from fractions import Fraction
>>> x = 1232.23
>>> f = Fraction(*x.as_integer_ratio())
>>> print(f)
2709702426188841/2199023255552 #Answer
The second method is:
>>> from fractions import Fraction
>>> x = 1232.23
>>> f = Fraction(str(x))
>>> print(f)
123223/100 #Answer
I want to know the reason behind these two different answers? Thank you in advance for your time. Any clue will be very considered. Sorry if this is a stupid question, I am new to programming and Python.
Edited: I found a way to convert inaccurate fraction obtained by first method to accurate by limit_denominator
method:
>>> from fractions import Fraction
>>> x = 1232.23
>>> f = Fraction(*x.as_integer_ratio())
>>> f = f.limit_denominator(100)
>>> print(f)
123223/100
>>> 0.1+0.1+0.1-0.3
5.551115123125783e-17
– Eaddy