First time here? Check out the FAQ!

Ask Your Question
0

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

asked 3 years ago

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?

Preview: (hide)

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 3 years ago

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).

Preview: (hide)
link

Question Tools

1 follower

Stats

Asked: 3 years ago

Seen: 154 times

Last updated: Jun 21 '21