-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathLibraryExample.java
127 lines (106 loc) · 3.78 KB
/
MathLibraryExample.java
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Java Program to demonstrate the
// Use of Math Class
public class MathLibraryExample {
public static void main(String[] args)
{
int i = 7;
int j = -9;
double x = 72.3;
double y = 0.34;
System.out.println("i is " + i);
System.out.println("j is " + j);
// The absolute value of a number is equal to the
// number if the number is positive or zero and
// equal to the negative of the number if the number
// is negative.
System.out.println("|" + i + "| is " + Math.abs(i));
System.out.println("|" + y + "| is " + Math.abs(y));
// Truncating and Rounding functions
// You can round off a floating point number to the
// nearest integer with round()
System.out.println(x + " is approximately "
+ Math.round(x));
System.out.println(y + " is approximately "
+ Math.round(y));
// The "ceiling" of a number is the smallest integer
// greater than or equal to the number. Every
// integer is its own //ceiling.
System.out.println("The ceiling of " + x + " is "
+ Math.ceil(x));
System.out.println("The ceiling of " + y + " is "
+ Math.ceil(y));
// The "floor" of a number is the largest integer
// less than or equal to the number. Every integer
// is its own floor.
System.out.println("The floor of " + x + " is "
+ Math.floor(x));
System.out.println("The floor of " + y + " is "
+ Math.floor(y));
// Comparison operators
// min() returns the smaller of the two arguments
// you pass it
System.out.println("min(" + i + "," + j + ") is "
+ Math.min(i, j));
System.out.println("min(" + x + "," + y + ") is "
+ Math.min(x, y));
// There's a corresponding max() method
// that returns the larger of two numbers
System.out.println("max(" + i + "," + j + ") is "
+ Math.max(i, j));
System.out.println("max(" + x + "," + y + ") is "
+ Math.max(x, y));
// The Math library defines a couple of useful
// constants:
System.out.println("Pi is " + Math.PI);
System.out.println("e is " + Math.E);
// Trigonometric methods. All arguments are given in
// radians
// Convert a 45 degree angle to radians
double angle = 45.0 * 2.0 * Math.PI / 360.0;
System.out.println("cos(" + angle + ") is "
+ Math.cos(angle));
System.out.println("sin(" + angle + ") is "
+ Math.sin(angle));
// Inverse Trigonometric methods. All values are
// returned as radians
double value = 0.707;
System.out.println("acos(" + value + ") is "
+ Math.acos(value));
System.out.println("asin(" + value + ") is "
+ Math.asin(value));
System.out.println("atan(" + value + ") is "
+ Math.atan(value));
// Exponential and Logarithmic Methods
// exp(a) returns e (2.71828...) raised
// to the power of a.
System.out.println("exp(1.0) is " + Math.exp(1.0));
System.out.println("exp(10.0) is "
+ Math.exp(10.0));
System.out.println("exp(0.0) is " + Math.exp(0.0));
// log(a) returns the natural
// logarithm (base e) of a.
System.out.println("log(1.0) is " + Math.log(1.0));
System.out.println("log(10.0) is "
+ Math.log(10.0));
System.out.println("log(Math.E) is "
+ Math.log(Math.E));
// pow(x, y) returns the x raised
// to the yth power.
System.out.println("pow(2.0, 2.0) is "
+ Math.pow(2.0, 2.0));
System.out.println("pow(10.0, 3.5) is "
+ Math.pow(10.0, 3.5));
System.out.println("pow(8, -1) is "
+ Math.pow(8, -1));
// sqrt(x) returns the square root of x.
for (i = 0; i < 10; i++) {
System.out.println("The square root of " + i
+ " is " + Math.sqrt(i));
}
// Finally there's one Random method
// that returns a pseudo-random number
// between 0.0 and 1.0;
System.out.println("Here's one random number: "
+ Math.random());
}
}