Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #394 from dtolnay/numbernan
Browse files Browse the repository at this point in the history
Deterministically destroy sign of NaN when converted to Number
  • Loading branch information
dtolnay committed Oct 26, 2023
2 parents 1e2a89c + 1cda03f commit 23069f2
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,22 +517,24 @@ macro_rules! from_unsigned {
};
}

macro_rules! from_float {
($($float_ty:ident)*) => {
$(
impl From<$float_ty> for Number {
#[inline]
fn from(f: $float_ty) -> Self {
Number { n: N::Float(f as f64) }
}
}
)*
from_signed!(i8 i16 i32 i64 isize);
from_unsigned!(u8 u16 u32 u64 usize);

impl From<f32> for Number {
fn from(f: f32) -> Self {
Number::from(f as f64)
}
}

from_signed!(i8 i16 i32 i64 isize);
from_unsigned!(u8 u16 u32 u64 usize);
from_float!(f32 f64);
impl From<f64> for Number {
fn from(mut f: f64) -> Self {
if f.is_nan() {
// Destroy NaN sign, signaling, and payload. YAML only has one NaN.
f = f64::NAN.copysign(1.0);
}
Number { n: N::Float(f) }
}
}

// This is fine, because we don't _really_ implement hash for floats
// all other hash functions should work as expected
Expand Down

0 comments on commit 23069f2

Please sign in to comment.