3 ms·
> Special care is required when specifying rational numbers, because integer division might not produce the answer you want. In other words, Python will not aut
by zokier 12y ago
> 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
>>>
- krastanov 12y agoWhich 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.
- tjl 12y agoThere'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.