-
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
Re-implement float min/max in rust #42430
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -242,4 +242,32 @@ impl Float for f32 { | |
let value: f32 = consts::PI; | ||
self * (value / 180.0f32) | ||
} | ||
|
||
/// Returns the maximum of the two numbers. | ||
#[inline] | ||
fn max(self, other: f32) -> f32 { | ||
// IEEE754 says: maxNum(x, y) is the canonicalized number y if x < y, x if y < x, the | ||
// canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it | ||
// is either x or y, canonicalized (this means results might differ among implementations). | ||
// When either x or y is a signalingNaN, then the result is according to 6.2. | ||
// | ||
// Since we do not support sNaN in Rust yet, we do not need to handle them. | ||
// FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by | ||
// multiplying by 1.0. Should switch to the `canonicalize` when it works. | ||
(if self < other || self.is_nan() { other } else { self }) * 1.0 | ||
} | ||
|
||
/// Returns the minimum of the two numbers. | ||
#[inline] | ||
fn min(self, other: f32) -> f32 { | ||
// IEEE754 says: minNum(x, y) is the canonicalized number x if x < y, y if y < x, the | ||
// canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it | ||
// is either x or y, canonicalized (this means results might differ among implementations). | ||
// When either x or y is a signalingNaN, then the result is according to 6.2. | ||
// | ||
// Since we do not support sNaN in Rust yet, we do not need to handle them. | ||
// FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by | ||
// multiplying by 1.0. Should switch to the `canonicalize` when it works. | ||
(if self < other || other.is_nan() { self } else { other }) * 1.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other one is/was wrong. I’ll just write down some tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} |
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 the musl implementation is quite different, it's not clear to me that these are the same?
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.
The only difference between the implementations is handling of the signed zero. IEEE 754 allows to ignore the signed-ness of zero here and, if
0.0
is compared to-0.0
, to return either one.Even the C99 standard does not mention signed zeroes in the annex referenced by musl. It quite literally paraphrases the IEEE 754 and provides an example implementation which matches (with small differences) the one provided here.
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.
Ok seems reasonable!