Skip to content

Commit

Permalink
Add lossy conversion for NotNan<f64> to NotNan<f32> (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
MultisampledNight authored Apr 30, 2022
1 parent 158a3ae commit 61975f1
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,18 @@ impl<T: Float + fmt::Display> fmt::Display for NotNan<T> {
}
}

impl NotNan<f64> {
/// Converts this [`NotNan`]`<`[`f64`]`>` to a [`NotNan`]`<`[`f32`]`>` while giving up on
/// precision, [using `roundTiesToEven` as rounding mode, yielding `Infinity` on
/// overflow](https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics).
pub fn as_f32(self) -> NotNan<f32> {
// This is not destroying invariants, as it is a pure rounding operation. The only two special
// cases are where f32 would be overflowing, then the operation yields Infinity, or where
// the input is already NaN, in which case the invariant is already broken elsewhere.
NotNan(self.0 as f32)
}
}

impl From<NotNan<f32>> for f32 {
#[inline]
fn from(value: NotNan<f32>) -> Self {
Expand Down

0 comments on commit 61975f1

Please sign in to comment.