@@ -409,24 +409,14 @@ pub fn pow(x: f64, y: f64) -> f64 {
409
409
return s * z;
410
410
}
411
411
412
- /// Special cases:
413
-
414
- /// 20. (anything) ** 1 is (anything)
415
- /// 21. (anything) ** -1 is 1/(anything)
416
- /// 22. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
417
- /// 23. (-anything except 0 and inf) ** (non-integer) is NAN
418
-
419
412
#[ cfg( test) ]
420
413
mod tests {
421
- // #[macro_use]
422
414
extern crate std;
423
415
424
416
use self :: std:: f64:: consts:: { E , PI } ;
425
417
use self :: std:: f64:: { EPSILON , INFINITY , MAX , MIN , MIN_POSITIVE , NAN , NEG_INFINITY } ;
426
418
use super :: pow;
427
419
428
- // const TESTCASES: &[f64] = &[1.0, 0.0, PI, -PI, E, -E, MIN, MAX, MIN_POSITIVE, NAN, INFINITY, NEG_INFINITY];
429
-
430
420
const POS_ZERO : & [ f64 ] = & [ 0.0 ] ;
431
421
const NEG_ZERO : & [ f64 ] = & [ -0.0 ] ;
432
422
const POS_ONE : & [ f64 ] = & [ 1.0 ] ;
@@ -440,21 +430,43 @@ mod tests {
440
430
const POS_ODDS : & [ f64 ] = & [ 3.0 , 7.0 ] ;
441
431
const NEG_ODDS : & [ f64 ] = & [ -7.0 , -3.0 ] ;
442
432
const NANS : & [ f64 ] = & [ NAN ] ;
443
- // const EDGES: &[f64] = &[MIN, MAX, MIN_POSITIVE, EPSILON];
444
433
const POS_INF : & [ f64 ] = & [ INFINITY ] ;
445
434
const NEG_INF : & [ f64 ] = & [ NEG_INFINITY ] ;
446
435
447
436
const ALL : & [ & [ f64 ] ] = & [
448
- POS_ZERO , NEG_ZERO , NANS , NEG_SMALL_FLOATS , POS_SMALL_FLOATS , NEG_FLOATS , POS_FLOATS , NEG_EVENS , POS_EVENS , NEG_ODDS , POS_ODDS ,
449
- NEG_INF , POS_INF , NEG_ONE , POS_ONE ,
437
+ POS_ZERO ,
438
+ NEG_ZERO ,
439
+ NANS ,
440
+ NEG_SMALL_FLOATS ,
441
+ POS_SMALL_FLOATS ,
442
+ NEG_FLOATS ,
443
+ POS_FLOATS ,
444
+ NEG_EVENS ,
445
+ POS_EVENS ,
446
+ NEG_ODDS ,
447
+ POS_ODDS ,
448
+ NEG_INF ,
449
+ POS_INF ,
450
+ NEG_ONE ,
451
+ POS_ONE ,
450
452
] ;
451
453
const POS : & [ & [ f64 ] ] = & [ POS_ZERO , POS_ODDS , POS_ONE , POS_FLOATS , POS_EVENS , POS_INF ] ;
452
454
const NEG : & [ & [ f64 ] ] = & [ NEG_ZERO , NEG_ODDS , NEG_ONE , NEG_FLOATS , NEG_EVENS , NEG_INF ] ;
453
455
454
456
fn pow_test ( base : f64 , exponent : f64 , expected : f64 ) {
455
457
let res = pow ( base, exponent) ;
456
- assert ! ( if expected. is_nan( ) { res. is_nan( ) } else { pow( base, exponent) == expected} ,
457
- "{} ** {} was {} instead of {}" , base, exponent, res, expected) ;
458
+ assert ! (
459
+ if expected. is_nan( ) {
460
+ res. is_nan( )
461
+ } else {
462
+ pow( base, exponent) == expected
463
+ } ,
464
+ "{} ** {} was {} instead of {}" ,
465
+ base,
466
+ exponent,
467
+ res,
468
+ expected
469
+ ) ;
458
470
}
459
471
460
472
fn test_sets_as_base ( sets : & [ & [ f64 ] ] , exponent : f64 , expected : f64 ) {
@@ -468,31 +480,37 @@ mod tests {
468
480
}
469
481
470
482
fn test_sets ( sets : & [ & [ f64 ] ] , computed : & Fn ( f64 ) -> f64 , expected : & Fn ( f64 ) -> f64 ) {
471
- sets. iter ( )
472
- . for_each ( |s| s. iter ( ) . for_each ( |val| {
483
+ sets. iter ( ) . for_each ( |s| {
484
+ s. iter ( ) . for_each ( |val| {
473
485
let exp = expected ( * val) ;
474
486
let res = computed ( * val) ;
475
487
476
- assert ! ( if exp. is_nan( ) { res. is_nan( ) } else { exp == res} ,
477
- "test for {} was {} instead of {}" , val, res, exp) ;
478
- } ) ) ;
488
+ assert ! (
489
+ if exp. is_nan( ) {
490
+ res. is_nan( )
491
+ } else {
492
+ exp == res
493
+ } ,
494
+ "test for {} was {} instead of {}" ,
495
+ val,
496
+ res,
497
+ exp
498
+ ) ;
499
+ } )
500
+ } ) ;
479
501
}
480
502
481
- /// 1. (anything) ** 0 is 1
482
503
#[ test]
483
504
fn zero_as_exponent ( ) {
484
505
test_sets_as_base ( ALL , 0.0 , 1.0 ) ;
485
506
test_sets_as_base ( ALL , -0.0 , 1.0 ) ;
486
507
}
487
508
488
- /// 2. 1 ** (anything) is 1
489
509
#[ test]
490
510
fn one_as_base ( ) {
491
511
test_sets_as_exponent ( 1.0 , ALL , 1.0 ) ;
492
512
}
493
513
494
- /// 3. (anything except 1) ** NAN is NAN
495
- /// 4. NAN ** (anything except 0) is NAN
496
514
#[ test]
497
515
fn nan_inputs ( ) {
498
516
// NAN as the base:
@@ -504,10 +522,6 @@ mod tests {
504
522
test_sets_as_base ( & ALL [ ..( ALL . len ( ) - 2 ) ] , NAN , NAN ) ;
505
523
}
506
524
507
- /// 16. +INF ** (+anything except 0,NAN) is +INF
508
- /// 17. +INF ** (-anything except 0,NAN) is +0
509
- /// 18. -INF ** (+odd integer) is -INF
510
- /// 19. -INF ** (anything) = -0 ** (-anything), (anything except odd integer)
511
525
#[ test]
512
526
fn infinity_as_base ( ) {
513
527
// Positive Infinity as the base:
@@ -527,19 +541,14 @@ mod tests {
527
541
test_sets ( ALL , & |v : f64 | pow ( NEG_INFINITY , v) , & |v : f64 | pow ( -0.0 , -v) ) ;
528
542
}
529
543
530
- /// 5. +-(|x| > 1) ** +INF is +INF
531
- /// 6. +-(|x| > 1) ** -INF is +0
532
- /// 7. +-(|x| < 1) ** +INF is +0
533
- /// 8. +-(|x| < 1) ** -INF is +INF
534
- /// 9. -1 ** +-INF is 1
535
544
#[ test]
536
545
fn infinity_as_exponent ( ) {
537
546
// Positive/Negative base greater than 1:
538
547
// (pos/neg > 1 ^ Infinity should be Infinity - note this excludes NAN as the base)
539
548
test_sets_as_base ( & ALL [ 5 ..( ALL . len ( ) - 2 ) ] , INFINITY , INFINITY ) ;
540
549
541
550
// (pos/neg > 1 ^ -Infinity should be 0.0)
542
- test_sets_as_base ( & ALL [ 5 ..( ALL . len ( ) - 2 ) ] , NEG_INFINITY , 0.0 ) ;
551
+ test_sets_as_base ( & ALL [ 5 ..ALL . len ( ) - 2 ] , NEG_INFINITY , 0.0 ) ;
543
552
544
553
// Positive/Negative base less than 1:
545
554
let base_below_one = & [ POS_ZERO , NEG_ZERO , NEG_SMALL_FLOATS , POS_SMALL_FLOATS ] ;
@@ -558,12 +567,6 @@ mod tests {
558
567
test_sets_as_base ( & [ NEG_ONE , POS_ONE ] , NEG_INFINITY , 1.0 ) ;
559
568
}
560
569
561
- /// 10. +0 ** (+anything except 0, NAN) is +0
562
- /// 11. -0 ** (+anything except 0, NAN, odd integer) is +0
563
- /// 12. +0 ** (-anything except 0, NAN) is +INF, raise divbyzero
564
- /// 13. -0 ** (-anything except 0, NAN, odd integer) is +INF, raise divbyzero
565
- /// 14. -0 ** (+odd integer) is -0
566
- /// 15. -0 ** (-odd integer) is -INF, raise divbyzero
567
570
#[ test]
568
571
fn zero_as_base ( ) {
569
572
// Positive Zero as the base:
@@ -576,7 +579,7 @@ mod tests {
576
579
577
580
// Negative Zero as the base:
578
581
// (-0 ^ anything positive but 0, NAN, and odd ints should be +0)
579
- test_sets_as_exponent ( -0.0 , & POS [ 3 ..] , 0.0 ) ;
582
+ test_sets_as_exponent ( -0.0 , & POS [ 3 ..] , 0.0 ) ;
580
583
581
584
// (-0 ^ anything negative but 0, NAN, and odd ints should be Infinity)
582
585
// (should panic because of divide by zero)
0 commit comments