Skip to content

Implement TryFrom<f64> for f32 #50552

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

Closed
wants to merge 1 commit into from

Conversation

clarfonthey
Copy link
Contributor

This will only perform the conversion if it can be done without any loss of precision.

@rust-highfive
Copy link
Contributor

r? @TimNN

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 9, 2018
@scottmcm
Copy link
Member

scottmcm commented May 9, 2018

Previous conversations about TryFrom<float> for integers: #47857

@kennytm
Copy link
Member

kennytm commented May 9, 2018

Please add test cases for these.

Also, this would make e.g. assert_eq!(f32::try_from(0.1_f64), None), which may be too strict? I'd expect it return None only if overflow occurs. Perhaps TryFrom isn't the proper way to introduce this operation, but rather separate methods allowing user to specify what to do with overflow, underflow, inexact exceptions and rounding direction (IEEE-754-2008 §4.3).

@kennytm kennytm added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 9, 2018
@clarfonthey
Copy link
Contributor Author

I'll take a look at these in a bit. You're right on the inexactness, although it's the same if you try f32::try_from(f64::consts::pi). You are fundamentally losing accuracy there.

@hanna-kruppe
Copy link
Contributor

See https://internals.rust-lang.org/t/how-should-we-provide-fallible-float-to-int-conversions/6708/1

@pietroalbini pietroalbini added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 14, 2018
@pietroalbini
Copy link
Member

Ping from triage @clarcharr! It's been a while since we heard from you, will you have time to work on this again soon?

@clarfonthey
Copy link
Contributor Author

@pietroalbini I haven't had time to look at this in the past few days, although I'm probably fine closing this for now. I'll open a new PR with tests once I get the time.

@pietroalbini
Copy link
Member

Sure.

@clarfonthey clarfonthey deleted the try_from_float branch July 23, 2018 01:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants