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
Add mod(a,d)
function
#43
Conversation
Yeah, I've thought about adding this function, too. Out of interest: what's your use case? One question is whether it should be an operator ( Another question is: what about negative numbers, both first and second argument? I see most programming languages define modulus in terms of division, which is kind of strange to me. Modular arithmetic seems like a more intuitive way to define it, which might give some guidance to the issue of negative arguments. If we define it in terms of division it seems we have to handle real numbers as well. We also need test cases for the first or second argument being |
The use case is to use in checks to discard messages. The idea is to have a filter based on The difference with Regarding the definition in terms of division: I must be honest and admit that I just wanted to have the same semantics as the java modulo since it's the underlying language where the I definitely add the test cases for null and for negative numbers. I can rethink the definition to make it match exactly with the java definition (which I think it's only defined for integral numbers, not real numbers) |
I like the idea. Thinking about it I think we'd rather not support real numbers for We also need to have a test for This Wikipedia page has a very good overview. Looks like the behaviour in different programming languages is all over the place because the full definition of modulo in terms of division actually involves a choice between several more precise definitions that give different results in this case. I need to set aside some time to read that properly. |
I already have written test for the real number, negative number, and description referencing the Java Remainder operator %. But I can create another commit to remove the real numbers with a test for error. I also notice the different behaviour or the modulo/remainder operator in different languages I was surprise to find that
|
@larsga Please take a look at the changes. I updated the description in functions.md to refer to the Java remainder operator , |
I dug a little deeper into this and found that Java defines The amusing thing is that Python appears to define it the same way: So why do they get different results? Because the division operators are not the same. I see this can be argued many ways, but one thing I don't like about the Java operator is that the expression Thinking about this some more, this matter for our use case. Let's say I write Thankfully, JSLT doesn't have integer division at the moment, so we haven't committed to one of these two choices, and thus the |
I see Java |
@ecerulm Thank you very much for the work so far. I will take over this branch now, just so we can get this finished. |
Given that this function is defined in terms of the remainder, and not in terms of modulo arithmetic, I think it's better to call it Java Python calls it "modulo operator": https://docs.python.org/2/reference/expressions.html#binary-arithmetic-operations, but the text describes it this way: "The % (modulo) operator yields the remainder from the division of the first argument by the second." |
I want to work through the test cases with negative numbers, to verify that I understand what the correct answers are, and why. The guiding principle is this equation: If we rewrite that to JSLT we get Note that
Somehow I convinced myself that Python always returns a value So ... perhaps best to backtrack. What if this were a real modulo function where the definition is |
Using this paper I found an approach that I think works. Use the same basic equation, but Euclidean division. That gives a simple definition, a simple implementation, and a beautifully simple rule for understanding the result. Basically
Note how the right-hand side is just (0, 1, 2) endlessly repeated in that order. So ... this is what we'll go for. |
And since it now really is modulo, we can call the function |
Interesting... This seems different from C and Node.js, where I get the feeling that they use truncation instead of the floor function to do the integer division part... (In both languages, |
I think that's correct. (I haven't checked the |
No description provided.