Skip to content

Commit b814861

Browse files
rrbutanialexcrichton
authored andcommitted
Cleaned up + rustfmt'ed
1 parent 5dddb9b commit b814861

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

src/math/pow.rs

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -409,24 +409,14 @@ pub fn pow(x: f64, y: f64) -> f64 {
409409
return s * z;
410410
}
411411

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-
419412
#[cfg(test)]
420413
mod tests {
421-
// #[macro_use]
422414
extern crate std;
423415

424416
use self::std::f64::consts::{E, PI};
425417
use self::std::f64::{EPSILON, INFINITY, MAX, MIN, MIN_POSITIVE, NAN, NEG_INFINITY};
426418
use super::pow;
427419

428-
// const TESTCASES: &[f64] = &[1.0, 0.0, PI, -PI, E, -E, MIN, MAX, MIN_POSITIVE, NAN, INFINITY, NEG_INFINITY];
429-
430420
const POS_ZERO: &[f64] = &[0.0];
431421
const NEG_ZERO: &[f64] = &[-0.0];
432422
const POS_ONE: &[f64] = &[1.0];
@@ -440,21 +430,43 @@ mod tests {
440430
const POS_ODDS: &[f64] = &[3.0, 7.0];
441431
const NEG_ODDS: &[f64] = &[-7.0, -3.0];
442432
const NANS: &[f64] = &[NAN];
443-
// const EDGES: &[f64] = &[MIN, MAX, MIN_POSITIVE, EPSILON];
444433
const POS_INF: &[f64] = &[INFINITY];
445434
const NEG_INF: &[f64] = &[NEG_INFINITY];
446435

447436
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,
450452
];
451453
const POS: &[&[f64]] = &[POS_ZERO, POS_ODDS, POS_ONE, POS_FLOATS, POS_EVENS, POS_INF];
452454
const NEG: &[&[f64]] = &[NEG_ZERO, NEG_ODDS, NEG_ONE, NEG_FLOATS, NEG_EVENS, NEG_INF];
453455

454456
fn pow_test(base: f64, exponent: f64, expected: f64) {
455457
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+
);
458470
}
459471

460472
fn test_sets_as_base(sets: &[&[f64]], exponent: f64, expected: f64) {
@@ -468,31 +480,37 @@ mod tests {
468480
}
469481

470482
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| {
473485
let exp = expected(*val);
474486
let res = computed(*val);
475487

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+
});
479501
}
480502

481-
/// 1. (anything) ** 0 is 1
482503
#[test]
483504
fn zero_as_exponent() {
484505
test_sets_as_base(ALL, 0.0, 1.0);
485506
test_sets_as_base(ALL, -0.0, 1.0);
486507
}
487508

488-
/// 2. 1 ** (anything) is 1
489509
#[test]
490510
fn one_as_base() {
491511
test_sets_as_exponent(1.0, ALL, 1.0);
492512
}
493513

494-
/// 3. (anything except 1) ** NAN is NAN
495-
/// 4. NAN ** (anything except 0) is NAN
496514
#[test]
497515
fn nan_inputs() {
498516
// NAN as the base:
@@ -504,10 +522,6 @@ mod tests {
504522
test_sets_as_base(&ALL[..(ALL.len() - 2)], NAN, NAN);
505523
}
506524

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)
511525
#[test]
512526
fn infinity_as_base() {
513527
// Positive Infinity as the base:
@@ -527,19 +541,14 @@ mod tests {
527541
test_sets(ALL, &|v: f64| pow(NEG_INFINITY, v), &|v: f64| pow(-0.0, -v));
528542
}
529543

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
535544
#[test]
536545
fn infinity_as_exponent() {
537546
// Positive/Negative base greater than 1:
538547
// (pos/neg > 1 ^ Infinity should be Infinity - note this excludes NAN as the base)
539548
test_sets_as_base(&ALL[5..(ALL.len() - 2)], INFINITY, INFINITY);
540549

541550
// (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);
543552

544553
// Positive/Negative base less than 1:
545554
let base_below_one = &[POS_ZERO, NEG_ZERO, NEG_SMALL_FLOATS, POS_SMALL_FLOATS];
@@ -558,12 +567,6 @@ mod tests {
558567
test_sets_as_base(&[NEG_ONE, POS_ONE], NEG_INFINITY, 1.0);
559568
}
560569

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
567570
#[test]
568571
fn zero_as_base() {
569572
// Positive Zero as the base:
@@ -576,7 +579,7 @@ mod tests {
576579

577580
// Negative Zero as the base:
578581
// (-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);
580583

581584
// (-0 ^ anything negative but 0, NAN, and odd ints should be Infinity)
582585
// (should panic because of divide by zero)

0 commit comments

Comments
 (0)