-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
libexpr: add modulo operator #12617
base: master
Are you sure you want to change the base?
libexpr: add modulo operator #12617
Conversation
we need modulo operator in nix !!! |
fixed a typo in one of the error trace tests |
A mod function is definitely a good idea, and assigning |
May i ask what kind of opportunity cost is paid here for using |
Some thoughts in random order:
|
It's a bit of a question about unknown unknowns, but ideas like #2511 make me think that it would be good to use operators only for often-used things that are relevant for a configuration language. Having chatted with the friendly Lix folks, they're a bit more hesitant about assigning If we were to treat this as the optimized version of Nixpkgs Could you remove the operator part and keep the |
I did copy either way I'm not sure how long it would take me to fix all of them but I'm willing to try
I am pretty sure that modulo and division behaviour should match - Nix implements division using the C++ if I were to make modulo do something else it wouldn't match with division and I feel like that's the biggest concern
I've had to implement a |
Nope, don't bother fixing them. Having to maintain backwards compatibility with the unintended semantic consequences is a pain, and given these having a proper dedicated operator does not help much. (In case you don't know, the specific issue is that |
regarding the last part of my message above, this is the only real issue I see with this but I also don't necessarily agree
I'd personally keep the operator but I get your worries, I'll remove it |
ah, that makes sense thank you if I'm getting rid of the operator, should I still somehow get rid of |
https://en.m.wikipedia.org/wiki/Modulo#In_programming_languages is a helpful list for seeing how different languages implement the modulo operator. |
again, I'm not sure we want any other behaviour than the one that matches the division operator and I don't think changing division behaviour is very backwards compatible :P |
I don't understand this sentence on a technical level. Integer division, unlike modulo, is pretty much uniquely defined. Therefore, I don't see how one would need to be "compatible" with the division operator. Modulo is a function defined w.r.t. a division operation, sure, and that division operation is fixed, but this doesn't change the fact that there are multiple possible ways of defining modulo in the first place. For that matter, many languages offer multiple definitions as separate library functions. Looking at the semantics in detail (thanks to that great Wikipedia article), I do very much prefer floored or euclidian semantics over truncated (as used by C++), because it guarantees a positive result. With truncated semantics, most of the time I do |
e.g. the example from the tests:
|
Closes: #12616
Motivation
I want the modulo operator in nix
Context
first contribution to nix so please make sure to check that I didn't miss anything
I grepped the project for
div
and replicated everything I found but formod
so I'm pretty sure I got it allworks on my machine™ so hopefully it should be good
this uses the same modulo behaviour as in C++ (since it's implemented using
%
in C++), if any change in its behaviour is desired please let me knowthis will error out with
modulo by zero
if the right hand side is 0, otherwise it can't failit's only implemented for integers because I haven't seen anyone use modulo with floats for anything I don't know if it'd be useful to anyone
Add 👍 to pull requests you find important.
The Nix maintainer team uses a GitHub project board to schedule and track reviews.