1 7 8 package java.lang; 9 import java.util.Random ; 10 import sun.misc.FpUtils; 11 12 46 47 public final class StrictMath { 48 49 52 private StrictMath() {} 53 54 58 public static final double E = 2.7182818284590452354; 59 60 65 public static final double PI = 3.14159265358979323846; 66 67 77 public static native double sin(double a); 78 79 87 public static native double cos(double a); 88 89 99 public static native double tan(double a); 100 101 112 public static native double asin(double a); 113 114 123 public static native double acos(double a); 124 125 135 public static native double atan(double a); 136 137 146 public static strictfp double toRadians(double angdeg) { 147 return angdeg / 180.0 * PI; 148 } 149 150 161 public static strictfp double toDegrees(double angrad) { 162 return angrad * 180.0 / PI; 163 } 164 165 178 public static native double exp(double a); 179 180 194 public static native double log(double a); 195 196 197 215 public static native double log10(double a); 216 217 233 public static native double sqrt(double a); 234 235 258 public static native double cbrt(double a); 259 260 282 public static native double IEEEremainder(double f1, double f2); 283 284 302 public static native double ceil(double a); 303 304 319 public static native double floor(double a); 320 321 337 public static double rint(double a) { 338 363 double twoToThe52 = (double)(1L << 52); double sign = FpUtils.rawCopySign(1.0, a); a = Math.abs(a); 366 367 if (a < twoToThe52) { a = ((twoToThe52 + a ) - twoToThe52); 369 } 370 371 return sign * a; } 373 374 422 public static native double atan2(double y, double x); 423 424 425 545 public static native double pow(double a, double b); 546 547 569 public static int round(float a) { 570 return (int)floor(a + 0.5f); 571 } 572 573 596 public static long round(double a) { 597 return (long)floor(a + 0.5d); 598 } 599 600 private static Random randomNumberGenerator; 601 602 private static synchronized void initRNG() { 603 if (randomNumberGenerator == null) 604 randomNumberGenerator = new Random (); 605 } 606 607 628 public static double random() { 629 if (randomNumberGenerator == null) initRNG(); 630 return randomNumberGenerator.nextDouble(); 631 } 632 633 647 public static int abs(int a) { 648 return (a < 0) ? -a : a; 649 } 650 651 665 public static long abs(long a) { 666 return (a < 0) ? -a : a; 667 } 668 669 684 public static float abs(float a) { 685 return (a <= 0.0F) ? 0.0F - a : a; 686 } 687 688 703 public static double abs(double a) { 704 return (a <= 0.0D) ? 0.0D - a : a; 705 } 706 707 718 public static int max(int a, int b) { 719 return (a >= b) ? a : b; 720 } 721 722 733 public static long max(long a, long b) { 734 return (a >= b) ? a : b; 735 } 736 737 private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f); 738 private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d); 739 740 754 public static float max(float a, float b) { 755 if (a != a) return a; if ((a == 0.0f) && (b == 0.0f) 757 && (Float.floatToIntBits(a) == negativeZeroFloatBits)) { 758 return b; 759 } 760 return (a >= b) ? a : b; 761 } 762 763 777 public static double max(double a, double b) { 778 if (a != a) return a; if ((a == 0.0d) && (b == 0.0d) 780 && (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) { 781 return b; 782 } 783 return (a >= b) ? a : b; 784 } 785 786 797 public static int min(int a, int b) { 798 return (a <= b) ? a : b; 799 } 800 801 812 public static long min(long a, long b) { 813 return (a <= b) ? a : b; 814 } 815 816 830 public static float min(float a, float b) { 831 if (a != a) return a; if ((a == 0.0f) && (b == 0.0f) 833 && (Float.floatToIntBits(b) == negativeZeroFloatBits)) { 834 return b; 835 } 836 return (a <= b) ? a : b; 837 } 838 839 853 public static double min(double a, double b) { 854 if (a != a) return a; if ((a == 0.0d) && (b == 0.0d) 856 && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) { 857 return b; 858 } 859 return (a <= b) ? a : b; 860 } 861 862 885 public static double ulp(double d) { 886 return sun.misc.FpUtils.ulp(d); 887 } 888 889 912 public static float ulp(float f) { 913 return sun.misc.FpUtils.ulp(f); 914 } 915 916 933 public static double signum(double d) { 934 return sun.misc.FpUtils.signum(d); 935 } 936 937 954 public static float signum(float f) { 955 return sun.misc.FpUtils.signum(f); 956 } 957 958 981 public static native double sinh(double x); 982 983 1005 public static native double cosh(double x); 1006 1007 1036 public static native double tanh(double x); 1037 1038 1059 public static native double hypot(double x, double y); 1060 1061 1086 public static native double expm1(double x); 1087 1088 1117 public static native double log1p(double x); 1118} 1119 | Popular Tags |