1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package com.octoperf;
import org.junit.Test;
import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Double.NaN;
import static java.lang.Double.POSITIVE_INFINITY;
import static org.junit.Assert.assertEquals;
public class MathPowTest {
private static final double DELTA = 0.001d;
@Test
public void javaMathPow() {
// If the second argument is positive or negative zero, then the result is 1.0.
assertEquals(1, Math.pow(2, 0), DELTA);
// If the second argument is 1.0, then the result is the same as the first argument.
assertEquals(2d, Math.pow(2, 1), DELTA);
// If the second argument is NaN, then the result is NaN.
assertEquals(NaN, Math.pow(2, NaN), DELTA);
// If the absolute value of the first argument is greater than 1 and the second argument is positive infinity, or
// the absolute value of the first argument is less than 1 and the second argument is negative infinity,
// then the result is positive infinity.
assertEquals(POSITIVE_INFINITY, Math.pow(2, POSITIVE_INFINITY), DELTA);
assertEquals(POSITIVE_INFINITY, Math.pow(0.5d, NEGATIVE_INFINITY), DELTA);
// If the absolute value of the first argument is greater than 1 and the second argument is negative infinity, or
// the absolute value of the first argument is less than 1 and the second argument is positive infinity,
// then the result is positive zero.
assertEquals(0d, Math.pow(2, NEGATIVE_INFINITY), DELTA);
assertEquals(0d, Math.pow(0.5d, POSITIVE_INFINITY), DELTA);
// If the absolute value of the first argument equals 1 and the second argument is infinite, then the result is NaN.
assertEquals(NaN, Math.pow(1d, POSITIVE_INFINITY), DELTA);
// If
// the first argument is positive zero and the second argument is greater than zero, or
// the first argument is positive infinity and the second argument is less than zero,
// then the result is positive zero.
assertEquals(0d, Math.pow(0d, 1d), DELTA);
assertEquals(0d, Math.pow(POSITIVE_INFINITY, -1d), DELTA);
// If
// the first argument is positive zero and the second argument is less than zero, or
// the first argument is positive infinity and the second argument is greater than zero,
// then the result is positive infinity.
assertEquals(POSITIVE_INFINITY, Math.pow(0d, -1d), DELTA);
assertEquals(POSITIVE_INFINITY, Math.pow(POSITIVE_INFINITY, 1d), DELTA);
// If
//the first argument is negative zero and the second argument is greater than zero but not a finite odd integer, or
//the first argument is negative infinity and the second argument is less than zero but not a finite odd integer,
//then the result is positive zero.
assertEquals(0d, Math.pow(-0d, 0.5d), DELTA);
assertEquals(0d, Math.pow(NEGATIVE_INFINITY, -1.5d), DELTA);
// If
// the first argument is negative zero and the second argument is a positive finite odd integer, or
// the first argument is negative infinity and the second argument is a negative finite odd integer,
// then the result is negative zero.
assertEquals(0d, Math.pow(-0d, 1d), DELTA);
assertEquals(0d, Math.pow(NEGATIVE_INFINITY, -1d), DELTA);
// If
// the first argument is negative zero and the second argument is less than zero but not a finite odd integer, or
// the first argument is negative infinity and the second argument is greater than zero but not a finite odd integer,
// then the result is positive infinity.
assertEquals(POSITIVE_INFINITY, Math.pow(-0d, -2d), DELTA);
assertEquals(POSITIVE_INFINITY, Math.pow(NEGATIVE_INFINITY, 2d), DELTA);
// If
// the first argument is negative zero and the second argument is a negative finite odd integer, or
// the first argument is negative infinity and the second argument is a positive finite odd integer,
// then the result is negative infinity.
assertEquals(NEGATIVE_INFINITY, Math.pow(-0d, -1d), DELTA);
assertEquals(NEGATIVE_INFINITY, Math.pow(NEGATIVE_INFINITY, 1d), DELTA);
// If the first argument is finite and less than zero
// if the second argument is a finite even integer, the result is equal to the result of raising
// the absolute value of the first argument to the power of the second argument
// if the second argument is a finite odd integer, the result is equal to the negative of the
// result of raising the absolute value of the first argument to the power of the second argument
// if the second argument is finite and not an integer, then the result is NaN.
assertEquals(1d, Math.pow(-1d, 2), DELTA);
assertEquals(-1d, Math.pow(-1d, 3), DELTA);
assertEquals(NaN, Math.pow(-1d, 2.5d), DELTA);
// If both arguments are integers, then the result is exactly equal to the mathematical
// result of raising the first argument to the power of the second argument if that result
// can in fact be represented exactly as a double value.
assertEquals(1024d, Math.pow(2d, 10d), DELTA);
}
}
|