28 |
package java.lang; |
package java.lang; |
29 |
|
|
30 |
import java.util.Random; |
import java.util.Random; |
31 |
|
import gnu.classpath.Configuration; |
32 |
|
|
33 |
/** |
/** |
34 |
* Helper class containing useful mathematical functions and constants. |
* Helper class containing useful mathematical functions and constants. |
47 |
/** |
/** |
48 |
* Math is non-instantiable |
* Math is non-instantiable |
49 |
*/ |
*/ |
50 |
private Math() |
private Math () |
51 |
{ |
{ |
52 |
} |
} |
53 |
|
|
54 |
static { |
static |
55 |
System.loadLibrary("javalangmath"); |
{ |
56 |
|
if (Configuration.INIT_LOAD_LIBRARY) |
57 |
|
{ |
58 |
|
System.loadLibrary ("javalangmath"); |
59 |
|
} |
60 |
} |
} |
61 |
|
|
62 |
static Random rand; |
static Random rand; |
67 |
* @see #log(double) |
* @see #log(double) |
68 |
* @see #exp(double) |
* @see #exp(double) |
69 |
*/ |
*/ |
70 |
public static final double E = 2.7182818284590452354; |
public static final double E = 2.7182818284590452354; |
71 |
|
|
72 |
/** |
/** |
73 |
* The mathematical constant <em>pi</em>. |
* The mathematical constant <em>pi</em>. |
89 |
* @return the absolute value. |
* @return the absolute value. |
90 |
* @see java.lang.Integer#MIN_VALUE |
* @see java.lang.Integer#MIN_VALUE |
91 |
*/ |
*/ |
92 |
public static int abs(int a) { |
public static int abs (int a) |
93 |
|
{ |
94 |
return (a < 0) ? -a : a; |
return (a < 0) ? -a : a; |
95 |
} |
} |
96 |
|
|
108 |
* @return the absolute value. |
* @return the absolute value. |
109 |
* @see java.lang.Long#MIN_VALUE |
* @see java.lang.Long#MIN_VALUE |
110 |
*/ |
*/ |
111 |
public static long abs(long a) { |
public static long abs (long a) |
112 |
|
{ |
113 |
return (a < 0) ? -a : a; |
return (a < 0) ? -a : a; |
114 |
} |
} |
115 |
|
|
119 |
* @param a the number to take the absolute value of. |
* @param a the number to take the absolute value of. |
120 |
* @return the absolute value. |
* @return the absolute value. |
121 |
*/ |
*/ |
122 |
public static float abs(float a) |
public static float abs (float a) |
123 |
{ |
{ |
124 |
// avoid method call overhead, but treat -0.0 correctly |
// avoid method call overhead, but treat -0.0 correctly |
125 |
// return Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a)); |
// return Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a)); |
132 |
* @param a the number to take the absolute value of. |
* @param a the number to take the absolute value of. |
133 |
* @return the absolute value. |
* @return the absolute value. |
134 |
*/ |
*/ |
135 |
public static double abs(double a) { |
public static double abs (double a) |
136 |
|
{ |
137 |
// avoid method call overhead, but treat -0.0 correctly |
// avoid method call overhead, but treat -0.0 correctly |
138 |
// return Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1); |
// return Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1); |
139 |
return (a <= 0) ? 0 - a : a; |
return (a <= 0) ? 0 - a : a; |
145 |
* @param b a second number |
* @param b a second number |
146 |
* @return the smaller of the two numbers. |
* @return the smaller of the two numbers. |
147 |
*/ |
*/ |
148 |
public static int min(int a, int b) { |
public static int min (int a, int b) |
149 |
|
{ |
150 |
return (a < b) ? a : b; |
return (a < b) ? a : b; |
151 |
} |
} |
152 |
|
|
156 |
* @param b a second number |
* @param b a second number |
157 |
* @return the smaller of the two numbers. |
* @return the smaller of the two numbers. |
158 |
*/ |
*/ |
159 |
public static long min(long a, long b) { |
public static long min (long a, long b) |
160 |
|
{ |
161 |
return (a < b) ? a : b; |
return (a < b) ? a : b; |
162 |
} |
} |
163 |
|
|
170 |
* @param b a second number |
* @param b a second number |
171 |
* @return the smaller of the two numbers. |
* @return the smaller of the two numbers. |
172 |
*/ |
*/ |
173 |
public static float min(float a, float b) |
public static float min (float a, float b) |
174 |
{ |
{ |
175 |
// this check for NaN, from JLS 15.21.1, saves a method call |
// this check for NaN, from JLS 15.21.1, saves a method call |
176 |
if (a != a) |
if (a != a) |
190 |
* @param b a second number |
* @param b a second number |
191 |
* @return the smaller of the two numbers. |
* @return the smaller of the two numbers. |
192 |
*/ |
*/ |
193 |
public static double min(double a, double b) |
public static double min (double a, double b) |
194 |
{ |
{ |
195 |
// this check for NaN, from JLS 15.21.1, saves a method call |
// this check for NaN, from JLS 15.21.1, saves a method call |
196 |
if (a != a) |
if (a != a) |
208 |
* @param b a second number |
* @param b a second number |
209 |
* @return the larger of the two numbers. |
* @return the larger of the two numbers. |
210 |
*/ |
*/ |
211 |
public static int max(int a, int b) { |
public static int max (int a, int b) |
212 |
|
{ |
213 |
return (a > b) ? a : b; |
return (a > b) ? a : b; |
214 |
} |
} |
215 |
|
|
219 |
* @param b a second number |
* @param b a second number |
220 |
* @return the larger of the two numbers. |
* @return the larger of the two numbers. |
221 |
*/ |
*/ |
222 |
public static long max(long a, long b) { |
public static long max (long a, long b) |
223 |
|
{ |
224 |
return (a > b) ? a : b; |
return (a > b) ? a : b; |
225 |
} |
} |
226 |
|
|
232 |
* @param b a second number |
* @param b a second number |
233 |
* @return the larger of the two numbers. |
* @return the larger of the two numbers. |
234 |
*/ |
*/ |
235 |
public static float max(float a, float b) |
public static float max (float a, float b) |
236 |
{ |
{ |
237 |
// this check for NaN, from JLS 15.21.1, saves a method call |
// this check for NaN, from JLS 15.21.1, saves a method call |
238 |
if (a != a) |
if (a != a) |
252 |
* @param b a second number |
* @param b a second number |
253 |
* @return the larger of the two numbers. |
* @return the larger of the two numbers. |
254 |
*/ |
*/ |
255 |
public static double max(double a, double b) |
public static double max (double a, double b) |
256 |
{ |
{ |
257 |
// this check for NaN, from JLS 15.21.1, saves a method call |
// this check for NaN, from JLS 15.21.1, saves a method call |
258 |
if (a != a) |
if (a != a) |
269 |
* @param a the angle (in radians). |
* @param a the angle (in radians). |
270 |
* @return sin(a). |
* @return sin(a). |
271 |
*/ |
*/ |
272 |
public native static double sin(double a); |
public native static double sin (double a); |
273 |
|
|
274 |
/** |
/** |
275 |
* The trigonometric function <em>cos</em>. |
* The trigonometric function <em>cos</em>. |
276 |
* @param a the angle (in radians). |
* @param a the angle (in radians). |
277 |
* @return cos(a). |
* @return cos(a). |
278 |
*/ |
*/ |
279 |
public native static double cos(double a); |
public native static double cos (double a); |
280 |
|
|
281 |
/** |
/** |
282 |
* The trigonometric function <em>tan</em>. |
* The trigonometric function <em>tan</em>. |
283 |
* @param a the angle (in radians). |
* @param a the angle (in radians). |
284 |
* @return tan(a). |
* @return tan(a). |
285 |
*/ |
*/ |
286 |
public native static double tan(double a); |
public native static double tan (double a); |
287 |
|
|
288 |
/** |
/** |
289 |
* The trigonometric function <em>arcsin</em>. |
* The trigonometric function <em>arcsin</em>. |
291 |
* @param a the sin to turn back into an angle. |
* @param a the sin to turn back into an angle. |
292 |
* @return arcsin(a). |
* @return arcsin(a). |
293 |
*/ |
*/ |
294 |
public native static double asin(double a); |
public native static double asin (double a); |
295 |
|
|
296 |
/** |
/** |
297 |
* The trigonometric function <em>arccos</em>. |
* The trigonometric function <em>arccos</em>. |
299 |
* @param a the cos to turn back into an angle. |
* @param a the cos to turn back into an angle. |
300 |
* @return arccos(a). |
* @return arccos(a). |
301 |
*/ |
*/ |
302 |
public native static double acos(double a); |
public native static double acos (double a); |
303 |
|
|
304 |
/** |
/** |
305 |
* The trigonometric function <em>arctan</em>. |
* The trigonometric function <em>arctan</em>. |
308 |
* @return arcsin(a). |
* @return arcsin(a). |
309 |
* @see #atan(double,double) |
* @see #atan(double,double) |
310 |
*/ |
*/ |
311 |
public native static double atan(double a); |
public native static double atan (double a); |
312 |
|
|
313 |
/** |
/** |
314 |
* A special version of the trigonometric function <em>arctan</em>. |
* A special version of the trigonometric function <em>arctan</em>. |
315 |
* Given a position (x,y), this function will give you the angle of |
* Given a position (x,y), this function will give you the angle of |
322 |
* @return arcsin(a). |
* @return arcsin(a). |
323 |
* @see #atan(double) |
* @see #atan(double) |
324 |
*/ |
*/ |
325 |
public native static double atan2(double y, double x); |
public native static double atan2 (double y, double x); |
326 |
|
|
327 |
/** |
/** |
328 |
* Take <em>e</em><sup>a</sup>. The opposite of <code>log()</code>. |
* Take <em>e</em><sup>a</sup>. The opposite of <code>log()</code>. |
331 |
* @see #log(double) |
* @see #log(double) |
332 |
* @see #pow(double,double) |
* @see #pow(double,double) |
333 |
*/ |
*/ |
334 |
public native static double exp(double a); |
public native static double exp (double a); |
335 |
|
|
336 |
/** |
/** |
337 |
* Take ln(a) (the natural log). The opposite of <code>exp()</code>. |
* Take ln(a) (the natural log). The opposite of <code>exp()</code>. |
341 |
* @return the natural log of <code>a</code>. |
* @return the natural log of <code>a</code>. |
342 |
* @see #exp(double) |
* @see #exp(double) |
343 |
*/ |
*/ |
344 |
public native static double log(double a); |
public native static double log (double a); |
345 |
|
|
346 |
/** |
/** |
347 |
* Take a square root. |
* Take a square root. |
350 |
* @return the square root of the argument. |
* @return the square root of the argument. |
351 |
* @see #pow(double,double) |
* @see #pow(double,double) |
352 |
*/ |
*/ |
353 |
public native static double sqrt(double a); |
public native static double sqrt (double a); |
354 |
|
|
355 |
/** |
/** |
356 |
* Take a number to a power. |
* Take a number to a power. |
358 |
* @param b the power to raise it to. |
* @param b the power to raise it to. |
359 |
* @return a<sup>b</sup>. |
* @return a<sup>b</sup>. |
360 |
*/ |
*/ |
361 |
public native static double pow(double a, double b); |
public native static double pow (double a, double b); |
362 |
|
|
363 |
/** |
/** |
364 |
* Get the floating point remainder on two numbers, |
* Get the floating point remainder on two numbers, |
384 |
* @return the IEEE 754-defined floating point remainder of x/y. |
* @return the IEEE 754-defined floating point remainder of x/y. |
385 |
* @see #rint(double) |
* @see #rint(double) |
386 |
*/ |
*/ |
387 |
public native static double IEEEremainder(double x, double y); |
public native static double IEEEremainder (double x, double y); |
388 |
|
|
389 |
/** |
/** |
390 |
* Take the nearest integer that is that is greater than or equal to the |
* Take the nearest integer that is that is greater than or equal to the |
392 |
* @param a the value to act upon. |
* @param a the value to act upon. |
393 |
* @return the nearest integer >= <code>a</code>. |
* @return the nearest integer >= <code>a</code>. |
394 |
*/ |
*/ |
395 |
public native static double ceil(double a); |
public native static double ceil (double a); |
396 |
|
|
397 |
/** |
/** |
398 |
* Take the nearest integer that is that is less than or equal to the |
* Take the nearest integer that is that is less than or equal to the |
400 |
* @param a the value to act upon. |
* @param a the value to act upon. |
401 |
* @return the nearest integer <= <code>a</code>. |
* @return the nearest integer <= <code>a</code>. |
402 |
*/ |
*/ |
403 |
public native static double floor(double a); |
public native static double floor (double a); |
404 |
|
|
405 |
/** |
/** |
406 |
* Take the nearest integer to the argument. If it is exactly between |
* Take the nearest integer to the argument. If it is exactly between |
408 |
* @param a the value to act upon. |
* @param a the value to act upon. |
409 |
* @return the nearest integer to <code>a</code>. |
* @return the nearest integer to <code>a</code>. |
410 |
*/ |
*/ |
411 |
public native static double rint(double a); |
public native static double rint (double a); |
412 |
|
|
413 |
/** |
/** |
414 |
* Take the nearest integer to the argument. If it is exactly between |
* Take the nearest integer to the argument. If it is exactly between |
422 |
* @see java.lang.Integer#MIN_VALUE |
* @see java.lang.Integer#MIN_VALUE |
423 |
* @see java.lang.Integer#MAX_VALUE |
* @see java.lang.Integer#MAX_VALUE |
424 |
*/ |
*/ |
425 |
public static int round(float a) { |
public static int round (float a) |
426 |
return (int)floor(a + 0.5f); |
{ |
427 |
|
return (int) floor (a + 0.5f); |
428 |
} |
} |
429 |
|
|
430 |
/** |
/** |
439 |
* @see java.lang.Long#MIN_VALUE |
* @see java.lang.Long#MIN_VALUE |
440 |
* @see java.lang.Long#MAX_VALUE |
* @see java.lang.Long#MAX_VALUE |
441 |
*/ |
*/ |
442 |
public static long round(double a) { |
public static long round (double a) |
443 |
return (long)floor(a + 0.5d); |
{ |
444 |
|
return (long) floor (a + 0.5d); |
445 |
} |
} |
446 |
|
|
447 |
/** |
/** |
449 |
* @return a random number. |
* @return a random number. |
450 |
* @see java.lang.Random#nextDouble() |
* @see java.lang.Random#nextDouble() |
451 |
*/ |
*/ |
452 |
public static synchronized double random() { |
public static synchronized double random () |
453 |
|
{ |
454 |
if (rand == null) |
if (rand == null) |
455 |
rand = new Random(); |
rand = new Random (); |
456 |
return rand.nextDouble(); |
return rand.nextDouble (); |
457 |
} |
} |
458 |
|
|
459 |
/** |
/** |
462 |
* @param degrees an angle in degrees |
* @param degrees an angle in degrees |
463 |
* @return the angle in radians |
* @return the angle in radians |
464 |
*/ |
*/ |
465 |
public static double toRadians(double degrees) { |
public static double toRadians (double degrees) |
466 |
return degrees * 0.017453292519943295; /* (degrees * (PI/180)) */ |
{ |
467 |
|
return degrees * 0.017453292519943295; /* (degrees * (PI/180)) */ |
468 |
} |
} |
469 |
|
|
470 |
/** |
/** |
473 |
* @param rads an angle in radians |
* @param rads an angle in radians |
474 |
* @return the angle in degrees |
* @return the angle in degrees |
475 |
*/ |
*/ |
476 |
public static double toDegrees(double rads) { |
public static double toDegrees (double rads) |
477 |
return rads / 0.017453292519943295; /* (rads / (PI/180)) */ |
{ |
478 |
|
return rads / 0.017453292519943295; /* (rads / (PI/180)) */ |
479 |
} |
} |
480 |
} |
} |