-
Notifications
You must be signed in to change notification settings - Fork 346
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
implement float_to_int_unchecked #1325
Conversation
I can't give this as much time and thought as I'd need to be more confident, but it seems at least plausibly correct to me. |
@SimonSapin could you have a look at this, maybe? |
Hmm there’s a fair amount going on here and I’m not at all familiar with the code base. Do you have a more specific question? Outside of const eval, |
Hm, if you are not familiar with apfloat then probably it would make most sense if you could look at the testcases. The run-pass
For |
} | ||
|
||
fn main() { | ||
unsafe { float_to_int_unchecked::<f64, u128>(340282366920938463463374607431768211455.0f64); } //~ ERROR: cannot be represented in target type `u128` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this test is correct, but it looks misleading or confusing. 340282366920938463463374607431768211455 (the integer) is u128::MAX
, so at first glance the conversion should succeed. But the 340282366920938463463374607431768211455.0f64
float is in fact not equal to that integer. That integer cannot be represented exactly in f64, so the Rust parser presumably rounds to the nearest representable float which is one more, 2^128, and does indeed overflow conversion to u128.
(I’m using Python to figure this out since it has infinite-capacity integers and f64
floats. a = 340282366920938463463374607431768211455; (hex(a), int(float(a)) - a, a.bit_length())
prints ('0xffffffffffffffffffffffffffffffff', 1, 128)
.)
But the point of these tests is not to test the Rust parser. I feel it’d be better to pick a decimal representation of the float that would, if precision and range were infinite, have the same numerical value as the actual f64 value does.
In fact it’d be even better if the source code of test cases could contain something closer to the memory representation of f64
. Hex float syntax exists, but not in Rust literals. I don’t know if this would be worth using a library for parsing it from strings. I see some other tests use f32::from_bits
with hex integers, but that’s much harder for humans to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, that number here and these from_bits
constants are all derived from https://github.com/WebAssembly/testsuite/blob/master/conversions.wast.
I could change this to u128::MAX as f64
, if you think that's better.
Is there a reason the upper and lower bounds for each integer type could not be determined ahead of time, reducing this code to just: match {integer_type} {
I32 => ({min}..={max}).contains(f),
...
} ? |
I don't see how that would handle edge cases correctly. For example, Also that |
I changed the test @SimonSapin commented on. Otherwise I think this has pretty solid test coverage and the implementation matches what @hanna-kruppe wrote at #1264 (comment). So, I'll land this. @bors r+ |
📌 Commit bb38ab4 has been approved by |
☀️ Test successful - checks-travis, status-appveyor |
@hanna-kruppe would be great if you could have a look at this.
float.rs
tests legal casts.test_cast
checks that bothas
casts and unchecked casts work (i.e., these are not saturating). Thecompile-fail
tests should ensure that illegal casts via the intrinsic are detected as such.Fixes #1264