Ask Your Question
0

unexpected(?) conversion to float while raising an integer to power -1 [closed]

asked 2021-06-21 19:57:54 +0200

Max Alekseyev gravatar image

This is what I observe in Sage 9.3:

sage: type(29**(-1))                                                                                                                                                                                       
<class 'sage.rings.rational.Rational'>
sage: type(ZZ(29)**(-1))                                                                                                                                                                                   
<class 'sage.rings.rational.Rational'>
sage: type(int(29)**(-1))                                                                                                                                                                                  
<class 'float'>

Why the last result is 'float'? Is it a bug or an expected behavior?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Max Alekseyev
close date 2021-11-10 17:25:56.423544

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-06-21 20:14:54 +0200

tmonteil gravatar image

This is the expected behaviour. When you type 29, the Sage preparser ensures that this number is considered as a Sage integer, whose inverse is a rational number. When you write int(29), the resulting number is a Python integer, whose inverse is a float (i.e. the basic representation of real numbers in Python3).

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2021-06-21 19:57:54 +0200

Seen: 103 times

Last updated: Jun 21 '21