Hi Shreyan,
we need this for symbolic math, e.g. in sympy. Probably you have never seen
somebody doing symbolic math with Python, but believe me, there are many.
Let me walk you through a toy problem to show where the issues are. Let's say
we want to solve the equation x**2 == 3. Not a very tough equation, in reality
things would be much more complicated. To start, we import sympy:
>>> from sympy import symbols, solve
Then we need to declare our variable:
>>> x = symbols("x")
Now we would just use the solve function to solve the equation. Unfortunately
the == operator does not work (another, independent problem), so we have to
reformulate our equation to x**2 - 3 == 0, and knowing that sympy implicitly
adds "== 0" to the equation, we can just type
>>> solve(x**2-3)
[-sqrt(3), sqrt(3)]
The result is not very astonishing. But wait, we realize we did a mistake, it
is not actually 3 that we have to equate to, but 2/3! So we type
>>> solve(x**2-2/3)
[-0.816496580927726, 0.816496580927726]
That went wrong. We wanted to have a symbolic result, not a numeric solution!
How did that come about? Well, that's the point of discussion: 2/3 gets
immediately turned into 0.66666..., and sympy has no clue where this is coming
from. Sure, we can reformulate our problem:
>>> solve(3*x**2-2)
[-sqrt(6)/3, sqrt(6)/3]
in our toy problem that is simple, but imagine what happens if you have 20+
terms that need to be changed. This is when it comes in handy to just be able
to write 2/3 to mean two-third. Sympy gives you other ways than reformulating
the problem to solve it, but that's a different topic. My point is, with my
proposal, you can just write
>>> solve(x**2 - 2/3)
[-sqrt(6)/3, sqrt(6)/3]
no weird things necessary.
But you might ask, what if the fraction is not a constant? Well, in this case
we need to declare another variable, and also tell solve in a second parameter
which variable we want to solve for:
>>> a=symbols("a")
>>> solve(x**2 - a/3, x)
[-sqrt(3)*sqrt(a)/3, sqrt(3)*sqrt(a)/3]
ok, but if we now put some concrete value for a, would that not cause problems?
Well the way you do that is to substitute:
>>> equation = x**2 - a/3
>>> solve(equation.subs(a, 2))
[-sqrt(6)/3, sqrt(6)/3]
>>> solve(equation.subs(a, 3))
[-1, 1]
as you see, there is no problem here. This is why the discussion whether the
ratiofloat should also appear when we are dividing two variables which contain
an int was not very interesting to me, as this is not a relevant problem for
symbolic math.
I hope I was able to show you where fractional constants are useful. Symbolic
math in Python has many more problems, the fact that we cannot use == for
example (I actually don't know why that is not possible, we should ask people
from SymPy). Also the declaration of variables is a bit weird. But those
problems are for another day.
Cheers
Martin
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/WQA2NVP7ECYEQFV67767547DWD2Q4PG6/
Code of Conduct: http://python.org/psf/codeofconduct/