Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modulo vs remainder #49

Open
ShawSumma opened this issue Jun 12, 2021 · 3 comments
Open

modulo vs remainder #49

ShawSumma opened this issue Jun 12, 2021 · 3 comments
Labels
design Ideas pertaining to language design question Question asking for help or clarification

Comments

@ShawSumma
Copy link
Member

x % y

passerine:

n % m m = 7 m = -7
n = 4 3 1
n =-4 1 3

c rust haskell lua js python:

n % m m = 7 m = -7
n = 4 3 -1
n =-4 1 -3

This behavior is not what I expected. I think the operator % should behave like it does in other languages.
An operator for what passerine has now is great, but it should be named something else.

@slightknack
Copy link
Member

slightknack commented Jun 12, 2021

That is a good point. In Passerine, % is the euclidean remainder operator. I guess see this post as to the mathematical justification why.

If we look at a set of integers, modulo N, all integers in that set must be 0 ≤ x < N. For this reason, using % to take the modulo should return a result within that set. This might not be how it works in other programming languages, and perhaps it might be a good idea to explicit distinguish between the different types of modulo, remainder, etc. that can happen. I guess the question of whether the euclidean remainder operator is a good default for % is up for debate. On one hand, I'd like there to be some cross-language familiarity, but on the other hand, I'd like the operator to be correct within the context of the definitions we've defined.

@complyue
Copy link

I suggest it's kinda safe to follow Haskell in this regard, there both quot / rem and div / mod are defined and honored equally, as no one pair is more "correct" than the other. For Python, I think Guido didn't realize the other pair is as "nice" mathematically, at http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html

I guess how (%) should behave is better considered together with integer division operator (we have a standard // operator in passerine?) , they should satisfy either "(x quot y)*y + (x rem y) == x" or "(x div y)*y + (x mod y) == x" at least. Which one to choose is less important but better to be conventional.

@slightknack slightknack added design Ideas pertaining to language design question Question asking for help or clarification labels Jul 6, 2021
@slightknack
Copy link
Member

Thanks @complyue, I'm glad you brought up the quot / rem and div / mod relationship! Passerine does not have a standard // integer division operator implemented. I think that div / mod makes the most sense by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Ideas pertaining to language design question Question asking for help or clarification
Projects
None yet
Development

No branches or pull requests

3 participants