Re:Taming math and physics using SymPy [pdf]
by zokier on Friday August 29, 2014 @03:40PM
(#8244210)
> Special care is required when specifying rational numbers, because integer division might not produce the answer you want. In other words, Python will not automatically convert the answer to a floating point number, but instead round the answer to the closest integer
Note that this applies only to Python2, Py3k does float-division by default and integer division with // operator:
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/7
0
>>> from __future__ import division
>>> 1/7
0.14285714285714285
>>> 1//7
0
>>>
Re:Taming math and physics using SymPy [pdf]
by tjl on Saturday August 30, 2014 @01:51AM
(#8245796)
There's a couple of other things you can do. Since those are Python integers, you can specify a SymPy Integer instead with Integer(1)/7 instead. The other thing you can do is S(1)/7. Both of these should work.
Re:Taming math and physics using SymPy [pdf]
by krastanov on Friday August 29, 2014 @04:18PM
(#8244330)
Which is still not what you want as floating point numbers do not cover all possible rationals. When the input is done correctly sympy can deal with arbitrary rationals as well.
Note that this applies only to Python2, Py3k does float-division by default and integer division with // operator: