-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
core::num::dec2flt interprets the rounding mode incorrectly for negative numbers #41753
Comments
|
I get the same output when the optimizations are off and the strings are It can still be said that the behavior of |
Rust in general doesn't care about/support changing rounding modes at all (partly as a consequence of LLVM not supporting it well, but also because our numerical story is rather naive in general). Furthermore, even if it did in some contexts, it's far from obvious to me that parsing should respect the rounding mode (e.g., it may conflict with the goal of perfect round-tripping of float formatting). Maybe IEEE-754 implies it, I haven't checked.
I haven't thought deeply about it, but it's definitely more involved than that. This wording seems to imply you think of the conversion as the equivalent of |
I didn't intend to imply that the conversion consists of simply summing up the weighted digits. What I tried to imply was that it is a series of standard floating operations that ultimately converges to the result. That still may be wrong of course. Also, as you rightly said, the rounding logic of the conversion may conflict with that of the CPU. I checked the implementation of I would still like to see this issue resolved in the far future where Rust can handle floats as good as C does. I leave it to you whether to label this issue for future reference or just to close it. |
Triage: no changes. |
I am going to close this issue as I think we're currently not intending to utilize the floating point rounding mode within libcore (or otherwise). |
…Dylan-DPC Document rounding for floating-point primitive operations and string parsing The docs for floating point don't have much to say at present about either the precision of their results or rounding behaviour. As I understand it[^1][^2], Rust doesn't support operating with non-default rounding directions, so we need only describe roundTiesToEven. [^1]: rust-lang#41753 (comment) [^2]: llvm/llvm-project#8472 (comment) This PR makes a start by documenting that for primitive operations and `from_str()`.
Document rounding for floating-point primitive operations and string parsing The docs for floating point don't have much to say at present about either the precision of their results or rounding behaviour. As I understand it[^1][^2], Rust doesn't support operating with non-default rounding directions, so we need only describe roundTiesToEven. [^1]: rust-lang/rust#41753 (comment) [^2]: llvm/llvm-project#8472 (comment) This PR makes a start by documenting that for primitive operations and `from_str()`.
core::num::dec2flt
interprets the floating-point rounding mode incorrectly for negative numbers. The assumption in the implementation that negative numbers can be handled by just flipping the sign bit is wrong in the general case.Observed Behavior
core::num::dec2flt
rounds negative numbers upwards when the rounding mode is set to downwards and vice versa.Here is the code that demonstrates the issue.
Output:
Here is the equivalent code in C.
Output:
C handles the rounding correctly whereas Rust gets it wrong for negative numbers.
Expected Behavior
core::num::dec2flt
should either not be affected by the rounding mode and always use half-to-even rounding as stated in the documentation or interpret the rounding mode correctly.Meta
rustc --version --verbose
:The text was updated successfully, but these errors were encountered: