Hi,
this will be maybe a repetitive question regarding how to deal with floats, but anyway:
having the following expression: ((1 - 1/6) / (1/12 + 1/8))
If I type it to google I get 4.
If I type it to spreadsheet (of any provider) still I get the result 4.
If I type it to Python, I get 4.000000000000001.
I wonder what would be the correct approach if I wanted the output to be same as from google or spreadsheet?
If you want an exact result, you need to perform the actual computation of ((1 - 1/6) / (1/12 + 1/8)) using something other than floats. There’s nothing you can do to the float result to make it more exact — the error is already there.
You can ask sympy to evaluate a string as a symbolic expression by calling sympy.sympify:
You can also use the fractions module from the standard library to do exact rational arithmetic:
from fractions import Fraction as F
a = F(1, 6)
b = F(1, 12)
c = F(1, 8)
ans = (1 - a) / (b + c)
>>> print(ans, type(ans))
4 <class 'fractions.Fraction'>
Sympy is a very heavy and overkill dependency for this sort of task, and parsing a string for a mathematical expression is not very elegant. I would definitely take the fractions.Fraction approach by default.