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

Commit 643fcc3

Browse files
varkoralexcrichton
authored andcommitted
Add maxf
1 parent e9eb8a2 commit 643fcc3

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
@@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99

1010
- minf
1111
- min
12+
- maxf
1213

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

src/lib.rs

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

140140
fn min(self, other: Self) -> Self;
141+
142+
fn max(self, other: Self) -> Self;
141143
}
142144

143145
impl F32Ext for f32 {
@@ -334,6 +336,11 @@ impl F32Ext for f32 {
334336
fn min(self, other: Self) -> Self {
335337
minf(self, other)
336338
}
339+
340+
#[inline]
341+
fn max(self, other: Self) -> Self {
342+
maxf(self, other)
343+
}
337344
}
338345

339346
/// Math support for `f64`

src/math/maxf.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 maxf(x: f32, y: f32) -> f32 {
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 maxf;
171172

172173
// Use separated imports instead of {}-grouped imports for easier merging.
173174
pub use self::acos::acos;
@@ -276,6 +277,7 @@ pub use self::trunc::trunc;
276277
pub use self::truncf::truncf;
277278
pub use self::min::min;
278279
pub use self::minf::minf;
280+
pub use self::maxf::maxf;
279281

280282
// Private modules
281283
mod expo2;

0 commit comments

Comments
 (0)