Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign up.to_uint() and .to_int() should return None on NaN #16613
Comments
This comment has been minimized.
This comment has been minimized.
|
So the most obvious fix to me for these specific concerns is to have:
But this raises some intent ambiguities for me. Is this API intended to provide a generic interface for casting, or meant to provide a more finicky alternative, for when you really care if the cast is sane (similar to providing checked versions of the math operators)? Right now we snap to the "nearest" valid value. So for instance |
This comment has been minimized.
This comment has been minimized.
|
Well to me at least, when a function returns an Furthermore I think it is just as sane to have But I see your point in that where to draw the line between valid and invalid input is not all that obvious. However if the current implementation sticks around, I would still love it if a short notice was added to the docs. FWIW though I would be fine with your current implementation for |
This comment has been minimized.
This comment has been minimized.
|
This is still an issue. I updated the playpen to compile with the latest rust |
huonw
added
the
A-libs
label
Jan 29, 2015
This comment has been minimized.
This comment has been minimized.
|
@bjz @nikomatsakis any thoughts here? |
This comment has been minimized.
This comment has been minimized.
|
I don't know, and it depends on the use case. I would tend towards saying that we should let the user handle the NaN. Adding in a conditional may prevent llvm from optimizing out those annoying In more general terms, I would love some way of generically exposing the raw |
This comment has been minimized.
This comment has been minimized.
|
Should we have a suite of casts for different usecases? |
This comment has been minimized.
This comment has been minimized.
|
Nominating for 1.0-beta P-backcompat-libs. |
aturon
added
the
I-nominated
label
Feb 16, 2015
This comment has been minimized.
This comment has been minimized.
|
1.0-beta, P-backcompat-libs. |
pnkfelix
added
the
P-backcompat-libs
label
Feb 19, 2015
pnkfelix
added this to the 1.0 beta milestone
Feb 19, 2015
brson
added
E-easy
and removed
P-backcompat-libs
labels
Feb 19, 2015
pnkfelix
added
P-backcompat-libs
and removed
E-easy
I-nominated
labels
Feb 19, 2015
This comment has been minimized.
This comment has been minimized.
|
Do as the title says. Easy. |
This comment has been minimized.
This comment has been minimized.
|
@brson Is this an endorsement of the change? |
This comment has been minimized.
This comment has been minimized.
|
Note: we do not plan to stabilize the triage: P-low () |
rust-highfive
removed
the
P-backcompat-libs
label
Mar 20, 2015
aturon
removed this from the 1.0 beta milestone
Mar 20, 2015
This comment has been minimized.
This comment has been minimized.
This has happened, and the traits have now been entirely removed too. Closing. |
opp11 commentedAug 19, 2014
Calling
.to_uint()or.to_int()on anf64orf32will always return a Some value, even if they are called with NaN (playpen link):As far as I can tell this this behavior comes from the fact that
.to_int()and.to_uint()is just implemented as casts as seen here:This seems (to me at least) a bit unintuitive since you would expect the method to return None when called with invalid input - such as NaN which has no obvious conversion to a
u/int- since the method returns an Option.Alternatively it would nice if it was mentioned in the docs, that the methods are the same as a cast for primitive types.
EDIT:
It seems like something similar happens for infinity.
EDIT 2:
Updated the playpen links - thank you frewsxcv!