@@ -2421,6 +2421,94 @@ mod tests {
24212421 check :: < f64 > ( 1e-12 ) ;
24222422 }
24232423
2424+ #[ test]
2425+ #[ cfg( any( feature = "std" , feature = "libm" ) ) ]
2426+ fn integer_decode_f32 ( ) {
2427+ use crate :: Float ;
2428+ use core:: f32;
2429+ use std:: format;
2430+
2431+ fn check ( x : f32 , x_mantissa : u64 , x_exponent : i16 , x_sign : i8 ) {
2432+ let ( mantissa, exponent, sign) = Float :: integer_decode ( x) ;
2433+ assert ! (
2434+ ( x_mantissa, x_exponent, x_sign) == ( mantissa, exponent, sign) ,
2435+ "{}\n \t {}\n \t {}" ,
2436+ format!( "while testing return value of Float::integer_decode({})" , x) ,
2437+ format!( "expected: ({:#x} {} {})" , x_mantissa, x_exponent, x_sign) ,
2438+ format!( " found: ({:#x} {} {})" , mantissa, exponent, sign) ,
2439+ ) ;
2440+
2441+ let sign_f = sign as f32 ;
2442+ let mantissa_f = mantissa as f32 ;
2443+ let exponent_f = exponent as f32 ;
2444+
2445+ let abs_difference = ( sign_f * mantissa_f * 2_f32 . powf ( exponent_f) - x) . abs ( ) ;
2446+
2447+ assert ! ( abs_difference < 1e-10 , "absolute difference {} must be less than 1e-10" , abs_difference) ;
2448+ }
2449+
2450+ for sign in [ 1 , -1 ] {
2451+ let sign_f = sign as f32 ;
2452+ check ( sign_f * 0.0_f32 , 0x000000 , -150 , sign) ;
2453+ check ( sign_f * f32:: MIN_POSITIVE , 0x800000 , -149 , sign) ;
2454+ check ( sign_f * 0.25_f32 , 0x800000 , -25 , sign) ;
2455+ check ( sign_f * 0.5_f32 , 0x800000 , -24 , sign) ;
2456+ check ( sign_f * 1_f32 , 0x800000 , -23 , sign) ;
2457+ check ( sign_f * 1.5_f32 , 0xc00000 , -23 , sign) ;
2458+ check ( sign_f * 2_f32 , 0x800000 , -22 , sign) ;
2459+ check ( sign_f * 2.5_f32 , 0xa00000 , -22 , sign) ;
2460+ check ( sign_f * 3_f32 , 0xc00000 , -22 , sign) ;
2461+ check ( sign_f * 4_f32 , 0x800000 , -21 , sign) ;
2462+ check ( sign_f * 5_f32 , 0xa00000 , -21 , sign) ;
2463+ check ( sign_f * 42_f32 , 0xa80000 , -18 , sign) ;
2464+ check ( sign_f * f32:: MAX , 0xffffff , 104 , sign) ;
2465+ }
2466+ }
2467+
2468+ #[ test]
2469+ #[ cfg( any( feature = "std" , feature = "libm" ) ) ]
2470+ fn integer_decode_f64 ( ) {
2471+ use crate :: Float ;
2472+ use core:: f64;
2473+ use std:: format;
2474+
2475+ fn check ( x : f64 , x_mantissa : u64 , x_exponent : i16 , x_sign : i8 ) {
2476+ let ( mantissa, exponent, sign) = Float :: integer_decode ( x) ;
2477+ assert ! (
2478+ ( x_mantissa, x_exponent, x_sign) == ( mantissa, exponent, sign) ,
2479+ "{}\n \t {}\n \t {}" ,
2480+ format!( "while testing return value of Float::integer_decode({})" , x) ,
2481+ format!( "expected: ({:#x} {} {})" , x_mantissa, x_exponent, x_sign) ,
2482+ format!( " found: ({:#x} {} {})" , mantissa, exponent, sign) ,
2483+ ) ;
2484+
2485+ let sign_f = sign as f64 ;
2486+ let mantissa_f = mantissa as f64 ;
2487+ let exponent_f = exponent as f64 ;
2488+
2489+ let abs_difference = ( sign_f * mantissa_f * 2_f64 . powf ( exponent_f) - x) . abs ( ) ;
2490+
2491+ assert ! ( abs_difference < 1e-10 , "absolute difference {} must be less than 1e-10" , abs_difference) ;
2492+ }
2493+
2494+ for sign in [ 1 , -1 ] {
2495+ let sign_f = sign as f64 ;
2496+ check ( sign_f * 0.0_f64 , 0x00000000000000 , -1075 , sign) ;
2497+ check ( sign_f * f64:: MIN_POSITIVE , 0x10000000000000 , -1074 , sign) ;
2498+ check ( sign_f * 0.25_f64 , 0x10000000000000 , -54 , sign) ;
2499+ check ( sign_f * 0.5_f64 , 0x10000000000000 , -53 , sign) ;
2500+ check ( sign_f * 1_f64 , 0x10000000000000 , -52 , sign) ;
2501+ check ( sign_f * 1.5_f64 , 0x18000000000000 , -52 , sign) ;
2502+ check ( sign_f * 2_f64 , 0x10000000000000 , -51 , sign) ;
2503+ check ( sign_f * 2.5_f64 , 0x14000000000000 , -51 , sign) ;
2504+ check ( sign_f * 3_f64 , 0x18000000000000 , -51 , sign) ;
2505+ check ( sign_f * 4_f64 , 0x10000000000000 , -50 , sign) ;
2506+ check ( sign_f * 5_f64 , 0x14000000000000 , -50 , sign) ;
2507+ check ( sign_f * 42_f64 , 0x15000000000000 , -47 , sign) ;
2508+ check ( sign_f * f64:: MAX , 0x1fffffffffffff , 971 , sign) ;
2509+ }
2510+ }
2511+
24242512 #[ test]
24252513 #[ cfg( any( feature = "std" , feature = "libm" ) ) ]
24262514 fn copysign ( ) {
0 commit comments