@@ -2397,6 +2397,94 @@ mod tests {
23972397 check :: < f64 > ( 1e-12 ) ;
23982398 }
23992399
2400+ #[ test]
2401+ #[ cfg( any( feature = "std" , feature = "libm" ) ) ]
2402+ fn integer_decode_f32 ( ) {
2403+ use crate :: Float ;
2404+ 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 ;
2420+
2421+ let abs_difference = ( sign_f * mantissa_f * 2_f32 . powf ( exponent_f) - x) . abs ( ) ;
2422+
2423+ assert ! ( abs_difference < 1e-10 , "absolute difference {} must be less than 1e-10" , abs_difference) ;
2424+ }
2425+
2426+ for sign in [ 1 , -1 ] {
2427+ let sign_f = sign as f32 ;
2428+ check ( sign_f * 0.0_f32 , 0x000000 , -150 , sign) ;
2429+ check ( sign_f * f32:: MIN_POSITIVE , 0x800000 , -149 , sign) ;
2430+ check ( sign_f * 0.25_f32 , 0x800000 , -25 , sign) ;
2431+ check ( sign_f * 0.5_f32 , 0x800000 , -24 , sign) ;
2432+ check ( sign_f * 1_f32 , 0x800000 , -23 , sign) ;
2433+ check ( sign_f * 1.5_f32 , 0xc00000 , -23 , sign) ;
2434+ check ( sign_f * 2_f32 , 0x800000 , -22 , sign) ;
2435+ check ( sign_f * 2.5_f32 , 0xa00000 , -22 , sign) ;
2436+ check ( sign_f * 3_f32 , 0xc00000 , -22 , sign) ;
2437+ check ( sign_f * 4_f32 , 0x800000 , -21 , sign) ;
2438+ check ( sign_f * 5_f32 , 0xa00000 , -21 , sign) ;
2439+ check ( sign_f * 42_f32 , 0xa80000 , -18 , sign) ;
2440+ check ( sign_f * f32:: MAX , 0xffffff , 104 , sign) ;
2441+ }
2442+ }
2443+
2444+ #[ test]
2445+ #[ cfg( any( feature = "std" , feature = "libm" ) ) ]
2446+ fn integer_decode_f64 ( ) {
2447+ use crate :: Float ;
2448+ 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 * 2_f64 . powf ( exponent_f) - x) . abs ( ) ;
2466+
2467+ assert ! ( abs_difference < 1e-10 , "absolute difference {} must be less than 1e-10" , abs_difference) ;
2468+ }
2469+
2470+ for sign in [ 1 , -1 ] {
2471+ let sign_f = sign as f64 ;
2472+ check ( sign_f * 0.0_f64 , 0x00000000000000 , -1075 , sign) ;
2473+ check ( sign_f * f64:: MIN_POSITIVE , 0x10000000000000 , -1074 , sign) ;
2474+ check ( sign_f * 0.25_f64 , 0x10000000000000 , -54 , sign) ;
2475+ check ( sign_f * 0.5_f64 , 0x10000000000000 , -53 , sign) ;
2476+ check ( sign_f * 1_f64 , 0x10000000000000 , -52 , sign) ;
2477+ check ( sign_f * 1.5_f64 , 0x18000000000000 , -52 , sign) ;
2478+ check ( sign_f * 2_f64 , 0x10000000000000 , -51 , sign) ;
2479+ check ( sign_f * 2.5_f64 , 0x14000000000000 , -51 , sign) ;
2480+ check ( sign_f * 3_f64 , 0x18000000000000 , -51 , sign) ;
2481+ check ( sign_f * 4_f64 , 0x10000000000000 , -50 , sign) ;
2482+ check ( sign_f * 5_f64 , 0x14000000000000 , -50 , sign) ;
2483+ check ( sign_f * 42_f64 , 0x15000000000000 , -47 , sign) ;
2484+ check ( sign_f * f64:: MAX , 0x1fffffffffffff , 971 , sign) ;
2485+ }
2486+ }
2487+
24002488 #[ test]
24012489 #[ cfg( any( feature = "std" , feature = "libm" ) ) ]
24022490 fn copysign ( ) {
0 commit comments