@@ -2397,31 +2397,39 @@ mod tests {
23972397 check :: < f64 > ( 1e-12 ) ;
23982398 }
23992399
2400+ macro_rules! check_integer_decode {
2401+ (
2402+ $func_name: ident,
2403+ $T: ty,
2404+ $tolerance: expr
2405+ ) => {
2406+ fn $func_name( x: $T, x_mantissa: u64 , x_exponent: i16 , x_sign: i8 ) {
2407+ let ( mantissa, exponent, sign) = crate :: float:: Float :: integer_decode( x) ;
2408+ assert!(
2409+ ( x_mantissa, x_exponent, x_sign) == ( mantissa, exponent, sign) ,
2410+ "{}\n \t {}\n \t {}" ,
2411+ std:: format!( "while testing return value of Float::integer_decode({})" , x) ,
2412+ std:: format!( "expected: ({:#x} {} {})" , x_mantissa, x_exponent, x_sign) ,
2413+ std:: format!( " found: ({:#x} {} {})" , mantissa, exponent, sign) ,
2414+ ) ;
2415+
2416+ let sign_f = sign as $T;
2417+ let mantissa_f = mantissa as $T;
2418+ let exponent_f = exponent as $T;
2419+
2420+ let abs_difference = ( sign_f * mantissa_f * exponent_f. exp2( ) - x) . abs( ) ;
2421+
2422+ assert!( abs_difference < $tolerance, "absolute difference {} must be less than {}" , abs_difference, $tolerance) ;
2423+ }
2424+ }
2425+ }
2426+
24002427 #[ test]
24012428 #[ cfg( any( feature = "std" , feature = "libm" ) ) ]
24022429 fn integer_decode_f32 ( ) {
2403- use crate :: Float ;
24042430 use core:: f32;
2405- use std:: format;
2406-
2407- fn check ( x : f32 , x_mantissa : u64 , x_exponent : i16 , x_sign : i8 ) {
2408- let ( mantissa, exponent, sign) = Float :: integer_decode ( x) ;
2409- assert ! (
2410- ( x_mantissa, x_exponent, x_sign) == ( mantissa, exponent, sign) ,
2411- "{}\n \t {}\n \t {}" ,
2412- format!( "while testing return value of Float::integer_decode({})" , x) ,
2413- format!( "expected: ({:#x} {} {})" , x_mantissa, x_exponent, x_sign) ,
2414- format!( " found: ({:#x} {} {})" , mantissa, exponent, sign) ,
2415- ) ;
2416-
2417- let sign_f = sign as f32 ;
2418- let mantissa_f = mantissa as f32 ;
2419- let exponent_f = exponent as f32 ;
24202431
2421- let abs_difference = ( sign_f * mantissa_f * exponent_f. exp2 ( ) - x) . abs ( ) ;
2422-
2423- assert ! ( abs_difference < 1e-10 , "absolute difference {} must be less than 1e-10" , abs_difference) ;
2424- }
2432+ check_integer_decode ! ( check, f32 , 1e-6 ) ;
24252433
24262434 for sign in [ 1 , -1 ] {
24272435 let sign_f = sign as f32 ;
@@ -2444,28 +2452,9 @@ mod tests {
24442452 #[ test]
24452453 #[ cfg( any( feature = "std" , feature = "libm" ) ) ]
24462454 fn integer_decode_f64 ( ) {
2447- use crate :: Float ;
24482455 use core:: f64;
2449- use std:: format;
2450-
2451- fn check ( x : f64 , x_mantissa : u64 , x_exponent : i16 , x_sign : i8 ) {
2452- let ( mantissa, exponent, sign) = Float :: integer_decode ( x) ;
2453- assert ! (
2454- ( x_mantissa, x_exponent, x_sign) == ( mantissa, exponent, sign) ,
2455- "{}\n \t {}\n \t {}" ,
2456- format!( "while testing return value of Float::integer_decode({})" , x) ,
2457- format!( "expected: ({:#x} {} {})" , x_mantissa, x_exponent, x_sign) ,
2458- format!( " found: ({:#x} {} {})" , mantissa, exponent, sign) ,
2459- ) ;
2460-
2461- let sign_f = sign as f64 ;
2462- let mantissa_f = mantissa as f64 ;
2463- let exponent_f = exponent as f64 ;
2464-
2465- let abs_difference = ( sign_f * mantissa_f * exponent_f. exp2 ( ) - x) . abs ( ) ;
24662456
2467- assert ! ( abs_difference < 1e-10 , "absolute difference {} must be less than 1e-10" , abs_difference) ;
2468- }
2457+ check_integer_decode ! ( check, f64 , 1e-6 ) ;
24692458
24702459 for sign in [ 1 , -1 ] {
24712460 let sign_f = sign as f64 ;
0 commit comments