1 |
/* java.lang.Math |
/* java.lang.Math -- common mathematical functions, native allowed |
2 |
Copyright (C) 1998, 2001 Free Software Foundation, Inc. |
Copyright (C) 1998, 2001, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
7 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
8 |
the Free Software Foundation; either version 2, or (at your option) |
the Free Software Foundation; either version 2, or (at your option) |
9 |
any later version. |
any later version. |
10 |
|
|
11 |
GNU Classpath is distributed in the hope that it will be useful, but |
GNU Classpath is distributed in the hope that it will be useful, but |
12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
58 |
/** |
/** |
59 |
* Math is non-instantiable |
* Math is non-instantiable |
60 |
*/ |
*/ |
61 |
private Math () |
private Math() |
62 |
{ |
{ |
63 |
} |
} |
64 |
|
|
66 |
{ |
{ |
67 |
if (Configuration.INIT_LOAD_LIBRARY) |
if (Configuration.INIT_LOAD_LIBRARY) |
68 |
{ |
{ |
69 |
System.loadLibrary ("javalang"); |
System.loadLibrary("javalang"); |
70 |
} |
} |
71 |
} |
} |
72 |
|
|
73 |
static Random rand; |
/** |
74 |
|
* A random number generator, initialized on first use. |
75 |
|
*/ |
76 |
|
private static Random rand; |
77 |
|
|
78 |
/** |
/** |
79 |
* The mathematical constant <em>e</em>. |
* The most accurate approximation to the mathematical constant <em>e</em>: |
80 |
* Used in natural log and exp. |
* <code>2.718281828459045</code>. Used in natural log and exp. |
81 |
|
* |
82 |
* @see #log(double) |
* @see #log(double) |
83 |
* @see #exp(double) |
* @see #exp(double) |
84 |
*/ |
*/ |
85 |
public static final double E = 2.7182818284590452354; |
public static final double E = 2.718281828459045; |
86 |
|
|
87 |
/** |
/** |
88 |
* The mathematical constant <em>pi</em>. |
* The most accurate approximation to the mathematical constant <em>pi</em>: |
89 |
* This is the ratio of a circle's diameter to its circumference. |
* <code>3.141592653589793</code>. This is the ratio of a circle's diameter |
90 |
|
* to its circumference. |
91 |
*/ |
*/ |
92 |
public static final double PI = 3.14159265358979323846; |
public static final double PI = 3.141592653589793; |
93 |
|
|
94 |
/** |
/** |
95 |
* Take the absolute value of the argument. |
* Take the absolute value of the argument. |
101 |
* a computer, MIN_VALUE is what will be returned. |
* a computer, MIN_VALUE is what will be returned. |
102 |
* This is a <em>negative</em> value. You have been warned. |
* This is a <em>negative</em> value. You have been warned. |
103 |
* |
* |
104 |
* @param a the number to take the absolute value of. |
* @param i the number to take the absolute value of |
105 |
* @return the absolute value. |
* @return the absolute value |
106 |
* @see java.lang.Integer#MIN_VALUE |
* @see Integer#MIN_VALUE |
107 |
*/ |
*/ |
108 |
public static int abs (int a) |
public static int abs(int i) |
109 |
{ |
{ |
110 |
return (a < 0) ? -a : a; |
return (i < 0) ? -i : i; |
111 |
} |
} |
112 |
|
|
113 |
/** |
/** |
120 |
* a computer, MIN_VALUE is what will be returned. |
* a computer, MIN_VALUE is what will be returned. |
121 |
* This is a <em>negative</em> value. You have been warned. |
* This is a <em>negative</em> value. You have been warned. |
122 |
* |
* |
123 |
* @param a the number to take the absolute value of. |
* @param l the number to take the absolute value of |
124 |
* @return the absolute value. |
* @return the absolute value |
125 |
* @see java.lang.Long#MIN_VALUE |
* @see Long#MIN_VALUE |
126 |
*/ |
*/ |
127 |
public static long abs (long a) |
public static long abs(long l) |
128 |
{ |
{ |
129 |
return (a < 0) ? -a : a; |
return (l < 0) ? -l : l; |
130 |
} |
} |
131 |
|
|
132 |
/** |
/** |
133 |
* Take the absolute value of the argument. |
* Take the absolute value of the argument. |
134 |
* (Absolute value means make it positive.) |
* (Absolute value means make it positive.) |
135 |
* @param a the number to take the absolute value of. |
* <P> |
136 |
* @return the absolute value. |
* |
137 |
|
* This is equivalent, but faster than, calling |
138 |
|
* <code>Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))</code>. |
139 |
|
* |
140 |
|
* @param f the number to take the absolute value of |
141 |
|
* @return the absolute value |
142 |
*/ |
*/ |
143 |
public static float abs (float a) |
public static float abs(float f) |
144 |
{ |
{ |
145 |
// avoid method call overhead, but treat -0.0 correctly |
return (f <= 0) ? 0 - f : f; |
|
// return Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a)); |
|
|
return (a <= 0) ? 0 - a : a; |
|
146 |
} |
} |
147 |
|
|
148 |
/** |
/** |
149 |
* Take the absolute value of the argument. |
* Take the absolute value of the argument. |
150 |
* (Absolute value means make it positive.) |
* (Absolute value means make it positive.) |
151 |
* @param a the number to take the absolute value of. |
* |
152 |
* @return the absolute value. |
* This is equivalent, but faster than, calling |
153 |
|
* <code>Double.longBitsToDouble(Double.doubleToLongBits(a) |
154 |
|
* << 1) >>> 1);</code>. |
155 |
|
* |
156 |
|
* @param d the number to take the absolute value of |
157 |
|
* @return the absolute value |
158 |
*/ |
*/ |
159 |
public static double abs (double a) |
public static double abs(double dn) |
160 |
{ |
{ |
161 |
// avoid method call overhead, but treat -0.0 correctly |
return (d <= 0) ? 0 - d : d; |
|
// return Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1); |
|
|
return (a <= 0) ? 0 - a : a; |
|
162 |
} |
} |
163 |
|
|
164 |
/** |
/** |
165 |
* Return whichever argument is smaller. |
* Return whichever argument is smaller. |
166 |
|
* |
167 |
* @param a the first number |
* @param a the first number |
168 |
* @param b a second number |
* @param b a second number |
169 |
* @return the smaller of the two numbers. |
* @return the smaller of the two numbers |
170 |
*/ |
*/ |
171 |
public static int min (int a, int b) |
public static int min(int a, int b) |
172 |
{ |
{ |
173 |
return (a < b) ? a : b; |
return (a < b) ? a : b; |
174 |
} |
} |
175 |
|
|
176 |
/** |
/** |
177 |
* Return whichever argument is smaller. |
* Return whichever argument is smaller. |
178 |
|
* |
179 |
* @param a the first number |
* @param a the first number |
180 |
* @param b a second number |
* @param b a second number |
181 |
* @return the smaller of the two numbers. |
* @return the smaller of the two numbers |
182 |
*/ |
*/ |
183 |
public static long min (long a, long b) |
public static long min(long a, long b) |
184 |
{ |
{ |
185 |
return (a < b) ? a : b; |
return (a < b) ? a : b; |
186 |
} |
} |
187 |
|
|
188 |
/** |
/** |
|
* Return whichever argument is smaller. |
|
189 |
* Return whichever argument is smaller. If either argument is NaN, the |
* Return whichever argument is smaller. If either argument is NaN, the |
190 |
* result is NaN, and when comparing 0 and -0, -0 is always smaller. |
* result is NaN, and when comparing 0 and -0, -0 is always smaller. |
191 |
* |
* |
192 |
* @param a the first number |
* @param a the first number |
193 |
* @param b a second number |
* @param b a second number |
194 |
* @return the smaller of the two numbers. |
* @return the smaller of the two numbers |
195 |
*/ |
*/ |
196 |
public static float min (float a, float b) |
public static float min(float a, float b) |
197 |
{ |
{ |
198 |
// 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 |
199 |
if (a != a) |
if (a != a) |
211 |
* |
* |
212 |
* @param a the first number |
* @param a the first number |
213 |
* @param b a second number |
* @param b a second number |
214 |
* @return the smaller of the two numbers. |
* @return the smaller of the two numbers |
215 |
*/ |
*/ |
216 |
public static double min (double a, double b) |
public static double min(double a, double b) |
217 |
{ |
{ |
218 |
// 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 |
219 |
if (a != a) |
if (a != a) |
227 |
|
|
228 |
/** |
/** |
229 |
* Return whichever argument is larger. |
* Return whichever argument is larger. |
230 |
|
* |
231 |
* @param a the first number |
* @param a the first number |
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 int max (int a, int b) |
public static int max(int a, int b) |
236 |
{ |
{ |
237 |
return (a > b) ? a : b; |
return (a > b) ? a : b; |
238 |
} |
} |
239 |
|
|
240 |
/** |
/** |
241 |
* Return whichever argument is larger. |
* Return whichever argument is larger. |
242 |
|
* |
243 |
* @param a the first number |
* @param a the first number |
244 |
* @param b a second number |
* @param b a second number |
245 |
* @return the larger of the two numbers. |
* @return the larger of the two numbers |
246 |
*/ |
*/ |
247 |
public static long max (long a, long b) |
public static long max(long a, long b) |
248 |
{ |
{ |
249 |
return (a > b) ? a : b; |
return (a > b) ? a : b; |
250 |
} |
} |
255 |
* |
* |
256 |
* @param a the first number |
* @param a the first number |
257 |
* @param b a second number |
* @param b a second number |
258 |
* @return the larger of the two numbers. |
* @return the larger of the two numbers |
259 |
*/ |
*/ |
260 |
public static float max (float a, float b) |
public static float max(float a, float b) |
261 |
{ |
{ |
262 |
// 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 |
263 |
if (a != a) |
if (a != a) |
275 |
* |
* |
276 |
* @param a the first number |
* @param a the first number |
277 |
* @param b a second number |
* @param b a second number |
278 |
* @return the larger of the two numbers. |
* @return the larger of the two numbers |
279 |
*/ |
*/ |
280 |
public static double max (double a, double b) |
public static double max(double a, double b) |
281 |
{ |
{ |
282 |
// 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 |
283 |
if (a != a) |
if (a != a) |
290 |
} |
} |
291 |
|
|
292 |
/** |
/** |
293 |
* The trigonometric function <em>sin</em>. |
* The trigonometric function <em>sin</em>. The sine of NaN or infinity is |
294 |
* @param a the angle (in radians). |
* NaN, and the sine of 0 retains its sign. This is accurate within 1 ulp, |
295 |
* @return sin(a). |
* and is semi-monotonic. |
296 |
*/ |
* |
297 |
public native static double sin (double a); |
* @param a the angle (in radians) |
298 |
|
* @return sin(a) |
299 |
/** |
*/ |
300 |
* The trigonometric function <em>cos</em>. |
public native static double sin(double a); |
301 |
* @param a the angle (in radians). |
|
302 |
* @return cos(a). |
/** |
303 |
*/ |
* The trigonometric function <em>cos</em>. The cosine of NaN or infinity is |
304 |
public native static double cos (double a); |
* NaN. This is accurate within 1 ulp, and is semi-monotonic. |
305 |
|
* |
306 |
/** |
* @param a the angle (in radians) |
307 |
* The trigonometric function <em>tan</em>. |
* @return cos(a) |
308 |
* @param a the angle (in radians). |
*/ |
309 |
* @return tan(a). |
public native static double cos(double a); |
310 |
*/ |
|
311 |
public native static double tan (double a); |
/** |
312 |
|
* The trigonometric function <em>tan</em>. The tangent of NaN or infinity |
313 |
/** |
* is NaN, and the tangent of 0 retains its sign. This is accurate within 1 |
314 |
* The trigonometric function <em>arcsin</em>. |
* ulp, and is semi-monotonic. |
315 |
* The range of angles you will get are from -pi/2 to pi/2 radians (-90 to 90 degrees) |
* |
316 |
* @param a the sin to turn back into an angle. |
* @param a the angle (in radians) |
317 |
* @return arcsin(a). |
* @return tan(a) |
318 |
*/ |
*/ |
319 |
public native static double asin (double a); |
public native static double tan(double a); |
320 |
|
|
321 |
/** |
/** |
322 |
* The trigonometric function <em>arccos</em>. |
* The trigonometric function <em>arcsin</em>. The range of angles returned |
323 |
* The range of angles you will get are from 0 to pi radians (0 to 180 degrees). |
* is -pi/2 to pi/2 radians (-90 to 90 degrees). If the argument is NaN or |
324 |
* @param a the cos to turn back into an angle. |
* its absolute value is beyond 1, the result is NaN; and the arcsine of |
325 |
* @return arccos(a). |
* 0 retains its sign. This is accurate within 1 ulp, and is semi-monotonic. |
326 |
*/ |
* |
327 |
public native static double acos (double a); |
* @param a the sin to turn back into an angle |
328 |
|
* @return arcsin(a) |
329 |
/** |
*/ |
330 |
* The trigonometric function <em>arctan</em>. |
public native static double asin(double a); |
331 |
* The range of angles you will get are from -pi/2 to pi/2 radians (-90 to 90 degrees) |
|
332 |
* @param a the sin to turn back into an angle. |
/** |
333 |
* @return arcsin(a). |
* The trigonometric function <em>arccos</em>. The range of angles returned |
334 |
* @see #atan(double,double) |
* is 0 to pi radians (0 to 180 degrees). If the argument is NaN or |
335 |
*/ |
* its absolute value is beyond 1, the result is NaN. This is accurate |
336 |
public native static double atan (double a); |
* within 1 ulp, and is semi-monotonic. |
337 |
|
* |
338 |
/** |
* @param a the cos to turn back into an angle |
339 |
* A special version of the trigonometric function <em>arctan</em>. |
* @return arccos(a) |
340 |
* Given a position (x,y), this function will give you the angle of |
*/ |
341 |
* that position. |
public native static double acos(double a); |
342 |
* The range of angles you will get are from -pi to pi radians (-180 to 180 degrees), |
|
343 |
* the whole spectrum of angles. That is what makes this function so |
/** |
344 |
* much more useful than the other <code>atan()</code>. |
* The trigonometric function <em>arcsin</em>. The range of angles returned |
345 |
|
* is -pi/2 to pi/2 radians (-90 to 90 degrees). If the argument is NaN, the |
346 |
|
* result is NaN; and the arctangent of 0 retains its sign. This is accurate |
347 |
|
* within 1 ulp, and is semi-monotonic. |
348 |
|
* |
349 |
|
* @param a the tan to turn back into an angle |
350 |
|
* @return arcsin(a) |
351 |
|
* @see #atan2(double, double) |
352 |
|
*/ |
353 |
|
public native static double atan(double a); |
354 |
|
|
355 |
|
/** |
356 |
|
* A special version of the trigonometric function <em>arctan</em>, for |
357 |
|
* converting rectangular coordinates <em>(x, y)</em> to polar |
358 |
|
* <em>(r, theta)</em>. This computes the arctangent of x/y in the range |
359 |
|
* of -pi to pi radians (-180 to 180 degrees). Special cases:<ul> |
360 |
|
* <li>If either argument is NaN, the result is NaN.</li> |
361 |
|
* <li>If the first argument is positive zero and the second argument is |
362 |
|
* positive, or the first argument is positive and finite and the second |
363 |
|
* argument is positive infinity, then the result is positive zero.</li> |
364 |
|
* <li>If the first argument is negative zero and the second argument is |
365 |
|
* positive, or the first argument is negative and finite and the second |
366 |
|
* argument is positive infinity, then the result is negative zero.</li> |
367 |
|
* <li>If the first argument is positive zero and the second argument is |
368 |
|
* negative, or the first argument is positive and finite and the second |
369 |
|
* argument is negative infinity, then the result is the double value |
370 |
|
* closest to pi.</li> |
371 |
|
* <li>If the first argument is negative zero and the second argument is |
372 |
|
* negative, or the first argument is negative and finite and the second |
373 |
|
* argument is negative infinity, then the result is the double value |
374 |
|
* closest to -pi.</li> |
375 |
|
* <li>If the first argument is positive and the second argument is |
376 |
|
* positive zero or negative zero, or the first argument is positive |
377 |
|
* infinity and the second argument is finite, then the result is the |
378 |
|
* double value closest to pi/2.</li> |
379 |
|
* <li>If the first argument is negative and the second argument is |
380 |
|
* positive zero or negative zero, or the first argument is negative |
381 |
|
* infinity and the second argument is finite, then the result is the |
382 |
|
* double value closest to -pi/2.</li> |
383 |
|
* <li>If both arguments are positive infinity, then the result is the |
384 |
|
* double value closest to pi/4.</li> |
385 |
|
* <li>If the first argument is positive infinity and the second argument |
386 |
|
* is negative infinity, then the result is the double value closest to |
387 |
|
* 3*pi/4.</li> |
388 |
|
* <li>If the first argument is negative infinity and the second argument |
389 |
|
* is positive infinity, then the result is the double value closest to |
390 |
|
* -pi/4.</li> |
391 |
|
* <li>If both arguments are negative infinity, then the result is the |
392 |
|
* double value closest to -3*pi/4.</li> |
393 |
|
* |
394 |
|
* </ul><p>This is accurate within 2 ulps, and is semi-monotonic. To get r, |
395 |
|
* use sqrt(x*x+y*y). |
396 |
|
* |
397 |
* @param y the y position |
* @param y the y position |
398 |
* @param x the x position |
* @param x the x position |
399 |
* @return arcsin(a). |
* @return <em>theta</em> in the conversion of (x, y) to (r, theta) |
400 |
* @see #atan(double) |
* @see #atan(double) |
401 |
*/ |
*/ |
402 |
public native static double atan2 (double y, double x); |
public native static double atan2(double y, double x); |
403 |
|
|
404 |
/** |
/** |
405 |
* 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>. If the |
406 |
* @param a the number to raise to the power. |
* argument is NaN, the result is NaN; if the argument is positive infinity, |
407 |
* @return the number raised to the power of <em>e</em>. |
* the result is positive infinity; and if the argument is negative |
408 |
|
* infinity, the result is positive zero. This is accurate within 1 ulp, |
409 |
|
* and is semi-monotonic. |
410 |
|
* |
411 |
|
* @param a the number to raise to the power |
412 |
|
* @return the number raised to the power of <em>e</em> |
413 |
* @see #log(double) |
* @see #log(double) |
414 |
* @see #pow(double,double) |
* @see #pow(double, double) |
415 |
*/ |
*/ |
416 |
public native static double exp (double a); |
public native static double exp(double a); |
417 |
|
|
418 |
/** |
/** |
419 |
* Take ln(a) (the natural log). The opposite of <code>exp()</code>. |
* Take ln(a) (the natural log). The opposite of <code>exp()</code>. If the |
420 |
* Note that the way to get log<sub>b</sub>(a) is to do this: |
* argument is NaN or negative, the result is NaN; if the argument is |
421 |
|
* positive infinity, the result is positive infinity; and if the argument |
422 |
|
* is either zero, the result is negative infinity. This is accurate within |
423 |
|
* 1 ulp, and is semi-monotonic. |
424 |
|
* |
425 |
|
* <p>Note that the way to get log<sub>b</sub>(a) is to do this: |
426 |
* <code>ln(a) / ln(b)</code>. |
* <code>ln(a) / ln(b)</code>. |
427 |
* @param a the number to take the natural log of. |
* |
428 |
* @return the natural log of <code>a</code>. |
* @param a the number to take the natural log of |
429 |
|
* @return the natural log of <code>a</code> |
430 |
* @see #exp(double) |
* @see #exp(double) |
431 |
*/ |
*/ |
432 |
public native static double log (double a); |
public native static double log(double a); |
433 |
|
|
434 |
/** |
/** |
435 |
* Take a square root. |
* Take a square root. If the argument is NaN or negative, the result is |
436 |
* For other roots, to pow(a,1/rootNumber). |
* NaN; if the argument is positive infinity, the result is positive |
437 |
|
* infinity; and if the result is either zero, the result is the same. |
438 |
|
* This is accurate within the limits of doubles. |
439 |
|
* |
440 |
|
* <p>For other roots, use pow(a, 1 / rootNumber). |
441 |
|
* |
442 |
* @param a the numeric argument |
* @param a the numeric argument |
443 |
* @return the square root of the argument. |
* @return the square root of the argument |
444 |
* @see #pow(double,double) |
* @see #pow(double, double) |
|
*/ |
|
|
public native static double sqrt (double a); |
|
|
|
|
|
/** |
|
|
* Take a number to a power. |
|
|
* @param a the number to raise. |
|
|
* @param b the power to raise it to. |
|
|
* @return a<sup>b</sup>. |
|
445 |
*/ |
*/ |
446 |
public native static double pow (double a, double b); |
public native static double sqrt(double a); |
447 |
|
|
448 |
/** |
/** |
449 |
* Get the floating point remainder on two numbers, |
* Raise a number to a power. Special cases:<ul> |
450 |
* which really does the following: |
* <li>If the second argument is positive or negative zero, then the result |
451 |
* <P> |
* is 1.0.</li> |
452 |
* |
* <li>If the second argument is 1.0, then the result is the same as the |
453 |
* <OL> |
* first argument.</li> |
454 |
* <LI> |
* <li>If the second argument is NaN, then the result is NaN.</li> |
455 |
* Takes x/y and finds the nearest integer <em>n</em> to the |
* <li>If the first argument is NaN and the second argument is nonzero, |
456 |
* quotient. (Uses the <code>rint()</code> function to do this. |
* then the result is NaN.</li> |
457 |
* </LI> |
* <li>If the absolute value of the first argument is greater than 1 and |
458 |
* <LI> |
* the second argument is positive infinity, or the absolute value of the |
459 |
* Takes x - y*<em>n</em>. |
* first argument is less than 1 and the second argument is negative |
460 |
* </LI> |
* infinity, then the result is positive infinity.</li> |
461 |
* <LI> |
* <li>If the absolute value of the first argument is greater than 1 and |
462 |
* If x = y*n, then the result is 0 if x is positive and -0 if x |
* the second argument is negative infinity, or the absolute value of the |
463 |
* is negative. |
* first argument is less than 1 and the second argument is positive |
464 |
* </LI> |
* infinity, then the result is positive zero.</li> |
465 |
* </OL> |
* <li>If the absolute value of the first argument equals 1 and the second |
466 |
|
* argument is infinite, then the result is NaN.</li> |
467 |
|
* <li>If the first argument is positive zero and the second argument is |
468 |
|
* greater than zero, or the first argument is positive infinity and the |
469 |
|
* second argument is less than zero, then the result is positive zero.</li> |
470 |
|
* <li>If the first argument is positive zero and the second argument is |
471 |
|
* less than zero, or the first argument is positive infinity and the |
472 |
|
* second argument is greater than zero, then the result is positive |
473 |
|
* infinity.</li> |
474 |
|
* <li>If the first argument is negative zero and the second argument is |
475 |
|
* greater than zero but not a finite odd integer, or the first argument is |
476 |
|
* negative infinity and the second argument is less than zero but not a |
477 |
|
* finite odd integer, then the result is positive zero.</li> |
478 |
|
* <li>If the first argument is negative zero and the second argument is a |
479 |
|
* positive finite odd integer, or the first argument is negative infinity |
480 |
|
* and the second argument is a negative finite odd integer, then the result |
481 |
|
* is negative zero.</li> |
482 |
|
* <li>If the first argument is negative zero and the second argument is |
483 |
|
* less than zero but not a finite odd integer, or the first argument is |
484 |
|
* negative infinity and the second argument is greater than zero but not a |
485 |
|
* finite odd integer, then the result is positive infinity.</li> |
486 |
|
* <li>If the first argument is negative zero and the second argument is a |
487 |
|
* negative finite odd integer, or the first argument is negative infinity |
488 |
|
* and the second argument is a positive finite odd integer, then the result |
489 |
|
* is negative infinity.</li> |
490 |
|
* <li>If the first argument is less than zero and the second argument is a |
491 |
|
* finite even integer, then the result is equal to the result of raising |
492 |
|
* the absolute value of the first argument to the power of the second |
493 |
|
* argument.</li> |
494 |
|
* <li>If the first argument is less than zero and the second argument is a |
495 |
|
* finite odd integer, then the result is equal to the negative of the |
496 |
|
* result of raising the absolute value of the first argument to the power |
497 |
|
* of the second argument.</li> |
498 |
|
* <li>If the first argument is finite and less than zero and the second |
499 |
|
* argument is finite and not an integer, then the result is NaN.</li> |
500 |
|
* <li>If both arguments are integers, then the result is exactly equal to |
501 |
|
* the mathematical result of raising the first argument to the power of |
502 |
|
* the second argument if that result can in fact be represented exactly as |
503 |
|
* a double value.</li> |
504 |
|
* |
505 |
|
* </ul><p>(In the foregoing descriptions, a floating-point value is |
506 |
|
* considered to be an integer if and only if it is a fixed point of the |
507 |
|
* method {@link #ceil(double)} or, equivalently, a fixed point of the |
508 |
|
* method {@link #floor(double)}. A value is a fixed point of a one-argument |
509 |
|
* method if and only if the result of applying the method to the value is |
510 |
|
* equal to the value.) This is accurate within 1 ulp, and is semi-monotonic. |
511 |
|
* |
512 |
|
* @param a the number to raise |
513 |
|
* @param b the power to raise it to |
514 |
|
* @return a<sup>b</sup> |
515 |
|
*/ |
516 |
|
public native static double pow(double a, double b); |
517 |
|
|
518 |
|
/** |
519 |
|
* Get the IEEE 754 floating point remainder on two numbers. This is the |
520 |
|
* value of <code>x - y * <em>n</em></code>, where <em>n</em> is the closest |
521 |
|
* double to <code>x / y</code> (ties go to the even n); for a zero |
522 |
|
* remainder, the sign is that of <code>x</code>. If either argument is NaN, |
523 |
|
* the first argument is infinite, or the second argument is zero, the result |
524 |
|
* is NaN; if x is finite but y is infinte, the result is x. This is |
525 |
|
* accurate within the limits of doubles. |
526 |
* |
* |
527 |
* @param x the dividend (the top half) |
* @param x the dividend (the top half) |
528 |
* @param y the divisor (the bottom half) |
* @param y the divisor (the bottom half) |
529 |
* @return the IEEE 754-defined floating point remainder of x/y. |
* @return the IEEE 754-defined floating point remainder of x/y |
530 |
* @see #rint(double) |
* @see #rint(double) |
531 |
*/ |
*/ |
532 |
public native static double IEEEremainder (double x, double y); |
public native static double IEEEremainder(double x, double y); |
533 |
|
|
534 |
/** |
/** |
535 |
* 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 |
536 |
* argument. |
* argument. If the argument is NaN, infinite, or zero, the result is the |
537 |
* @param a the value to act upon. |
* same; if the argument is between -1 and 0, the result is negative zero. |
538 |
* @return the nearest integer >= <code>a</code>. |
* Note that <code>Math.ceil(x) == -Math.floor(-x)</code>. |
539 |
|
* |
540 |
|
* @param a the value to act upon |
541 |
|
* @return the nearest integer >= <code>a</code> |
542 |
*/ |
*/ |
543 |
public native static double ceil (double a); |
public native static double ceil(double a); |
544 |
|
|
545 |
/** |
/** |
546 |
* 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 |
547 |
* argument. |
* argument. If the argument is NaN, infinite, or zero, the result is the |
548 |
* @param a the value to act upon. |
* same. Note that <code>Math.ceil(x) == -Math.floor(-x)</code>. |
549 |
* @return the nearest integer <= <code>a</code>. |
* |
550 |
|
* @param a the value to act upon |
551 |
|
* @return the nearest integer <= <code>a</code> |
552 |
*/ |
*/ |
553 |
public native static double floor (double a); |
public native static double floor(double a); |
554 |
|
|
555 |
/** |
/** |
556 |
* Take the nearest integer to the argument. If it is exactly between |
* Take the nearest integer to the argument. If it is exactly between |
557 |
* two integers, the even integer is taken. |
* two integers, the even integer is taken. If the argument is NaN, |
558 |
* @param a the value to act upon. |
* infinite, or zero, the result is the same. |
559 |
* @return the nearest integer to <code>a</code>. |
* |
560 |
|
* @param a the value to act upon |
561 |
|
* @return the nearest integer to <code>a</code> |
562 |
*/ |
*/ |
563 |
public native static double rint (double a); |
public native static double rint(double a); |
564 |
|
|
565 |
/** |
/** |
566 |
* Take the nearest integer to the argument. If it is exactly between |
* Take the nearest integer to the argument. This is equivalent to |
567 |
* two integers, then the lower of the two (-10 lower than -9) is taken. |
* <code>(int) Math.floor(a + 0.5f). If the argument is NaN, the result |
568 |
* If the argument is less than Integer.MIN_VALUE or negative infinity, |
* is 0; otherwise if the argument is outside the range of int, the result |
569 |
* Integer.MIN_VALUE will be returned. If the argument is greater than |
* will be Integer.MIN_VALUE or Integer.MAX_VALUE, as appropriate. |
570 |
* Integer.MAX_VALUE, Integer.MAX_VALUE will be returned. |
* |
571 |
* |
* @param a the argument to round |
572 |
* @param a the argument to round. |
* @return the nearest integer to the argument |
573 |
* @return the nearest integer to the argument. |
* @see Integer#MIN_VALUE |
574 |
* @see java.lang.Integer#MIN_VALUE |
* @see Integer#MAX_VALUE |
|
* @see java.lang.Integer#MAX_VALUE |
|
575 |
*/ |
*/ |
576 |
public static int round (float a) |
public static int round(float a) |
577 |
{ |
{ |
578 |
return (int) floor (a + 0.5f); |
return (int) floor(a + 0.5f); |
579 |
} |
} |
580 |
|
|
581 |
/** |
/** |
582 |
* Take the nearest integer to the argument. If it is exactly between |
* Take the nearest long to the argument. This is equivalent to |
583 |
* two integers, then the lower of the two (-10 lower than -9) is taken. |
* <code>(long) Math.floor(a + 0.5)</code>. If the argument is NaN, the |
584 |
* If the argument is less than Long.MIN_VALUE or negative infinity, |
* result is 0; otherwise if the argument is outside the range of long, the |
585 |
* Long.MIN_VALUE will be returned. If the argument is greater than |
* result will be Long.MIN_VALUE or Long.MAX_VALUE, as appropriate. |
|
* Long.MAX_VALUE, Long.MAX_VALUE will be returned. |
|
586 |
* |
* |
587 |
* @param a the argument to round. |
* @param a the argument to round |
588 |
* @return the nearest integer to the argument. |
* @return the nearest long to the argument |
589 |
* @see java.lang.Long#MIN_VALUE |
* @see Long#MIN_VALUE |
590 |
* @see java.lang.Long#MAX_VALUE |
* @see Long#MAX_VALUE |
591 |
*/ |
*/ |
592 |
public static long round (double a) |
public static long round(double a) |
593 |
{ |
{ |
594 |
return (long) floor (a + 0.5d); |
return (long) floor(a + 0.5d); |
595 |
} |
} |
596 |
|
|
597 |
/** |
/** |
598 |
* Get a random number. This behaves like Random.nextDouble(). |
* Get a random number. This behaves like Random.nextDouble(), seeded by |
599 |
* @return a random number. |
* System.currentTimeMillis() when first called. In other words, the number |
600 |
* @see java.lang.Random#nextDouble() |
* is from a pseudorandom sequence, and lies in the range [+0.0, 1.0). |
601 |
|
* This random sequence is only used by this method, and is threadsafe, |
602 |
|
* although you may want your own random number generator if it is shared |
603 |
|
* among threads. |
604 |
|
* |
605 |
|
* @return a random number |
606 |
|
* @see Random#nextDouble() |
607 |
|
* @see System#currentTimeMillis() |
608 |
*/ |
*/ |
609 |
public static synchronized double random () |
public static synchronized double random() |
610 |
{ |
{ |
611 |
if (rand == null) |
if (rand == null) |
612 |
rand = new Random (); |
rand = new Random(); |
613 |
return rand.nextDouble (); |
return rand.nextDouble(); |
614 |
} |
} |
615 |
|
|
616 |
/** |
/** |
617 |
* Convert from degrees to radians. |
* Convert from degrees to radians. The formula for this is |
618 |
* The formula for this is radians = degrees * (pi/180). |
* radians = degrees * (pi/180); however it is not always exact given the |
619 |
|
* limitations of floating point numbers. |
620 |
|
* |
621 |
* @param degrees an angle in degrees |
* @param degrees an angle in degrees |
622 |
* @return the angle in radians |
* @return the angle in radians |
623 |
|
* @since 1.2 |
624 |
*/ |
*/ |
625 |
public static double toRadians (double degrees) |
public static double toRadians(double degrees) |
626 |
{ |
{ |
627 |
return degrees * 0.017453292519943295; /* (degrees * (PI/180)) */ |
return degrees * (PI / 180); |
628 |
} |
} |
629 |
|
|
630 |
/** |
/** |
631 |
* Convert from radians to degrees. |
* Convert from radians to degrees. The formula for this is |
632 |
* The formula for this is degrees = radians * (180/pi). |
* degrees = radians * (180/pi); however it is not always exact given the |
633 |
|
* limitations of floating point numbers. |
634 |
|
* |
635 |
* @param rads an angle in radians |
* @param rads an angle in radians |
636 |
* @return the angle in degrees |
* @return the angle in degrees |
637 |
|
* @since 1.2 |
638 |
*/ |
*/ |
639 |
public static double toDegrees (double rads) |
public static double toDegrees(double rads) |
640 |
{ |
{ |
641 |
return rads / 0.017453292519943295; /* (rads / (PI/180)) */ |
return rads * (180 / PI); |
642 |
} |
} |
643 |
} |
} |