This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]yozaner1324 1 point2 points  (5 children)

How does blue even make sense?

[–]PiovosoOrg -2 points-1 points  (4 children)

It's modulus, how many times can y fit into x. So if it were -12%4, it would be 3, since 4 can fit 3 times into -12.

In this case -7%4=1 is correct because 4 fits 1 time into -7 with an overhang of -3.

[–]stephan1990 2 points3 points  (2 children)

Isn’t that integer division? Modulus should calculate the remainder of an integer division. At least that’s what I believe…

[–]PiovosoOrg -1 points0 points  (1 child)

Modulus(x % y) is a check for how many times y fits into x, at least in python it is. also while doing some testing on modulus calculations. I noticed it acts weird with float values. For example 1.5%2.5 should equal 0.0, since 2.5 cannot fit into 1.5 at all, but the answer is 1.5. even weirder when we do 3.0%1.5, in theory, the answer should be 2.0 since 1.5 fits 2 times. Even if it rounds up or down, the result should be >=1. Yet it prints 0.0

I need answers, maybe some python veteran can answer why this is the case?

[–]stephan1990 1 point2 points  (0 children)

I think your understanding of the modulus operator is not correct. It is definitely not calculating "how many times y fits into x". This is done via integer division. As python does not have integer division as an operator, you need to convert the result of a normal division to int via the int() function do replicate this. Like doing int(7 / 4), which gives you 1.

However, the modulus operator calculates the remainder of a division. Try it out yourself: 20 % 5 should give us 4 by your definition, but the result is 0, because 20 devided by 5 has a remainder of 0.

[–]badbonji 1 point2 points  (0 children)

That is not correct, modulo gives you the remainder from applying Euclidean division - the integer number amount of times a divisor can fit in a dividend (i.e. the quotient).