@@ -2397,108 +2397,100 @@ mod tests {
2397
2397
check :: < f64 > ( 1e-12 ) ;
2398
2398
}
2399
2399
2400
- /// Create a function to test `Float::integer_decode` for a given float type (`f32` or `f64`) and tolerance.
2401
- ///
2402
- /// # Examples
2403
- /// ```rs
2404
- /// check_integer_decode!(check, f32, 1e-6);
2405
- /// check(sign_f * 0.0_f32, 0x000000, -150, sign);
2406
- /// ```
2407
- macro_rules! check_integer_decode {
2408
- (
2409
- $func_name: ident,
2410
- $float_type: ty,
2411
- $tolerance: expr
2412
- ) => {
2413
- /// Test the behavior of `Float::integer_decode(input)`
2414
- fn $func_name( input: $float_type, mantissa: u64 , exponent: i16 , sign: i8 ) {
2415
- let output = crate :: float:: Float :: integer_decode( input) ;
2416
-
2417
- assert!(
2418
- ( mantissa, exponent, sign) == output,
2419
- "unexpected output of `Float::integer_decode({0:e})`
2420
- \t expected: {1}
2421
- \t found: {2}",
2422
- input,
2423
- std:: format!( "({:#x} {} {})" , mantissa, exponent, sign) ,
2424
- std:: format!( "({:#x} {} {})" , output. 0 , output. 1 , output. 2 ) ,
2425
- ) ;
2426
-
2427
- // Destructure output and values cast as float
2428
- let mantissa_f = output. 0 as $float_type;
2429
- let exponent_f = output. 1 as $float_type;
2430
- let sign_f = output. 2 as $float_type;
2431
-
2432
- // Recover input using equation: sign * mantissa * 2^exponent
2433
- let recovered_input = sign_f * mantissa_f * exponent_f. exp2( ) ;
2434
- let deviation = recovered_input - input;
2435
-
2436
- assert!(
2437
- recovered_input == input || deviation. abs( ) < $tolerance,
2438
- "failed to recover input of `Float::integer_decode({0:e})`
2439
- \t expected: ~ {:e}
2440
- \t found: {:e}
2441
- \t deviation: {:+e}
2442
- \t tolerance: < {:e}",
2443
- input,
2444
- recovered_input,
2445
- deviation,
2446
- $tolerance
2447
- ) ;
2448
- }
2449
- } ;
2400
+ /// Test the behavior of `Float::integer_decode` with the `given` input and `expected` output values.
2401
+ fn test_integer_decode < T > ( given : T , expected : ( u64 , i16 , i8 ) )
2402
+ where
2403
+ T : crate :: float:: Float + core:: fmt:: LowerExp + core:: fmt:: Debug ,
2404
+ {
2405
+ use crate :: float:: Float ;
2406
+
2407
+ let found = Float :: integer_decode ( given) ;
2408
+
2409
+ assert ! (
2410
+ expected == found,
2411
+ "unexpected output of `Float::integer_decode({0:e})`
2412
+ \t expected: ({1:#x} {2} {3})
2413
+ \t found: {4:#x} {5} {6}",
2414
+ given,
2415
+ expected. 0 ,
2416
+ expected. 1 ,
2417
+ expected. 2 ,
2418
+ found. 0 ,
2419
+ found. 1 ,
2420
+ found. 2
2421
+ ) ;
2422
+
2423
+ // Destructure the `found` output and cast values as float.
2424
+ let mantissa_f = T :: from ( found. 0 ) . unwrap ( ) ;
2425
+ let exponent_f = T :: from ( found. 1 ) . unwrap ( ) ;
2426
+ let sign_f = T :: from ( found. 2 ) . unwrap ( ) ;
2427
+
2428
+ // Recover the `given` input using equation: sign * mantissa * 2^exponent.
2429
+ let recovered = sign_f * mantissa_f * exponent_f. exp2 ( ) ;
2430
+ let deviation = recovered - given;
2431
+ let tolerance = T :: from ( 1e-6 ) . unwrap ( ) ;
2432
+
2433
+ assert_eq ! ( T :: one( ) , tolerance. signum( ) , "tolerance must be positive" ) ;
2434
+ assert ! (
2435
+ recovered == given || deviation. abs( ) < tolerance,
2436
+ "absolute deviation must not exceed tolerance`
2437
+ \t given: {:+e}
2438
+ \t recovered: {:+e}
2439
+ \t deviation: {:+e}
2440
+ \t tolerance: <{:e}",
2441
+ given,
2442
+ recovered,
2443
+ deviation,
2444
+ tolerance
2445
+ ) ;
2450
2446
}
2451
2447
2452
2448
#[ test]
2453
2449
#[ cfg( any( feature = "std" , feature = "libm" ) ) ]
2454
2450
fn integer_decode_f32 ( ) {
2455
- check_integer_decode ! ( check, f32 , 1e-6 ) ;
2456
-
2457
2451
for sign in [ 1 , -1 ] {
2458
2452
let sign_f = sign as f32 ;
2459
- check ( sign_f * 0.0__f32 , 0x000000 , -150 , sign) ;
2460
- check ( sign_f * f32:: MIN_POSITIVE , 0x800000 , -149 , sign) ;
2461
- check ( sign_f * 0.25_f32 , 0x800000 , -25 , sign) ;
2462
- check ( sign_f * 0.5__f32 , 0x800000 , -24 , sign) ;
2463
- check ( sign_f * 1____f32 , 0x800000 , -23 , sign) ;
2464
- check ( sign_f * 1.5__f32 , 0xc00000 , -23 , sign) ;
2465
- check ( sign_f * 2____f32 , 0x800000 , -22 , sign) ;
2466
- check ( sign_f * 2.5__f32 , 0xa00000 , -22 , sign) ;
2467
- check ( sign_f * 3____f32 , 0xc00000 , -22 , sign) ;
2468
- check ( sign_f * 4____f32 , 0x800000 , -21 , sign) ;
2469
- check ( sign_f * 5____f32 , 0xa00000 , -21 , sign) ;
2470
- check ( sign_f * 42___f32 , 0xa80000 , -18 , sign) ;
2471
- check ( sign_f * f32:: MAX , 0xffffff , 104 , sign) ;
2472
- check ( sign_f * f32:: INFINITY , 0x800000 , 105 , sign) ;
2453
+ test_integer_decode ( sign_f * 0.0__f32 , ( 0x000000 , -150 , sign) ) ;
2454
+ test_integer_decode ( sign_f * f32:: MIN_POSITIVE , ( 0x800000 , -149 , sign) ) ;
2455
+ test_integer_decode ( sign_f * 0.25_f32 , ( 0x800000 , -25 , sign) ) ;
2456
+ test_integer_decode ( sign_f * 0.5__f32 , ( 0x800000 , -24 , sign) ) ;
2457
+ test_integer_decode ( sign_f * 1____f32 , ( 0x800000 , -23 , sign) ) ;
2458
+ test_integer_decode ( sign_f * 1.5__f32 , ( 0xc00000 , -23 , sign) ) ;
2459
+ test_integer_decode ( sign_f * 2____f32 , ( 0x800000 , -22 , sign) ) ;
2460
+ test_integer_decode ( sign_f * 2.5__f32 , ( 0xa00000 , -22 , sign) ) ;
2461
+ test_integer_decode ( sign_f * 3____f32 , ( 0xc00000 , -22 , sign) ) ;
2462
+ test_integer_decode ( sign_f * 4____f32 , ( 0x800000 , -21 , sign) ) ;
2463
+ test_integer_decode ( sign_f * 5____f32 , ( 0xa00000 , -21 , sign) ) ;
2464
+ test_integer_decode ( sign_f * 42___f32 , ( 0xa80000 , -18 , sign) ) ;
2465
+ test_integer_decode ( sign_f * f32:: MAX , ( 0xffffff , 104 , sign) ) ;
2466
+ test_integer_decode ( sign_f * f32:: INFINITY , ( 0x800000 , 105 , sign) ) ;
2473
2467
}
2474
2468
// FIXME: Unclear if we should be able to recover NaN inputs
2475
- // check (f32::NAN, 0xc00000, 105, 1);
2469
+ // check_integer_decode (f32::NAN, ( 0xc00000, 105, 1) );
2476
2470
}
2477
2471
2478
2472
#[ test]
2479
2473
#[ cfg( any( feature = "std" , feature = "libm" ) ) ]
2480
2474
fn integer_decode_f64 ( ) {
2481
- check_integer_decode ! ( check, f64 , 1e-6 ) ;
2482
-
2483
2475
for sign in [ 1 , -1 ] {
2484
2476
let sign_f = sign as f64 ;
2485
- check ( sign_f * 0.0__f64 , 0x00000000000000 , -1075 , sign) ;
2486
- check ( sign_f * f64:: MIN_POSITIVE , 0x10000000000000 , -1074 , sign) ;
2487
- check ( sign_f * 0.25_f64 , 0x10000000000000 , -54 , sign) ;
2488
- check ( sign_f * 0.5__f64 , 0x10000000000000 , -53 , sign) ;
2489
- check ( sign_f * 1____f64 , 0x10000000000000 , -52 , sign) ;
2490
- check ( sign_f * 1.5__f64 , 0x18000000000000 , -52 , sign) ;
2491
- check ( sign_f * 2____f64 , 0x10000000000000 , -51 , sign) ;
2492
- check ( sign_f * 2.5__f64 , 0x14000000000000 , -51 , sign) ;
2493
- check ( sign_f * 3____f64 , 0x18000000000000 , -51 , sign) ;
2494
- check ( sign_f * 4____f64 , 0x10000000000000 , -50 , sign) ;
2495
- check ( sign_f * 5____f64 , 0x14000000000000 , -50 , sign) ;
2496
- check ( sign_f * 42___f64 , 0x15000000000000 , -47 , sign) ;
2497
- check ( sign_f * f64:: MAX , 0x1fffffffffffff , 971 , sign) ;
2498
- check ( sign_f * f64:: INFINITY , 0x10000000000000 , 972 , sign) ;
2477
+ test_integer_decode ( sign_f * 0.0__f64 , ( 0x00000000000000 , -1075 , sign) ) ;
2478
+ test_integer_decode ( sign_f * f64:: MIN_POSITIVE , ( 0x10000000000000 , -1074 , sign) ) ;
2479
+ test_integer_decode ( sign_f * 0.25_f64 , ( 0x10000000000000 , -54 , sign) ) ;
2480
+ test_integer_decode ( sign_f * 0.5__f64 , ( 0x10000000000000 , -53 , sign) ) ;
2481
+ test_integer_decode ( sign_f * 1____f64 , ( 0x10000000000000 , -52 , sign) ) ;
2482
+ test_integer_decode ( sign_f * 1.5__f64 , ( 0x18000000000000 , -52 , sign) ) ;
2483
+ test_integer_decode ( sign_f * 2____f64 , ( 0x10000000000000 , -51 , sign) ) ;
2484
+ test_integer_decode ( sign_f * 2.5__f64 , ( 0x14000000000000 , -51 , sign) ) ;
2485
+ test_integer_decode ( sign_f * 3____f64 , ( 0x18000000000000 , -51 , sign) ) ;
2486
+ test_integer_decode ( sign_f * 4____f64 , ( 0x10000000000000 , -50 , sign) ) ;
2487
+ test_integer_decode ( sign_f * 5____f64 , ( 0x14000000000000 , -50 , sign) ) ;
2488
+ test_integer_decode ( sign_f * 42___f64 , ( 0x15000000000000 , -47 , sign) ) ;
2489
+ test_integer_decode ( sign_f * f64:: MAX , ( 0x1fffffffffffff , 971 , sign) ) ;
2490
+ test_integer_decode ( sign_f * f64:: INFINITY , ( 0x10000000000000 , 972 , sign) ) ;
2499
2491
}
2500
2492
// FIXME: Unclear if we should be able to recover NaN inputs
2501
- // check (f64::NAN, 0x18000000000000, 972, 1);
2493
+ // check_integer_decode (f64::NAN, ( 0x18000000000000, 972, 1) );
2502
2494
}
2503
2495
2504
2496
#[ test]
0 commit comments