Skip to content

Implement TryFrom<f64> for f32#50552

Closed
clarfonthey wants to merge 1 commit into
rust-lang:masterfrom
clarfonthey:try_from_float
Closed

Implement TryFrom<f64> for f32#50552
clarfonthey wants to merge 1 commit into
rust-lang:masterfrom
clarfonthey:try_from_float

Conversation

@clarfonthey
Copy link
Copy Markdown
Contributor

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

@rust-highfive
Copy link
Copy Markdown
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
Copy Markdown
Member

scottmcm commented May 9, 2018

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

@kennytm
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

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

@emilyalbini emilyalbini 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
@emilyalbini
Copy link
Copy Markdown
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
Copy Markdown
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.

@emilyalbini
Copy link
Copy Markdown
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