Skip to content

Conversation

vks
Copy link
Contributor

@vks vks commented Apr 10, 2018

The current f32::to_degrees implementation uses a division to
calculate 180/π, which causes a loss of precision. Using a constant is
still not perfect (implementing a maximally-precise algorithm would come
with a high performance cost), but improves precision with a minimal
change.

This is a backport from std.

The current `f32::to_degrees` implementation uses a division to
calculate 180/π, which causes a loss of precision. Using a constant is
still not perfect (implementing a maximally-precise algorithm would come
with a high performance cost), but improves precision with a minimal
change.

This is a backport from [`std`].

[`std`]: rust-lang/rust@e34c31b
@cuviper
Copy link
Member

cuviper commented Apr 10, 2018

Just for reference, we also discussed the implication that some inputs are now less accurate, in rust-lang/rust#48617. But the rounding is more correct in general, and here we forward to std when possible anyway.

src/float.rs Outdated

#[test]
fn to_degrees_rounding() {
assert_eq!(1_f32.to_degrees(), 57.2957795130823208767981548141051703);
Copy link
Member

Choose a reason for hiding this comment

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

When we forward to std, this fails on Rust 1.8 since it lacks rust-lang/rust#47919. Maybe just limit this test to no-std builds? (with a comment explaining this reason)

@cuviper
Copy link
Member

cuviper commented Apr 17, 2018

Strange, 1.8 still failed!

---- float::tests::to_degrees_rounding stdout ----
	thread 'float::tests::to_degrees_rounding' panicked at 'assertion failed: `(left == right)` (left: `57.295776`, right: `57.29578`)', src/float.rs:2022

@cuviper
Copy link
Member

cuviper commented May 4, 2018

Solved by explicitly calling FloatCore in the test.

bors r+

bors bot added a commit that referenced this pull request May 4, 2018
61: Use constant for 180/π in f32::to_degrees r=cuviper a=vks

The current `f32::to_degrees` implementation uses a division to
calculate 180/π, which causes a loss of precision. Using a constant is
still not perfect (implementing a maximally-precise algorithm would come
with a high performance cost), but improves precision with a minimal
change.

This is a backport from [`std`].

[`std`]: rust-lang/rust@e34c31b

Co-authored-by: Vinzent Steinberg <[email protected]>
Co-authored-by: Josh Stone <[email protected]>
@vks
Copy link
Contributor Author

vks commented May 4, 2018

Thanks for the fix! I forgot about this PR...

@bors
Copy link
Contributor

bors bot commented May 4, 2018

Build succeeded

@bors bors bot merged commit 6aaff33 into rust-num:master May 4, 2018
@vks vks deleted the to_degrees-precision branch May 4, 2018 19:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants