In [1]:
from fractions import Fraction
from sympy import Rational, nsimplify
In [2]:
Fraction.from_float(1.1)
Out[2]:
Fraction(2476979795053773, 2251799813685248)
In [3]:
Fraction(1.1)
Out[3]:
Fraction(2476979795053773, 2251799813685248)
In [4]:
Rational(1.1)
Out[4]:
$\displaystyle \frac{2476979795053773}{2251799813685248}$
In [5]:
nsimplify(1.1)
Out[5]:
$\displaystyle \frac{11}{10}$
In [6]:
def to_frational(x):
print(Fraction(x))
print(nsimplify(x))
print()
In [7]:
vals = [0.0,
1.2879011017187576,
2.5905203907920316e-16,
-0.4714045207910316,
1.4802973661668753e-16,
0.3450920601366943]
In [8]:
for v in vals:
to_frational(v)
0 0 725023865223831/562949953421312 32197527542969/25000000000000 2627099782632789/10141204801825835211973625643008 259052039079203/1000000000000000000000000000000 -8492068896701029/18014398509481984 -sqrt(2)/3 6004799503160661/40564819207303340847894502572032 9251858538543/62500000000000000000000000000 6216625893760533/18014398509481984 172546030068347/500000000000000