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

Handle to_f64() with raw division by zero #83

Merged
merged 1 commit into from
Oct 30, 2020

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Aug 28, 2020

You're not really supposed to have a zero denominator, but it's possible
with Ratio::new_raw, and we can treat that more carefully when
converting to floats. If the numerator is also zero, return None
rather than using NaN. Otherwise, return Some(±infinity) matching the
sign of the numerator.

You're not really supposed to have a zero denominator, but it's possible
with `Ratio::new_raw`, and we can treat that more carefully when
converting to floats. If the numerator is also zero, return `None`
rather than using NaN. Otherwise, return `Some(±infinity)` matching the
sign of the numerator.
cuviper added a commit to cuviper/num-traits that referenced this pull request Aug 28, 2020
The implementation of `<f64 as ToPrimitive>::to_f32` was written at a
time when float-to-float overflow was though to be undefined behavior,
per rust-lang/rust#15536, but this was later determined to be fine.
Casting a large `f64` to `f32` just results in an infinity with the
matching sign. The sign gives more information than if `to_f32` just
returns `None`, so now we let these infinities through as a result.

See also rust-num/num-bigint#163 and rust-num/num-rational#83.
bors bot added a commit to rust-num/num-traits that referenced this pull request Oct 29, 2020
185: Trust the "i128" feature r=cuviper a=cuviper

If the "i128" feature is explicity requested, don't bother probing for
it. It will still cause a build error if that was set improperly.

186: Allow large f64-to-f32 to saturate to infinity r=cuviper a=cuviper

The implementation of `<f64 as ToPrimitive>::to_f32` was written at a
time when float-to-float overflow was though to be undefined behavior,
per rust-lang/rust#15536, but this was later determined to be fine.
Casting a large `f64` to `f32` just results in an infinity with the
matching sign. The sign gives more information than if `to_f32` just
returns `None`, so now we let these infinities through as a result.

See also rust-num/num-bigint#163 and rust-num/num-rational#83.

190: Normalize the comment style r=cuviper a=cuviper



Co-authored-by: Josh Stone <cuviper@gmail.com>
@cuviper
Copy link
Member Author

cuviper commented Oct 30, 2020

bors r+

@bors
Copy link
Contributor

bors bot commented Oct 30, 2020

@bors bors bot merged commit 2a567d3 into rust-num:master Oct 30, 2020
@cuviper cuviper deleted the raw-to_f64 branch July 21, 2023 22:28
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.

1 participant