001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.lang;
019:
020: import java.util.Random;
021:
022: /**
023: * Class StrictMath provides various numeric operations using the standards set
024: * by the known "Freely Distributable Math Library" (fdlibm). The standard is
025: * set by the January 4th, 1995 version of the library.
026: */
027: public final class StrictMath {
028:
029: /**
030: * Standard math constant
031: */
032: public final static double E = Math.E;
033:
034: /**
035: * Standard math constant
036: */
037: public final static double PI = Math.PI;
038:
039: private static java.util.Random random;
040:
041: /**
042: * Prevents this class from being instantiated.
043: */
044: private StrictMath() {
045: }
046:
047: /**
048: * Answers the absolute value of the argument.
049: *
050: * @param d
051: * the value to be converted
052: * @return the argument if it is positive, otherwise the negation of the
053: * argument.
054: */
055: public static double abs(double d) {
056: long bits = Double.doubleToLongBits(d);
057: bits &= 0x7fffffffffffffffL;
058: return Double.longBitsToDouble(bits);
059: }
060:
061: /**
062: * Answers the absolute value of the argument.
063: *
064: * @param f
065: * the value to be converted
066: * @return the argument if it is positive, otherwise the negation of the
067: * argument.
068: */
069: public static float abs(float f) {
070: int bits = Float.floatToIntBits(f);
071: bits &= 0x7fffffff;
072: return Float.intBitsToFloat(bits);
073: }
074:
075: /**
076: * Answers the absolute value of the argument.
077: *
078: * @param i
079: * the value to be converted
080: * @return the argument if it is positive, otherwise the negation of the
081: * argument.
082: */
083: public static int abs(int i) {
084: return i >= 0 ? i : -i;
085: }
086:
087: /**
088: * Answers the absolute value of the argument.
089: *
090: * @param l
091: * the value to be converted
092: * @return the argument if it is positive, otherwise the negation of the
093: * argument.
094: */
095: public static long abs(long l) {
096: return l >= 0 ? l : -l;
097: }
098:
099: /**
100: * Answers the closest double approximation of the arc cosine of the
101: * argument
102: *
103: * @param d
104: * the value to compute acos of
105: * @return the arc cosine of the argument.
106: */
107: public static native double acos(double d);
108:
109: /**
110: * Answers the closest double approximation of the arc sine of the argument
111: *
112: * @param d
113: * the value to compute asin of
114: * @return the arc sine of the argument.
115: */
116: public static native double asin(double d);
117:
118: /**
119: * Answers the closest double approximation of the arc tangent of the
120: * argument
121: *
122: * @param d
123: * the value to compute atan of
124: * @return the arc tangent of the argument.
125: */
126: public static native double atan(double d);
127:
128: /**
129: * Answers the closest double approximation of the arc tangent of the result
130: * of dividing the first argument by the second argument.
131: *
132: * @param d1
133: * the numerator of the value to compute atan of
134: * @param d2
135: * the denominator of the value to compute atan of
136: * @return the arc tangent of d1/d2.
137: */
138: public static native double atan2(double d1, double d2);
139:
140: /**
141: * Answers the closest double approximation of the cube root of the
142: * argument.
143: *
144: * @param d
145: * the value to compute cube root of
146: * @return the cube root of the argument.
147: */
148: public static native double cbrt(double d);
149:
150: /**
151: * Answers the double conversion of the most negative (i.e. closest to
152: * negative infinity) integer value which is greater than the argument.
153: *
154: * @param d
155: * the value to be converted
156: * @return the ceiling of the argument.
157: */
158: public static native double ceil(double d);
159:
160: /**
161: * Answers the closest double approximation of the hyperbolic cosine of the
162: * argument.
163: *
164: * @param d
165: * the value to compute hyperbolic cosine of
166: * @return the hyperbolic cosine of the argument.
167: */
168: public static native double cosh(double d);
169:
170: /**
171: * Answers the closest double approximation of the cosine of the argument
172: *
173: * @param d
174: * the value to compute cos of
175: * @return the cosine of the argument.
176: */
177: public static native double cos(double d);
178:
179: /**
180: * Answers the closest double approximation of the raising "e" to the power
181: * of the argument
182: *
183: * @param d
184: * the value to compute the exponential of
185: * @return the exponential of the argument.
186: */
187: public static native double exp(double d);
188:
189: /**
190: * Answers the closest double approximation of <i>e</i><sup>d</sup> - 1.
191: * If the argument is very close to 0, it is much more accurate to use
192: * expm1(d)+1 than exp(d).
193: *
194: * @param d
195: * the value to compute the <i>e</i><sup>d</sup> - 1 of
196: * @return the <i>e</i><sup>d</sup> - 1 value of the argument.
197: */
198: public static native double expm1(double d);
199:
200: /**
201: * Answers the double conversion of the most positive (i.e. closest to
202: * positive infinity) integer value which is less than the argument.
203: *
204: *
205: * @param d
206: * the value to be converted
207: * @return the ceiling of the argument.
208: */
209: public static native double floor(double d);
210:
211: /**
212: * Answers sqrt(<i>x</i><sup>2</sup>+<i>y</i><sup>2</sup>). The
213: * final result is without medium underflow or overflow.
214: *
215: * @param x
216: * a double number
217: * @param y
218: * a double number
219: * @return the sqrt(<i>x</i><sup>2</sup>+<i>y</i><sup>2</sup>) value
220: * of the arguments.
221: */
222: public static native double hypot(double x, double y);
223:
224: /**
225: * Answers the remainder of dividing the first argument by the second using
226: * the IEEE 754 rules.
227: *
228: * @param d1
229: * the numerator of the operation
230: * @param d2
231: * the denominator of the operation
232: * @return the result of d1/d2.
233: */
234: public static native double IEEEremainder(double d1, double d2);
235:
236: /**
237: * Answers the closest double approximation of the natural logarithm of the
238: * argument
239: *
240: * @param d
241: * the value to compute the log of
242: * @return the natural logarithm of the argument.
243: */
244: public static native double log(double d);
245:
246: /**
247: * Answers the logarithm of the argument and the base is 10.
248: *
249: * @param d
250: * the value to compute the base 10 log of
251: * @return the base 10 logarithm of the argument.
252: */
253: public static native double log10(double d);
254:
255: /**
256: * Answers the closest double approximation of the natural logarithm of the
257: * sum of the argument and 1. If the argument is very close to 0, it is much
258: * more accurate to use log1p(d) than log(1.0+d).
259: *
260: * @param d
261: * the value to compute the ln(1+d) of
262: * @return the natural logarithm of the sum of the argument and 1.
263: */
264: public static native double log1p(double d);
265:
266: /**
267: * Answers the most positive (i.e. closest to positive infinity) of the two
268: * arguments.
269: *
270: * @param d1
271: * the first argument to check
272: * @param d2
273: * the second argument
274: * @return the larger of d1 and d2.
275: */
276: public static double max(double d1, double d2) {
277: if (d1 > d2)
278: return d1;
279: if (d1 < d2)
280: return d2;
281: /* if either arg is NaN, return NaN */
282: if (d1 != d2)
283: return Double.NaN;
284: /* max( +0.0,-0.0) == +0.0 */
285: if (d1 == 0.0
286: && ((Double.doubleToLongBits(d1) & Double
287: .doubleToLongBits(d2)) & 0x8000000000000000L) == 0)
288: return 0.0;
289: return d1;
290: }
291:
292: /**
293: * Answers the most positive (i.e. closest to positive infinity) of the two
294: * arguments.
295: *
296: * @param f1
297: * the first argument to check
298: * @param f2
299: * the second argument
300: * @return the larger of f1 and f2.
301: */
302: public static float max(float f1, float f2) {
303: if (f1 > f2)
304: return f1;
305: if (f1 < f2)
306: return f2;
307: /* if either arg is NaN, return NaN */
308: if (f1 != f2)
309: return Float.NaN;
310: /* max( +0.0,-0.0) == +0.0 */
311: if (f1 == 0.0f
312: && ((Float.floatToIntBits(f1) & Float
313: .floatToIntBits(f2)) & 0x80000000) == 0)
314: return 0.0f;
315: return f1;
316: }
317:
318: /**
319: * Answers the most positive (i.e. closest to positive infinity) of the two
320: * arguments.
321: *
322: * @param i1
323: * the first argument to check
324: * @param i2
325: * the second argument
326: * @return the larger of i1 and i2.
327: */
328: public static int max(int i1, int i2) {
329: return i1 > i2 ? i1 : i2;
330: }
331:
332: /**
333: * Answers the most positive (i.e. closest to positive infinity) of the two
334: * arguments.
335: *
336: * @param l1
337: * the first argument to check
338: * @param l2
339: * the second argument
340: * @return the larger of l1 and l2.
341: */
342: public static long max(long l1, long l2) {
343: return l1 > l2 ? l1 : l2;
344: }
345:
346: /**
347: * Answers the most negative (i.e. closest to negative infinity) of the two
348: * arguments.
349: *
350: * @param d1
351: * the first argument to check
352: * @param d2
353: * the second argument
354: * @return the smaller of d1 and d2.
355: */
356: public static double min(double d1, double d2) {
357: if (d1 > d2)
358: return d2;
359: if (d1 < d2)
360: return d1;
361: /* if either arg is NaN, return NaN */
362: if (d1 != d2)
363: return Double.NaN;
364: /* min( +0.0,-0.0) == -0.0 */
365: if (d1 == 0.0
366: && ((Double.doubleToLongBits(d1) | Double
367: .doubleToLongBits(d2)) & 0x8000000000000000l) != 0)
368: return 0.0 * (-1.0);
369: return d1;
370: }
371:
372: /**
373: * Answers the most negative (i.e. closest to negative infinity) of the two
374: * arguments.
375: *
376: * @param f1
377: * the first argument to check
378: * @param f2
379: * the second argument
380: * @return the smaller of f1 and f2.
381: */
382: public static float min(float f1, float f2) {
383: if (f1 > f2)
384: return f2;
385: if (f1 < f2)
386: return f1;
387: /* if either arg is NaN, return NaN */
388: if (f1 != f2)
389: return Float.NaN;
390: /* min( +0.0,-0.0) == -0.0 */
391: if (f1 == 0.0f
392: && ((Float.floatToIntBits(f1) | Float
393: .floatToIntBits(f2)) & 0x80000000) != 0)
394: return 0.0f * (-1.0f);
395: return f1;
396: }
397:
398: /**
399: * Answers the most negative (i.e. closest to negative infinity) of the two
400: * arguments.
401: *
402: * @param i1
403: * the first argument to check
404: * @param i2
405: * the second argument
406: * @return the smaller of i1 and i2.
407: */
408: public static int min(int i1, int i2) {
409: return i1 < i2 ? i1 : i2;
410: }
411:
412: /**
413: * Answers the most negative (i.e. closest to negative infinity) of the two
414: * arguments.
415: *
416: * @param l1
417: * the first argument to check
418: * @param l2
419: * the second argument
420: * @return the smaller of l1 and l2.
421: */
422: public static long min(long l1, long l2) {
423: return l1 < l2 ? l1 : l2;
424: }
425:
426: /**
427: * Answers the closest double approximation of the result of raising the
428: * first argument to the power of the second.
429: *
430: * @param d1
431: * the base of the operation.
432: * @param d2
433: * the exponent of the operation.
434: * @return d1 to the power of d2
435: */
436: public static native double pow(double d1, double d2);
437:
438: /**
439: * Returns a pseudo-random number between 0.0 and 1.0.
440: *
441: * @return a pseudo-random number
442: */
443: public static double random() {
444: if (random == null)
445: random = new Random();
446: return random.nextDouble();
447: }
448:
449: /**
450: * Answers the double conversion of the result of rounding the argument to
451: * an integer.
452: *
453: * @param d
454: * the value to be converted
455: * @return the closest integer to the argument (as a double).
456: */
457: public static native double rint(double d);
458:
459: /**
460: * Answers the result of rounding the argument to an integer.
461: *
462: * @param d
463: * the value to be converted
464: * @return the closest integer to the argument.
465: */
466: public static long round(double d) {
467: // check for NaN
468: if (d != d)
469: return 0L;
470: return (long) Math.floor(d + 0.5d);
471: }
472:
473: /**
474: * Answers the result of rounding the argument to an integer.
475: *
476: * @param f
477: * the value to be converted
478: * @return the closest integer to the argument.
479: */
480: public static int round(float f) {
481: // check for NaN
482: if (f != f)
483: return 0;
484: return (int) Math.floor(f + 0.5f);
485: }
486:
487: /**
488: * Answers the signum function of the argument. If the argument is less than
489: * zero, it answers -1.0. If greater than zero, 1.0 is returned. It returns
490: * zero if the argument is also zero.
491: *
492: * @param d
493: * the value to compute signum function of
494: * @return the value of the signum function.
495: */
496: public static double signum(double d) {
497: if (Double.isNaN(d)) {
498: return Double.NaN;
499: }
500: double sig = d;
501: if (d > 0) {
502: sig = 1.0;
503: } else if (d < 0) {
504: sig = -1.0;
505: }
506: return sig;
507: }
508:
509: /**
510: * Answers the signum function of the argument. If the argument is less than
511: * zero, it answers -1.0. If greater than zero, 1.0 is returned. It returns
512: * zero if the argument is also zero.
513: *
514: * @param f
515: * the value to compute signum function of
516: * @return the value of the signum function.
517: */
518: public static float signum(float f) {
519: if (Float.isNaN(f)) {
520: return Float.NaN;
521: }
522: float sig = f;
523: if (f > 0) {
524: sig = 1.0f;
525: } else if (f < 0) {
526: sig = -1.0f;
527: }
528: return sig;
529: }
530:
531: /**
532: * Answers the closest double approximation of the hyperbolic sine of the
533: * argument.
534: *
535: * @param d
536: * the value to compute hyperbolic sine of
537: * @return the hyperbolic sine of the argument.
538: */
539: public static native double sinh(double d);
540:
541: /**
542: * Answers the closest double approximation of the sine of the argument
543: *
544: * @param d
545: * the value to compute sin of
546: * @return the sine of the argument.
547: */
548: public static native double sin(double d);
549:
550: /**
551: * Answers the closest double approximation of the square root of the
552: * argument
553: *
554: * @param d
555: * the value to compute sqrt of
556: * @return the square root of the argument.
557: */
558: public static native double sqrt(double d);
559:
560: /**
561: * Answers the closest double approximation of the tangent of the argument
562: *
563: * @param d
564: * the value to compute tan of
565: * @return the tangent of the argument.
566: */
567: public static native double tan(double d);
568:
569: /**
570: * Answers the closest double approximation of the hyperbolic tangent of the
571: * argument. The absolute value is always less than 1.
572: *
573: * @param d
574: * the value to compute hyperbolic tangent of
575: * @return the hyperbolic tangent of the argument.
576: */
577: public static native double tanh(double d);
578:
579: /**
580: * Returns the measure in degrees of the supplied radian angle
581: *
582: * @param angrad
583: * an angle in radians
584: * @return the degree measure of the angle.
585: */
586: public static double toDegrees(double angrad) {
587: return angrad * 180d / PI;
588: }
589:
590: /**
591: * Returns the measure in radians of the supplied degree angle
592: *
593: * @param angdeg
594: * an angle in degrees
595: * @return the radian measure of the angle.
596: */
597: public static double toRadians(double angdeg) {
598: return angdeg / 180d * PI;
599: }
600:
601: /**
602: * Answers the argument's ulp. The size of a ulp of a double value is the
603: * positive distance between this value and the double value next larger
604: * in magnitude. For non-NaN x, ulp(-x) == ulp(x).
605: *
606: * @param d
607: * the floating-point value to compute ulp of
608: * @return the size of a ulp of the argument.
609: */
610: public static double ulp(double d) {
611: // special cases
612: if (Double.isInfinite(d)) {
613: return Double.POSITIVE_INFINITY;
614: } else if (d == Double.MAX_VALUE || d == -Double.MAX_VALUE) {
615: return pow(2, 971);
616: }
617: d = Math.abs(d);
618: return nextafter(d, Double.MAX_VALUE) - d;
619: }
620:
621: /**
622: * Answers the argument's ulp. The size of a ulp of a float value is the
623: * positive distance between this value and the float value next larger
624: * in magnitude. For non-NaN x, ulp(-x) == ulp(x).
625: *
626: * @param f
627: * the floating-point value to compute ulp of
628: * @return the size of a ulp of the argument.
629: */
630: public static float ulp(float f) {
631: // special cases
632: if (Float.isNaN(f)) {
633: return Float.NaN;
634: } else if (Float.isInfinite(f)) {
635: return Float.POSITIVE_INFINITY;
636: } else if (f == Float.MAX_VALUE || f == -Float.MAX_VALUE) {
637: return (float) pow(2, 104);
638: }
639: f = Math.abs(f);
640: return nextafterf(f, Float.MAX_VALUE) - f;
641: }
642:
643: private native static double nextafter(double x, double y);
644:
645: private native static float nextafterf(float x, float y);
646: }
|