Skip to content
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

Rust does not specify IEEE 754 rounding mode for signed zero #116339

Closed
orlp opened this issue Oct 2, 2023 · 4 comments · Fixed by #116355
Closed

Rust does not specify IEEE 754 rounding mode for signed zero #116339

orlp opened this issue Oct 2, 2023 · 4 comments · Fixed by #116355
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@orlp
Copy link
Contributor

orlp commented Oct 2, 2023

Location

https://doc.rust-lang.org/std/primitive.f32.html

Summary

The Rust documentation on f32 states (emphasis mine):

When the number resulting from a primitive operation (addition, subtraction, multiplication, or division) on this type is not exactly representable as f32, it is rounded according to the roundTiesToEven direction defined in IEEE 754-2008.

However, this does not specify the behavior for -0.0f32 + 0.0f32. This value is exactly representable, the issue is in fact that it is representable in two ways. According to the IEEE754-2019 standard the behavior should be:

When the sum of two operands with opposite signs (or the difference of two operands with like signs) is exactly zero, the sign of that sum (or difference) shall be +0 under all rounding-direction attributes except roundTowardNegative; under that attribute, the sign of an exact zero sum (or difference) shall be -0.

In other words, the behavior depends on the rounding mode. We should specify that in this scenario we also follow the roundTiesToEven rounding mode, that is, the result of -0.0f32 + 0.0f32 should be 0.0f32.

@orlp orlp added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Oct 2, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 2, 2023
@Noratrieb Noratrieb added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 2, 2023
@thomcc
Copy link
Member

thomcc commented Oct 2, 2023

IMO this is just a docs bug, where we were overly strict by specifying that the rounding mode only matters when the result "is not exactly representable". Patches should be welcome.

@orlp
Copy link
Contributor Author

orlp commented Oct 2, 2023

I agree, there is no reasonable world in which we'll use a different behavior for signed zero than what IEEE 754 prescribes for roundTiesToEven. What would better wording be?

@thomcc
Copy link
Member

thomcc commented Oct 2, 2023

Something along the lines of "is not exactly representable as a distinct f32" perhaps?

@orlp
Copy link
Contributor Author

orlp commented Oct 2, 2023

@thomcc I made a PR with an alternate wording I like. It simply drops any condition on rounding, simply stating we use the roundTiesToEven rounding mode, and clarifying what happens with sums.

"The result is the representable value closest to the true value, if there is a unique closest representable value." also perfectly covers any result that doesn't need to be rounded, so no harm in dropping the condition.

@bors bors closed this as completed in 043fcc4 Oct 3, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 3, 2023
Rollup merge of rust-lang#116355 - orlp:signed-zero-rounding-mode, r=thomcc

Clarify float rounding direction for signed zero

Closes rust-lang#116339.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants