-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloatingPoint.java
More file actions
220 lines (188 loc) · 8.11 KB
/
FloatingPoint.java
File metadata and controls
220 lines (188 loc) · 8.11 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* The FloatingPoint class investigates a number of features of floating point arithmetic in Java,
* such as the truncation of doubles and the calculation of double maximum, minimum, and epsilon
* values. This class also defines a main method that tests various operations with derived values
* of zero and infinity, comparing results derived from computational methods against the official
* Java database.
*
* @author Rishab Parthasarathy
* @version 01.22.2022
*/
public class FloatingPoint
{
static final double INF = 1.0/0.0;
static final double NEG_INF = -1.0*INF;
/**
* Method main tests first tests these 7 conditions of floating point arithmetic in Java,
* evaluating the results produced by:
* 1. nonzero / zero
* 2. zero / zero
* 3. +- Infinity / zero
* 4. zero / +- Infinity
* 5. zero * +- Infinity
* 6. +- Infinity * +- Infinity
* 7. +- Infinity / +- Infinity
*
* where Infinity is calculated using the results of the nonzero / zero operation on
* doubles. Then, the main method calls functions to calculate +- MAX, +- MIN, and
* +- EPS, comparing the results derived against those stored in the Java Double and Math
* classes.
*
* @param args a String array of command line arguments
*/
public static void main(String[] args)
{
FloatingPoint tester = new FloatingPoint();
// nonzero (int) / zero (int)
try
{
System.out.println(1/0);
}
catch (Exception e)
{
System.out.println("nonzero (int) / zero (int): " +
"Throws an Arithmetic Exception: / by zero");
}
// nonzero (double) / zero (double)
System.out.println("nonzero (double) / zero (double): " + 1.0/0.0 + "\n");
// zero (int) / zero (int)
try
{
System.out.println(0/0);
}
catch (Exception e)
{
System.out.println("zero (int) / zero (int): " +
"Throws an Arithmetic Exception: / by zero");
}
// zero (double) / zero (double)
System.out.println("zero (double) / zero (double): " + 0.0/0.0 + "\n");
// infinity (double) / zero (double)
System.out.println("infinity (double) / zero (double): " + INF/0.0);
// -infinity (double) / zero (double)
System.out.println("-infinity (double) / zero (double): " + NEG_INF/0.0 + "\n");
// zero (double) / infinity (double)
System.out.println("zero (double) / infinity (double): " + 0.0/INF);
// zero (double) / -infinity (double)
System.out.println("zero (double) / -infinity (double): " + 0.0/NEG_INF + "\n");
// zero (double) * infinity (double)
System.out.println("zero (double) * infinity (double): " + 0.0*INF);
// zero (double) * -infinity (double)
System.out.println("zero (double) * -infinity (double): " + 0.0*NEG_INF + "\n");
// infinity (double) * infinity (double)
System.out.println("infinity (double) * infinity (double): " + INF*INF);
// infinity (double) * -infinity (double)
System.out.println("infinity (double) * -infinity (double): " + INF*NEG_INF);
// -infinity (double) * infinity (double)
System.out.println("-infinity (double) * infinity (double): " + NEG_INF*INF);
// -infinity (double) * -infinity (double)
System.out.println("-infinity (double) * -infinity (double): " + NEG_INF*NEG_INF + "\n");
// infinity (double) / infinity (double)
System.out.println("infinity (double) / infinity (double): " + INF/INF);
double nanOne = INF/INF;
double nanTwo = 0.0 * INF;
System.out.println(nanOne == Double.NaN);
// infinity (double) / -infinity (double)
System.out.println("infinity (double) / -infinity (double): " + INF/NEG_INF);
// -infinity (double) / infinity (double)
System.out.println("-infinity (double) / infinity (double): " + NEG_INF/INF);
// -infinity (double) / -infinity (double)
System.out.println("-infinity (double) / -infinity (double): " + NEG_INF/NEG_INF + "\n");
// value of +MIN
double min = tester.calcMin(1.0);
System.out.println("Calculated value of +MIN: " + min);
// value of -MIN
double negMin = tester.calcMin(-1.0);
System.out.println("Calculated value of -MIN: " + negMin);
// Java stipulations on MIN
System.out.println("Official JAVA value of MIN: " + Double.MIN_VALUE);
System.out.println("Official JAVA value of MIN_EXP: " + Double.MIN_EXPONENT);
System.out.println("Official JAVA value of MIN_NORMAL: " + Double.MIN_NORMAL + "\n");
// value of +MAX
double max = tester.calcMax(1.0);
System.out.println("Calculated value of +MAX: " + max);
// value of -MAX
double negMax = tester.calcMax(-1.0);
System.out.println("Calculated value of -MAX: " + negMax);
// Java stipulations on MAX
System.out.println("Official JAVA value of MAX: " + Double.MAX_VALUE);
System.out.println("Official JAVA value of MAX_EXP: " + Double.MAX_EXPONENT + "\n");
// value of +EPS
double eps = tester.calcEps(1.0);
System.out.println("Calculated value of +EPS: " + eps);
// value of -EPS
double negEps = tester.calcEps(-1.0);
System.out.println("Calculated value of -EPS: " + negEps);
// Java stipulation on EPS
System.out.println("Official JAVA value of EPS: " + Math.ulp(1.0));
return;
}
/**
* Method calcMin uses binary descent to calculate the double MIN value in Java given some
* starting value for the descent. Specifically, this method utilizes the formula:
*
* 2.0 * (MIN / 2.0) != 0.0
*
* Whenever this formula is true, calcMin tests the next value of MIN as half of the current
* value, continuing in a binary descent until the smallest possible value of MIN has been
* derived.
*
* @param startingValue the starting point for the binary descent
* @return the double MIN value calculated using the given binary descent
*/
private double calcMin(double startingValue)
{
double curMin = startingValue;
while (! (2.0*(curMin/2.0) == 0.0))
{
curMin /= 2.0;
}
curMin *= 2.0;
return curMin;
}
/**
* Method calcMax uses binary ascent to calculate the double MAX value in Java given some
* starting value for the ascent. Specifically, this method utilizes the formula:
*
* (MAX * 2.0) / 2.0 != Infinity
*
* Whenever this formula is true, calcMax tests the next value of MAX as double the current
* value, continuing in a binary ascent until the largest possible value of MAX has been
* derived.
*
* @param startingValue the starting point for the binary ascent
* @return the double MAX value calculated using the given binary ascent
*/
private double calcMax(double startingValue)
{
double curMax = startingValue;
while (! (((2.0*curMax)/2.0 == INF) || ((2.0*curMax)/2.0 == -1.0*INF)))
{
curMax *= 2.0;
}
curMax /= 2.0;
return curMax;
}
/**
* Method calcEps uses binary descent to calculate the double EPS value in Java given some
* starting value for the descent. Specifically, this method utilizes the formula:
*
* 1.0 + (EPS / 2.0) = 1.0 for EPS != 0
*
* Whenever this formula is false, calcEps tests the next value of EPS as half of the current
* value, continuing in a binary descent until the largest possible value of EPS has been
* derived.
*
* @param startingValue the starting point for the binary descent
* @return the double EPS value calculated using the given binary descent
*/
private double calcEps(double startingValue)
{
double curEps = startingValue;
while ((1.0 + curEps/2.0) != 1.0)
{
curEps /= 2.0;
}
return curEps;
}
}