Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 7457338

Browse files
committed
Add max
1 parent bd6e10f commit 7457338

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010
- minf
1111
- min
1212
- maxf
13+
- max
1314

1415
## [v0.1.2] - 2018-07-18
1516

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ pub trait F64Ext: private::Sealed + Sized {
426426
fn atanh(self) -> Self;
427427

428428
fn min(self, other: Self) -> Self;
429+
430+
fn max(self, other: Self) -> Self;
429431
}
430432

431433
impl F64Ext for f64 {
@@ -622,6 +624,11 @@ impl F64Ext for f64 {
622624
fn min(self, other: Self) -> Self {
623625
min(self, other)
624626
}
627+
628+
#[inline]
629+
fn max(self, other: Self) -> Self {
630+
max(self, other)
631+
}
625632
}
626633

627634
mod private {

src/math/max.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[inline]
2+
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
3+
pub fn max(x: f64, y: f64) -> f64 {
4+
// IEEE754 says: maxNum(x, y) is the canonicalized number y if x < y, x if y < x, the
5+
// canonicalized number if one operand is a number and the other a quiet NaN. Otherwise it
6+
// is either x or y, canonicalized (this means results might differ among implementations).
7+
// When either x or y is a signalingNaN, then the result is according to 6.2.
8+
//
9+
// Since we do not support sNaN in Rust yet, we do not need to handle them.
10+
// FIXME(nagisa): due to https://bugs.llvm.org/show_bug.cgi?id=33303 we canonicalize by
11+
// multiplying by 1.0. Should switch to the `canonicalize` when it works.
12+
(if x.is_nan() || x < y { y } else { x }) * 1.0
13+
}

src/math/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ mod trunc;
168168
mod truncf;
169169
mod min;
170170
mod minf;
171+
mod max;
171172
mod maxf;
172173

173174
// Use separated imports instead of {}-grouped imports for easier merging.
@@ -277,6 +278,7 @@ pub use self::trunc::trunc;
277278
pub use self::truncf::truncf;
278279
pub use self::min::min;
279280
pub use self::minf::minf;
281+
pub use self::max::max;
280282
pub use self::maxf::maxf;
281283

282284
// Private modules

0 commit comments

Comments
 (0)