Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 2011-11-22 11:55:59 -0600

DSM gravatar image

Eval doesn't return 1.666...67 in python-- at least not in python 2, unless you import from the future.

Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 5/3
1
>>> eval('5/3')
1
>>> from __future__ import division
>>> eval('5/3')
1.6666666666666667

Switching to so-called true division was one of the incompatible changes between python 2 and python 3, among other reasons because it proved a pretty common mistake. (I made it myself several times in production code..)

Python 3.2.1 (default, Jul 12 2011, 22:22:01) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> eval('5/3')
1.6666666666666667

Sage's underlying python is python 2, so integer division is truncating, and eval is doing the expected thing here. You can import from the future within Sage in the above way if you want to change the default behaviour, or use sage_eval instead:

----------------------------------------------------------------------
| Sage Version 4.7.2, Release Date: 2011-10-29                       |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: 5/3
5/3
sage: eval('5/3')
1
sage: from __future__ import division
sage: eval('5/3')
1.6666666666666667
sage: sage_eval('5/3')
5/3

Eval doesn't return 1.666...67 in python-- at least not in python 2, unless you import from the future.

Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 5/3
1
>>> eval('5/3')
1
>>> from __future__ import division
>>> eval('5/3')
1.6666666666666667

Switching to so-called true division was one of the incompatible changes between python 2 and python 3, one made among other reasons because it proved a pretty common mistake. (I made it myself several times in production code..)

Python 3.2.1 (default, Jul 12 2011, 22:22:01) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> eval('5/3')
1.6666666666666667

Sage's underlying python is python 2, so integer division is truncating, and eval is doing the expected thing here. You can import from the future within Sage in the above way if you want to change the default behaviour, or use sage_eval instead:

----------------------------------------------------------------------
| Sage Version 4.7.2, Release Date: 2011-10-29                       |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: 5/3
5/3
sage: eval('5/3')
1
sage: from __future__ import division
sage: eval('5/3')
1.6666666666666667
sage: sage_eval('5/3')
5/3

Eval doesn't return 1.666...67 in python-- at least not in python 2, unless you import from the future.

Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 5/3
1
>>> eval('5/3')
1
>>> from __future__ import division
>>> eval('5/3')
1.6666666666666667

Switching to so-called true division was one of the incompatible changes between python 2 and python 3, one made among other reasons because it proved a pretty common mistake. (I made it myself several times in production code..)code, and, come to think of it, it was the source of a number of bugs in Sage's graph plotting code.)

Python 3.2.1 (default, Jul 12 2011, 22:22:01) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> eval('5/3')
1.6666666666666667

Sage's underlying python is python 2, so integer division is truncating, and eval is doing the expected thing here. You can import from the future within Sage in the above way if you want to change the default behaviour, or use sage_eval instead:

----------------------------------------------------------------------
| Sage Version 4.7.2, Release Date: 2011-10-29                       |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: 5/3
5/3
sage: eval('5/3')
1
sage: from __future__ import division
sage: eval('5/3')
1.6666666666666667
sage: sage_eval('5/3')
5/3