Skip to content

simplify f16 to_degrees and to_radians by using expression directly #145625

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

karolzwolak
Copy link
Contributor

Simplify f16 to_degrees and to_radians by using expression directly.
This seems to return the same values for me, but maybe on some targets the literal is actually better?

I added a test that's gonna check if they are equivalent in CI, we'd probably want to get rid of it once we get verify this.

r? @tgross35

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 19, 2025
Comment on lines 642 to 644
pub const fn to_degrees(self) -> Self {
// Use a literal for better precision.
const PIS_IN_180: f16 = 57.2957795130823208767981548141051703_f16;
self * PIS_IN_180
self * 57.2957795130823208767981548141051703_f16
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The results here will be identical. I meant something like what f64 does where the result is calculated (possibly to a const),

pub const fn to_degrees(self) -> f64 {
// The division here is correctly rounded with respect to the true
// value of 180/π. (This differs from f32, where a constant must be
// used to ensure a correctly rounded result.)
self * (180.0f64 / consts::PI)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you're saying we can just merge this? And why the behavior difference for f64?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, you mean the to_degrees() is a const fn, which in turn could increase precision for constant which uses to_degrees()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite :) I mean that it makes absolutely no difference whether we multiply by the literal 57.2957795130823208767981548141051703, or assign it to a const first, or let. They compiler guarantees that the comptime and runtime semantics are the same here.

What I do mean is for f16, 57.295... is not exactly the same as 180 / pi https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=85a373861e735f8c2a58b00b4540a150. And using the value of 180 / pi happens to roundtrip for an input of 180.0, though not for other values.

I'm not sure which value (exact at larger precision or computed as f16) should be preferred though, @beetrees is more likely to have this knowledge ready to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants