-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
What does the % operator on floats do? #57738
Comments
Definitely not.
a /& b = f64::trunc(a / b) and when I refer to this as its corresponding division function, I mean that a = (a /& b) * b + (a % b) I believe it is the same as the C function Other notable properties:
I really do feel that this stuff should be documented somewhere. |
sorry, |
Thank you @ExpHP. |
Not sure if this is currently documented anywhere, but at the very least, it should be mentioned here |
@AljoschaMeyer to be honest, this is more or less how I naturally suspected it would work, based on experience with a similar Your post is the very first time I've ever heard that IEEE-754 specifies a modulus operation. Its definition (as the partner to I'm not sure what provides the implementation, or if it is even correctly rounded. However, one can see from the trivial trait impl that it is a compiler-builtin operation that likely lowers to some intrinsic. |
Floating point rust/src/librustc_codegen_ssa/mir/rvalue.rs Lines 584 to 585 in 01f8e25
rust/src/librustc_codegen_llvm/builder.rs Lines 378 to 383 in 01f8e25
|
Would it be possible to implement a canonical modulus function, perhaps
The rule is that the output wraps around the second argument on a range |
There is an effort to add euclidean modulus and division to all numerical types. These define the modulus to lie in (worth noting: Both yours and my definition are typically capable of producing a value that is exactly equal to |
@AljoschaMeyer Would you still like to fix this? Otherwise I could take an attempt at improving the documentation (Y). |
@DevQps Please go ahead =) |
@steveklabnik I'd like to fix this, and since there were no other assignee's I didn't know who else to ping :) I searched |
@DevQps -- the actual operation is in the compiler, but the trait is still here: Lines 538 to 552 in 33ef0ba
|
@cuviper @steveklabnik Thanks for the info! I took a look at it (and looked some information up about macro's as well). I see these solutions:
Additional question: I wonder what you think is the best, and then I will go and create a Pull Request! (Unless you also have open points of course) |
I'd go with option 1. |
Added documentation on the remainder (Rem) operator for floating points. # Description As has been explained in rust-lang#57738 the remainder operator on floating points is not clear. This PR requests adds some information on how the `Rem` / remainder operator on floating points works. Note also that this description is for both `Rem<f32> for f32` and `Rem<f64> for f64` implementations. Ps. I wasn't really sure on how to formulate things. So please suggest changes if you have better idea's! closes rust-lang#57738
I can't find any documentation on the behavior of the
Rem
implementation forf64
(orf32
). Where can I find out how it precisely works, regarding rounding modes etc? Is it the IEEE 754-2008 "remainder" operation?The text was updated successfully, but these errors were encountered: