-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
f32/f64 sqrt has undefined behaviour for values less than -0.0 #9987
Comments
cc @bjz, @jensnockert |
It is correct that this is an issue, but I think the solution is wrong. We should not break with IEEE 754 unless we have really good reason. The correct thing imho is to essentially to return NaN, but we should double-check the standard. |
@jensnockert: The POSIX/C solution is to set |
POSIX/C does not follow IEEE 754, it allows for different formats of floating-point, and doesn't require NaN. That is why it has an additional way to signal out-of-domain. IEEE 754 was not ubiquitous when they were designed. From the POSIX standard
But since we can assume IEEE 754 in the standard library (there are no other floating-point standards that are popular anymore) I think we should just return NaN. From IEEE 754-2008
I believe that the POSIX workaround will be surprising to others, since we have an in-band signalling mechanism that is standardized. This is the same in-band mechanism we use for example overflow and division by zero for floating-point operands. |
Ah, okay, that's a much simpler way to do it. |
@jensnockert: would we just be returning |
That should be fine. |
Wouldn't we have to also make sure the floating point environment exception is set properly? Or are we not worrying about that? |
0 / 0 throws the same exception as sqrt of something negative (invalid operation), so it should be fine. But if we ever expose floating-point exceptions, then we would probably need to check the f32/f64 modules for similar issues. |
@jensnockert: I'll get around to sending in a fix like this to the standard library then: https://github.com/thestinger/rust-core/commit/b5cdd2c10952527f100bc439b1ecb993dface0d1 |
Looks awesome. |
The following is true as of right now on the playpen: assert_eq!((-1.0f32).sqrt(), 0.0f32);
|
@DanielKeep: It's undefined behaviour. The returned result is arbitrary so it will change based on optimizations. It will crash / corrupt memory in some cases. Even if it happened to return |
This fixes a reappearance of bug rust-lang#9987 introduced in 1ddee80, which caused f64::tests::test_sqrt_domain to fail (at least on some systems).
Don't cross contexts while building the suggestion for `redundant_closure_call` fixes rust-lang#9957 changelog: `redundant_closure_call`: Don't cross macro contexts while building the suggestion
http://llvm.org/docs/LangRef.html#llvm-sqrt-intrinsic
The text was updated successfully, but these errors were encountered: