Ask Your Question
1

difference Python and Sage - integer and rational number

asked 2021-03-10 22:48:57 +0100

Miroslaw gravatar image

updated 2021-03-10 23:04:22 +0100

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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-03-11 00:15:36 +0100

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
edit flag offensive delete link more

Comments

great. thanks a lot.

Miroslaw gravatar imageMiroslaw ( 2021-03-11 07:57:53 +0100 )edit

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: 2021-03-10 22:48:57 +0100

Seen: 346 times

Last updated: Mar 11 '21