8000 Implement min, minf, max, maxf by varkor · Pull Request #180 · rust-lang/libm · GitHub
[go: up one dir, main page]

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

Implement min, minf, max, maxf #180

Merged
merged 6 commits into :master from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add max
  • Loading branch information
varkor committed Jun 5, 2019
commit 98a45f4061b433bb6d0825d463e29b48371ab22a
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- minf
- min
- maxf
- max

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

Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ pub trait F64Ext: private::Sealed + Sized {
fn atanh(self) -> Self;

fn min(self, other: Self) -> Self;

fn max(self, other: Self) -> Self;
}

impl F64Ext for f64 {
Expand Down Expand Up @@ -622,6 +624,11 @@ impl F64Ext for f64 {
fn min(self, other: Self) -> Self {
min(self, other)
}

#[inline]
fn max(self, other: Self) -> Self {
max(self, other)
}
}

mod private {
Expand Down
13 changes: 13 additions & 0 deletions src/math/max.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#[inline]
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn max(x: f64, y: f64) -> f64 {
// 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 x.is_nan() || x < y { y } else { x }) * 1.0
}
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ mod trunc;
mod truncf;
mod min;
mod minf;
mod max;
mod maxf;

// Use separated imports instead of {}-grouped imports for easier merging.
Expand Down Expand Up @@ -277,6 +278,7 @@ pub use self::trunc::trunc;
pub use self::truncf::truncf;
pub use self::min::min;
pub use self::minf::minf;
pub use self::max::max;
pub use self::maxf::maxf;

// Private modules
Expand Down
0