Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 2f163d5

Browse files
rrbutanialexcrichton
authored andcommitted
Some additional tests
1 parent b814861 commit 2f163d5

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/math/pow.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,12 @@ mod tests {
421421
const NEG_ZERO: &[f64] = &[-0.0];
422422
const POS_ONE: &[f64] = &[1.0];
423423
const NEG_ONE: &[f64] = &[-1.0];
424-
const POS_FLOATS: &[f64] = &[E, PI, MAX];
425-
const NEG_FLOATS: &[f64] = &[-E, -PI, MIN];
424+
const POS_FLOATS: &[f64] = &[99.0 / 70.0, E, PI];
425+
const NEG_FLOATS: &[f64] = &[-99.0 / 70.0, -E, -PI];
426426
const POS_SMALL_FLOATS: &[f64] = &[(1.0 / 2.0), MIN_POSITIVE, EPSILON];
427427
const NEG_SMALL_FLOATS: &[f64] = &[-(1.0 / 2.0), -MIN_POSITIVE, -EPSILON];
428-
const POS_EVENS: &[f64] = &[2.0, 6.0, 8.0, 10.0, 22.0, 100.0];
429-
const NEG_EVENS: &[f64] = &[-8.0, -2.0];
428+
const POS_EVENS: &[f64] = &[2.0, 6.0, 8.0, 10.0, 22.0, 100.0, MAX];
429+
const NEG_EVENS: &[f64] = &[MIN, -100.0, -22.0, -10.0, -8.0, -6.0, -2.0];
430430
const POS_ODDS: &[f64] = &[3.0, 7.0];
431431
const NEG_ODDS: &[f64] = &[-7.0, -3.0];
432432
const NANS: &[f64] = &[NAN];
@@ -574,7 +574,7 @@ mod tests {
574574
test_sets_as_exponent(0.0, &POS[1..], 0.0);
575575

576576
// (+0 ^ anything negative but 0 and NAN should be Infinity)
577-
// (this should panic because we're dividing by zero but won't because release mode, I think)
577+
// (this should panic because we're dividing by zero)
578578
test_sets_as_exponent(0.0, &NEG[1..], INFINITY);
579579

580580
// Negative Zero as the base:
@@ -593,10 +593,39 @@ mod tests {
593593
test_sets_as_exponent(-0.0, &[NEG_ODDS], NEG_INFINITY);
594594
}
595595

596+
#[test]
597+
fn special_cases() {
598+
// / 20. (anything) ** 1 is (anything)
599+
// One as the exponent:
600+
// (anything ^ 1 should be anything - i.e. the base)
601+
test_sets(ALL, &|v: f64| pow(v, 1.0), &|v: f64| v);
602+
603+
// / 21. (anything) ** -1 is 1/(anything)
604+
// Negative One as the exponent:
605+
// (anything ^ -1 should be 1/anything)
606+
test_sets(ALL, &|v: f64| pow(v, -1.0), &|v: f64| 1.0 / v);
607+
608+
// / 22. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer)
609+
// Factoring -1 out:
610+
// (negative anything ^ integer should be (-1 ^ integer) * (positive anything ^ integer))
611+
&[POS_ZERO, NEG_ZERO, POS_ONE, NEG_ONE, POS_EVENS, NEG_EVENS].iter().for_each(|int_set| int_set.iter().for_each(|int| {
612+
test_sets(ALL, &|v: f64| pow(-v, *int), &|v: f64| pow(-1.0, *int) * pow(v, *int));
613+
}));
614+
615+
// / 23. (-anything except 0 and inf) ** (non-integer) is NAN
616+
// Negative base (imaginary results):
617+
// (-anything except 0 and Infinity ^ non-integer should be NAN)
618+
&NEG[1..(NEG.len()-1)].iter().for_each(|set| set.iter().for_each(|val| {
619+
test_sets(&ALL[3..7], &|v: f64| pow(*val, v), &|_| NAN);
620+
}));
621+
622+
}
623+
596624
#[test]
597625
fn normal_cases() {
598626
assert_eq!(pow(2.0, 20.0), (1 << 20) as f64);
599627
assert_eq!(pow(-1.0, 9.0), -1.0);
600628
assert!(pow(-1.0, 2.2).is_nan());
629+
assert!(pow(-1.0, -1.14).is_nan());
601630
}
602631
}

0 commit comments

Comments
 (0)