Ask Your Question

Revision history [back]

Python's float does not have high precision, and when you do the division number/2 in Python, that's what you get. Try replacing number with Integer(number) to make sure you use Sage's arbitrary precision integers; once you've done that, then any divisions (for example) involving these integers will remain exact. Try to avoid converting to float either explicitly or implicitly. Here is an example illustrating some differences between Sage Integer and Python int:

sage: type(4)                                                                                                            
<class 'sage.rings.integer.Integer'>
sage: 4/3                                                                                                                
4/3
sage: type(4/3)                                                                                                          
<class 'sage.rings.rational.Rational'>
sage: 3 * 4/3                                                                                                            
4

as compared to

>>> type(4)
<class 'int'>
>>> 4/3
1.3333333333333333
>>> type(4/3)
<class 'float'>
>>> 3 * 4/3
4.0

Also, you lose precision when you move from Sage numeric types to Python ones:

sage: a                                                                                                                  
117849195257837440502398011747896672
sage: int(float(a))                                                                                                      
117849195257837440633821053382033408

Python's float does not have high precision, and when you do the division number/2 in Python, that's what you get. Try replacing number with Integer(number) to make sure you use Sage's arbitrary precision integers; once you've done that, then any divisions (for example) involving these integers will remain exact. Try to avoid converting to float either explicitly or implicitly. Here is an example illustrating some differences between Sage Integer and Python int:: Sage

sage: type(4)                                                                                                            
<class 'sage.rings.integer.Integer'>
sage: 4/3                                                                                                                
4/3
sage: type(4/3)                                                                                                          
<class 'sage.rings.rational.Rational'>
sage: 3 * 4/3                                                                                                            
4

as compared toto Python

>>> type(4)
<class 'int'>
>>> 4/3
1.3333333333333333
>>> type(4/3)
<class 'float'>
>>> 3 * 4/3
4.0

Also, you lose precision when you move from Sage numeric types to Python ones:

sage: a                                                                                                                  
117849195257837440502398011747896672
sage: int(float(a))                                                                                                      
117849195257837440633821053382033408

Python's float does not have high precision, and when you do the division number/2 in Python, that's what you get. Try replacing number with Integer(number) to make sure you use Sage's arbitrary precision integers; once you've done that, then any divisions (for example) involving these integers will remain exact. Try to avoid converting to float either explicitly or implicitly. Here is an example illustrating some differences between Sage Integer and Python int: Sage

sage: type(4)                                                                                                            
<class 'sage.rings.integer.Integer'>
sage: 4/3                                                                                                                
4/3
sage: type(4/3)                                                                                                          
<class 'sage.rings.rational.Rational'>
sage: 3 * 4/3                                                                                                            
4

as compared to Python

>>> type(4)
<class 'int'>
>>> 4/3
1.3333333333333333
>>> type(4/3)
<class 'float'>
>>> 3 * 4/3
4.0

Also, you lose precision when you move from Sage numeric types to Python ones:

sage: a = 117849195257837440502398011747896672 
sage: a                                                                                                                  
117849195257837440502398011747896672
sage: int(float(a))                                                                                                      
117849195257837440633821053382033408