-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample103.java
More file actions
65 lines (57 loc) · 1.91 KB
/
Example103.java
File metadata and controls
65 lines (57 loc) · 1.91 KB
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
// Example 103 from page 77
//
class Example103 {
public static void main(String[] args) {
mathtest();
}
static void mathtest() {
print("Illegal arguments, NaN results:");
print(Math.sqrt(-1)); // NaN
print(Math.log(-1)); // NaN
print(Math.pow(-1, 2.5)); // NaN
print(Math.acos(1.1)); // NaN
print("Infinite results:");
print(Math.log(0)); // -Infinity
print(Math.pow(0, -1)); // Infinity
print(Math.exp(1000.0)); // Infinity (overflow)
print("Infinite arguments:");
double infinity = Double.POSITIVE_INFINITY;
print(Math.sqrt(infinity)); // Infinity
print(Math.log(infinity)); // Infinity
print(Math.exp(-infinity)); // 0.0
print("NaN arguments and special cases:");
double nan = Math.log(-1);
print(Math.sqrt(nan)); // NaN
print(Math.pow(nan, 0)); // 1.0 (special case)
print(Math.pow(0, 0)); // 1.0 (special case)
print(Math.round(nan)); // 0 (special case)
print(Math.round(1E50)); // 9223372036854775807 (Long.MAX_VALUE)
// For all (x, y) except (0.0, 0.0):
// sign(cos(atan2(y, x))) == sign(x) && sign(sin(atan2(y, x))) == sign(y)
for (double x=-100; x<=100; x+=0.125) {
for (double y=-100; y<=100; y+=0.125) {
double r = Math.atan2(y, x);
if (!(sign(Math.cos(r))==sign(x) && sign(Math.sin(r))==sign(y)))
print("x = " + x + "; y = " + y);
}
}
}
static int sign(double x) {
final double tolerance = 1E-14;
if (x < -tolerance)
return -1;
else if (x > +tolerance)
return +1;
else
return 0;
}
static void print(String d) {
System.out.println(d);
}
static void print(double d) {
System.out.println(d);
}
static void print(long d) {
System.out.println(d);
}
}