In Python, about the remainder using a negative number.

Asked 2 years ago, Updated 2 years ago, 29 views

Python Code

 print (-7%2) #1
print(-7%3)#2
print(7%-2)#-1
print(-7%3)#2
print(7%-3)#-2

But don't you know why?

python

2022-09-30 21:48

2 Answers

The reason is, "That's what I decided to do."Specification: The division of integers is rounded in a negative infinite direction.
(2)

of https://docs.python.org/ja/3/library/stdtypes.html

Also, if (x//y)*y+(x%y)=x is not present, the remainder will inevitably be determined.

This behavior shows questions tagged with or , this is the decision you made.

# I want to know why Oira decided to do so.


2022-09-30 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.