First time here? Check out the FAQ!

Ask Your Question
1

difference Python and Sage - integer and rational number

asked 4 years ago

Miroslaw gravatar image

updated 4 years ago

in Python, if k is an integer, equation k / 4 returns the integer part of k over 4, while in Sage, it returns a rational. How to work in Sage and change from result od divide from rational to like in Python ?

example: python

5 divide by 3 = 1

5 divide by 2 = 2

4 divide by 2 = 2

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
3

answered 4 years ago

slelievre gravatar image

The Python behaviour you describe was that of Python 2, which is no longer current.

In Python 3:

>>> 5 / 3
1.6666666666666667
>>> 5 / 2
2.5
>>> 4 / 2
2.0

In Sage, if you want "floor division" (integer quotient without remainder), use // instead of /.

sage: 5 // 3
1
sage: 5 // 2
2
sage: 4 // 2
2
Preview: (hide)
link

Comments

great. thanks a lot.

Miroslaw gravatar imageMiroslaw ( 4 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 4 years ago

Seen: 409 times

Last updated: Mar 11 '21