Skip to content

Commit

Permalink
Merge branch 'mtilda/test/float-integer-decode' into mtilda/enhance/f…
Browse files Browse the repository at this point in the history
…loat-integer-decode
  • Loading branch information
mtilda committed Jun 27, 2024
2 parents bf6372b + 19a57e7 commit 141f91e
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2421,6 +2421,94 @@ mod tests {
check::<f64>(1e-12);
}

#[test]
#[cfg(any(feature = "std", feature = "libm"))]
fn integer_decode_f32() {
use crate::Float;
use core::f32;
use std::format;

fn check(x: f32, x_mantissa: u64, x_exponent: i16, x_sign: i8) {
let (mantissa, exponent, sign) = Float::integer_decode(x);
assert!(
(x_mantissa, x_exponent, x_sign) == (mantissa, exponent, sign),
"{}\n\t{}\n\t{}",
format!("while testing return value of Float::integer_decode({})", x),
format!("expected: ({:#x} {} {})", x_mantissa, x_exponent, x_sign),
format!(" found: ({:#x} {} {})", mantissa, exponent, sign),
);

let sign_f = sign as f32;
let mantissa_f = mantissa as f32;
let exponent_f = exponent as f32;

let abs_difference = (sign_f * mantissa_f * 2_f32.powf(exponent_f) - x).abs();

assert!(abs_difference < 1e-10, "absolute difference {} must be less than 1e-10", abs_difference);
}

for sign in [1, -1] {
let sign_f = sign as f32;
check(sign_f * 0.0_f32, 0x000000, -150, sign);
check(sign_f * f32::MIN_POSITIVE, 0x800000, -149, sign);
check(sign_f * 0.25_f32, 0x800000, -25, sign);
check(sign_f * 0.5_f32, 0x800000, -24, sign);
check(sign_f * 1_f32, 0x800000, -23, sign);
check(sign_f * 1.5_f32, 0xc00000, -23, sign);
check(sign_f * 2_f32, 0x800000, -22, sign);
check(sign_f * 2.5_f32, 0xa00000, -22, sign);
check(sign_f * 3_f32, 0xc00000, -22, sign);
check(sign_f * 4_f32, 0x800000, -21, sign);
check(sign_f * 5_f32, 0xa00000, -21, sign);
check(sign_f * 42_f32, 0xa80000, -18, sign);
check(sign_f * f32::MAX, 0xffffff, 104, sign);
}
}

#[test]
#[cfg(any(feature = "std", feature = "libm"))]
fn integer_decode_f64() {
use crate::Float;
use core::f64;
use std::format;

fn check(x: f64, x_mantissa: u64, x_exponent: i16, x_sign: i8) {
let (mantissa, exponent, sign) = Float::integer_decode(x);
assert!(
(x_mantissa, x_exponent, x_sign) == (mantissa, exponent, sign),
"{}\n\t{}\n\t{}",
format!("while testing return value of Float::integer_decode({})", x),
format!("expected: ({:#x} {} {})", x_mantissa, x_exponent, x_sign),
format!(" found: ({:#x} {} {})", mantissa, exponent, sign),
);

let sign_f = sign as f64;
let mantissa_f = mantissa as f64;
let exponent_f = exponent as f64;

let abs_difference = (sign_f * mantissa_f * 2_f64.powf(exponent_f) - x).abs();

assert!(abs_difference < 1e-10, "absolute difference {} must be less than 1e-10", abs_difference);
}

for sign in [1, -1] {
let sign_f = sign as f64;
check(sign_f * 0.0_f64, 0x00000000000000, -1075, sign);
check(sign_f * f64::MIN_POSITIVE, 0x10000000000000, -1074, sign);
check(sign_f * 0.25_f64, 0x10000000000000, -54, sign);
check(sign_f * 0.5_f64, 0x10000000000000, -53, sign);
check(sign_f * 1_f64, 0x10000000000000, -52, sign);
check(sign_f * 1.5_f64, 0x18000000000000, -52, sign);
check(sign_f * 2_f64, 0x10000000000000, -51, sign);
check(sign_f * 2.5_f64, 0x14000000000000, -51, sign);
check(sign_f * 3_f64, 0x18000000000000, -51, sign);
check(sign_f * 4_f64, 0x10000000000000, -50, sign);
check(sign_f * 5_f64, 0x14000000000000, -50, sign);
check(sign_f * 42_f64, 0x15000000000000, -47, sign);
check(sign_f * f64::MAX, 0x1fffffffffffff, 971, sign);
}
}

#[test]
#[cfg(any(feature = "std", feature = "libm"))]
fn copysign() {
Expand Down

0 comments on commit 141f91e

Please sign in to comment.