-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Why does the behavior of n mod 0
differ from the one in JavaScript?
#2617
Comments
Thanks for bringing this up. I don't think there is a strong reason for it. It makes sense to have For reference: https://en.wikipedia.org/wiki/Modulo_operation |
I'd love to have that assigned to me if possible |
Thanks for your offer @KevBeltrao , that would be very nice! |
I know that I have been away from the project for a few weeks, but I would like to voice a contrary opinion here. Mostly mathjs makes the choices that are familiar and common to mathematicians (after all, math is in the name). And that already leads to one big diference in % in particular: in mathjs |
Sorry, I did not address this reference. It is silent on the the question of "mod 0". However, it points out a few major variants of of the mod function, and clearly for positive values of the modulus, mathjs is currently consistent with the "Euclidean division" version of mod. The Euclidean division version would prescribe 22 % 0 should be 22, as |
Thanks for your input Glen. I learn something new every day :). Your explanation makes sense, and in mathjs I would indeed prefer the mathematical way rather than the programming-language way. What are your thoughs @xxczaki, @KevBeltrao ?
I was triggered by the text: "(a mod 1 is always 0; a mod 0 is undefined, ...)" For reference, the current implementation of mathjs/src/plain/number/arithmetic.js Lines 159 to 171 in 09a044f
|
Ah I missed that in the intro. But in the next sentence it includes a reference to the (mathematical) modular arithmetic article, which notes thar the integers are unchanged mod 0.... |
We'll release mathjs v11 coming Friday, everything is ready for this except this open discussion about if there are no further inputs, we'll keep the behavior as it currently is and close this issue coming Friday. |
I'll agree with gwhitney! Thank you for sharing that |
Thanks, @gwhitney for the comments, I agree that it does make sense to leave the existing behavior untouched to stay in parallel with the mathematical way of thinking. I do have a question, however; is there an easy way to override the behavior of this? We can override the |
Wow, you're right; from the demo on mathjs.org
yields |
Ok thanks for your feedback guys. We'll keep the existing behavior of mod, I've removed this topic from the v11 milestone.
Yes, the // default behavior
math.evaluate('22 % 0') // 22
// replace mod
// (the following function is for numbers only, no BigNumber support or anything)
function mod(x, y) {
return x % y
}
math.import({ mod }, {override: true})
// use the new mod
math.evaluate('22 % 0') // NaN I noticed though that this mathematical behavior for |
That is an interesting case. What happens right now is that functions and constants can be provided by the |
I'll close this issue now. The issue about operators not being overridden will be discussed in #2631. |
As far as I can tell, this wasn't raised in any of the issues yet. Hence, I am thinking that this might be the expected behavior. Most of the people I know would say that the second option should be the correct one, although I did some research and contrary opinions exist; https://math.stackexchange.com/questions/516251/why-is-n-mod-0-undefined/516270#516270.
The text was updated successfully, but these errors were encountered: