Skip to content
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

Add lossy conversion for NotNan<f64> to NotNan<f32> #109

Merged
merged 2 commits into from Apr 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/lib.rs
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