-
Notifications
You must be signed in to change notification settings - Fork 13.3k
(NaN == x) and (NaN < x) and (x < NaN) are true for all x:float #1083
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
Comments
This is by design. We break IEEE 754 compatibility because < and == are supposed to be used in hashtables and trees and such, and I'd rather not have hashtables mysteriously break when NaNs are used as keys. We should have an IEEE 754-compliant comparison function in the standard library though. |
I discussed this with pcwalton on IRC, and determined that (NaN < 0) is by design, but (NaN == Infinity) and (NaN == -Infinity) are bugs. Updated the issue description. |
All of the assertions in the following program pass in the current Rust implementation. use std;
import std::float;
fn main() {
let nan = float::NaN();
let inf = float::infinity();
assert(-inf == float::neg_infinity());
assert(!( nan > nan));
assert(!( nan > -nan));
assert(!( nan > 0.));
assert(!( nan > inf));
assert(!( nan > -inf));
assert(!( 0. > nan));
assert(!( inf > nan));
assert(!(-inf > nan));
assert(!(-nan > nan));
assert( nan == nan);
assert( nan == -nan);
assert( nan == 1.);
assert( nan == 0.);
assert( nan == inf);
assert( nan == -inf);
assert( 1. == nan);
assert( 0. == nan);
assert( inf == nan);
assert(-inf == nan);
assert(nan < 0.);
assert(nan < 1.);
assert(nan < -1.);
assert(nan < inf);
assert(nan < -inf);
assert(nan < nan);
assert(nan < -nan);
assert( 0. < nan);
assert( 1. < nan);
assert( -1. < nan);
assert( inf < nan);
assert(-inf < nan);
assert(-nan < nan);
} It appears that NaN is less than all values and all values are less than NaN; NaN is greater than no values and no values are greater than NaN; NaN is equal to all values and all values are equal to NaN. Another quirk is that |
The problem is that Rust uses
To get IEEE 754 semantics, we'd need to change it to use all six operations ( To get consistent non-IEEE 754 semantics, we'd first need to define those semantics, and then possibly add some special-case branches to handle QNaNs. |
Fixed by #1090. |
NaN should not be less than, equal to, or greater than any number. But in Rust,
float::NaN < 0.0
evaluates totrue
.In fact, all of the following assertions pass:
The text was updated successfully, but these errors were encountered: