Ask Your Question

Revision history [back]

Evaluate from string an equation that has integer division

I have the following Python list that contains strings of equations.

L = ['651/349*t + 5382747/9778631000', 't + 57879/196133000', '1000/349*t + 57879/68450417']

You can see that all of the equations have integer division. Because this is obtained by some program, I can not edit the strings.

When I evaluate for t=1.0 with the following code in SageMath (for example), it does not evaluate as an Euclidean division.

F = function('F')(t)
for k in L:
    F(t) = eval(k)
    Fc = fast_callable(F, vars=[t])
    val2eval = 1.0
    print(Fc(val2eval).n(13))

It gives

1.00000000000000
1.00000000000000
2.00000000000000

And should be give

1.86587997307599
1.00029510077345
2.86617507384944

Of course I can solve this by modifying MANUALLY the strings of the equations by indicating that the denominator is a real number and not an integer (I put a decimal point at the end of the integer in the denominator) as following.

L = ['651/349.*t + 5382747/9778631000.', 't + 57879/196133000.', '1000/349.*t + 57879/68450417.']

But this is not the idea, the strings of the equations are generated automatically and one can not modify by editing manually, because it will be implemented in a process where it should be create at least 2000 equations.

Is there some elegant solution for this? --Many thanks!