9 package javolution.lang;
41 return (i < 1 << 16) ? (i < 1 << 8) ?
BIT_LENGTH[i]
47 private static final byte[]
BIT_LENGTH = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4,
48 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6,
49 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
50 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
51 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
52 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
53 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
54 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
55 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
56 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
57 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
58 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
73 int i = (int) (l >> 32);
75 return (i < 1 << 16) ? (i < 1 << 8) ?
BIT_LENGTH[i] + 32
82 return (i < 0) ? 32 : (i < 1 << 16) ? (i < 1 << 8) ?
BIT_LENGTH[i]
84 : (i < 1 << 24) ? BIT_LENGTH[i >>> 16] + 16
98 longValue = longValue - ((longValue >>> 1) & 0x5555555555555555L);
99 longValue = (longValue & 0x3333333333333333L)
100 + ((longValue >>> 2) & 0x3333333333333333L);
101 longValue = (longValue + (longValue >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
102 longValue = longValue + (longValue >>> 8);
103 longValue = longValue + (longValue >>> 16);
104 longValue = longValue + (longValue >>> 32);
105 return (
int) longValue & 0x7f;
122 int x = (int)(longValue >>> 32);
123 if (x == 0) { n += 32; x = (int)longValue; }
124 if (x >>> 16 == 0) { n += 16; x <<= 16; }
125 if (x >>> 24 == 0) { n += 8; x <<= 8; }
126 if (x >>> 28 == 0) { n += 4; x <<= 4; }
127 if (x >>> 30 == 0) { n += 2; x <<= 2; }
143 if (longValue == 0)
return 64;
145 y = (int)longValue;
if (y != 0) { n = n -32; x = y; }
else x = (int)(longValue>>>32);
146 y = x <<16;
if (y != 0) { n = n -16; x = y; }
147 y = x << 8;
if (y != 0) { n = n - 8; x = y; }
148 y = x << 4;
if (y != 0) { n = n - 4; x = y; }
149 y = x << 2;
if (y != 0) { n = n - 2; x = y; }
150 return n - ((x << 1) >>> 31);
163 return (i >= 100000) ? (i >= 10000000) ? (i >= 1000000000) ? 10
164 : (i >= 100000000) ? 9 : 8 : (i >= 1000000) ? 7 : 6
165 : (i >= 100) ? (i >= 10000) ? 5 : (i >= 1000) ? 4 : 3
167 if (i == Integer.MIN_VALUE)
182 return (l <= Integer.MAX_VALUE) ?
digitLength((
int) l)
184 (l >= 100000000000000L) ? (l >= 10000000000000000L) ? (l >= 1000000000000000000L) ? 19
185 : (l >= 100000000000000000L) ? 18 : 17
186 : (l >= 1000000000000000L) ? 16 : 15
187 : (l >= 100000000000L) ? (l >= 10000000000000L) ? 14
188 : (l >= 1000000000000L) ? 13 : 12
189 : (l >= 10000000000L) ? 11 : 10;
190 if (l == Long.MIN_VALUE)
206 if (m == Long.MIN_VALUE)
212 long exp = 1023L + 52 + n + shift;
214 return Double.POSITIVE_INFINITY;
221 long bits = (shift > 0) ? (m >> shift) + ((m >> (shift - 1)) & 1) :
223 if (((bits >> 52) != 1) && (++
exp >= 0x7FF))
224 return Double.POSITIVE_INFINITY;
225 bits &= 0x000fffffffffffffL;
227 return Double.longBitsToDouble(bits);
243 if (m == Long.MIN_VALUE)
249 return Double.POSITIVE_INFINITY;
281 long carry = x3 >>> 32;
294 long mantissa = (shift < 0) ? (x3 << 31) | (x2 >>> 1) :
295 (((x3 << 32) | x2) << shift) | (x1 >>> (32 - shift));
311 x1 |= x0 >>> (63 - shift);
324 long wh = (x1 >>> 32);
325 long qh = wh / divisor;
326 long r = wh - qh * divisor;
327 long wl = (r << 32) | (x1 &
MASK_32);
328 long ql = wl / divisor;
329 r = wl - ql * divisor;
330 x1 = (qh << 32) | ql;
332 wh = (r << 31) | (x0 >>> 32);
334 r = wh - qh * divisor;
335 wl = (r << 32) | (x0 &
MASK_32);
337 x0 = (qh << 32) | ql;
347 private static final long MASK_63 = 0x7FFFFFFFFFFFFFFFL;
349 private static final long MASK_32 = 0xFFFFFFFFL;
351 private static final int[]
POW5_INT = { 1, 5, 25, 125, 625, 3125, 15625,
352 78125, 390625, 1953125, 9765625, 48828125, 244140625, 1220703125 };
367 long bits = Double.doubleToLongBits(d);
368 boolean isNegative = (bits >> 63) != 0;
369 int exp = ((int) (bits >> 52)) & 0x7FF;
370 long m = bits & 0x000fffffffffffffL;
372 throw new ArithmeticException(
373 "Cannot convert to long (Infinity or NaN)");
377 return toLongPow2(d * 18014398509481984L, n - 54);
379 m |= 0x0010000000000000L;
380 long shift =
exp - 1023L - 52 + n;
384 throw new ArithmeticException(
"Cannot convert to long (overflow)");
385 m = (shift >= 0) ? m << shift : (m >> -shift)
386 + ((m >> -(shift + 1)) & 1);
387 return isNegative ? -m : m;
401 long bits = Double.doubleToLongBits(d);
402 boolean isNegative = (bits >> 63) != 0;
403 int exp = ((int) (bits >> 52)) & 0x7FF;
404 long m = bits & 0x000fffffffffffffL;
406 throw new ArithmeticException(
407 "Cannot convert to long (Infinity or NaN)");
413 m |= 0x0010000000000000L;
414 int pow2 =
exp - 1023 - 52;
447 long carry = x3 >>> 32;
460 m = (shift < 0) ? (x3 << 31) | (x2 >>> 1) :
461 (((x3 << 32) | x2) << shift) | (x1 >>> (32 - shift));
473 x1 |= x0 >>> (63 - shift);
486 long wh = (x1 >>> 32);
487 long qh = wh / divisor;
488 long r = wh - qh * divisor;
489 long wl = (r << 32) | (x1 &
MASK_32);
490 long ql = wl / divisor;
491 r = wl - ql * divisor;
492 x1 = (qh << 32) | ql;
494 wh = (r << 31) | (x0 >>> 32);
496 r = wh - qh * divisor;
497 wl = (r << 32) | (x0 &
MASK_32);
499 x0 = (qh << 32) | ql;
508 throw new ArithmeticException(
"Overflow");
511 m = (m >> -pow2) + ((m >> -(pow2 + 1)) & 1);
512 return isNegative ? -m : m;
528 throw new ArithmeticException(
"Negative number or zero");
529 long bits = Double.doubleToLongBits(d);
530 int exp = ((int) (bits >> 52)) & 0x7FF;
532 throw new ArithmeticException(
"Infinity or NaN");
534 return floorLog2(d * 18014398509481984L) - 54;
552 if ((pow10 <= d) && (pow10 * 10 > d))
564 public static final double E = 2.71828182845904523536028747135266;
569 public static final double PI = 3.1415926535897932384626433832795;
574 public static final double HALF_PI = 1.5707963267948966192313216916398;
579 public static final double TWO_PI = 6.283185307179586476925286766559;
584 public static final double FOUR_PI = 12.566370614359172953850573533118;
589 public static final double PI_SQUARE = 9.8696044010893586188344909998762;
594 public static final double LOG2 = 0.69314718055994530941723212145818;
599 public static final double LOG10 = 2.3025850929940456840179914546844;
604 public static final double SQRT2 = 1.4142135623730950488016887242097;
609 public static final double NaN = 0.0 / 0.0;
624 return degrees * (
PI / 180.0);
636 return radians * (180.0 /
PI);
647 public static double sqrt(
double x) {
660 public static double rem(
double x,
double y) {
678 public static double ceil(
double x) {
692 public static double floor(
double x) {
693 return Math.floor(x);
704 public static double sin(
double radians) {
705 return Math.sin(radians);
716 public static double cos(
double radians) {
717 return Math.cos(radians);
728 public static double tan(
double radians) {
729 return Math.tan(radians);
741 public static double asin(
double x) {
742 if (x < -1.0 || x > 1.0)
760 public static double acos(
double x) {
775 public static double atan(
double x) {
790 public static double atan2(
double y,
double x) {
795 if ((y > 0) && (x == 0))
return PI / 2;
796 if ((y < 0) && (x == 0))
return -
PI / 2;
808 public static double sinh(
double x) {
820 public static double cosh(
double x) {
832 public static double tanh(
double x) {
846 public static double exp(
double x) {
859 public static double log(
double x) {
871 public static double log10(
double x) {
875 private static double INV_LOG10 = 0.43429448190325182765112891891661;
885 public static double pow(
double x,
double y) {
887 if ((x < 0) && (y == (
int) y))
888 return (((
int) y) & 1) == 0 ?
pow(-x, y) : -
pow(-x, y);
899 return (
int)
floor(f + 0.5f);
911 public static long round(
double d) {
912 return (
long)
floor(d + 0.5d);
921 public static int abs(
int i) {
922 return (i < 0) ? -i : i;
931 public static long abs(
long l) {
932 return (l < 0) ? -l : l;
941 public static float abs(
float f) {
942 return (f < 0) ? -f : f;
951 public static double abs(
double d) {
952 return (d < 0) ? -d : d;
964 public static int max(
int x,
int y) {
965 return (x >= y) ? x : y;
975 public static long max(
long x,
long y) {
976 return (x >= y) ? x : y;
986 public static float max(
float x,
float y) {
987 return (x >= y) ? x : y;
997 public static double max(
double x,
double y) {
998 return (x >= y) ? x : y;
1010 public static int min(
int x,
int y) {
1011 return (x < y) ? x : y;
1021 public static long min(
long x,
long y) {
1022 return (x < y) ? x : y;
1032 public static float min(
float x,
float y) {
1033 return (x < y) ? x : y;
1045 public static double min(
double x,
double y) {
1046 return (x < y) ? x : y;
1082 static final double atanhi[] = { 4.63647609000806093515e-01,
1083 7.85398163397448278999e-01,
1084 9.82793723247329054082e-01,
1085 1.57079632679489655800e+00,
1088 static final double atanlo[] = { 2.26987774529616870924e-17,
1089 3.06161699786838301793e-17,
1090 1.39033110312309984516e-17,
1091 6.12323399573676603587e-17,
1094 static final double aT[] = { 3.33333333333329318027e-01,
1095 -1.99999999998764832476e-01,
1096 1.42857142725034663711e-01,
1097 -1.11111104054623557880e-01,
1098 9.09088713343650656196e-02,
1099 -7.69187620504482999495e-02,
1100 6.66107313738753120669e-02,
1101 -5.83357013379057348645e-02,
1102 4.97687799461593236017e-02,
1103 -3.65315727442169155270e-02,
1104 1.62858201153657823623e-02,
1110 double w, s1, s2, z;
1112 long xBits = Double.doubleToLongBits(x);
1113 int __HIx = (int) (xBits >> 32);
1114 int __LOx = (int) xBits;
1117 ix = hx & 0x7fffffff;
1118 if (ix >= 0x44100000) {
1119 if (ix > 0x7ff00000 || (ix == 0x7ff00000 && (__LOx != 0)))
1126 if (ix < 0x3fdc0000) {
1127 if (ix < 0x3e200000)
1133 if (ix < 0x3ff30000)
1134 if (ix < 0x3fe60000) {
1136 x = (2.0 * x -
one) / (2.0 + x);
1139 x = (x -
one) / (x +
one);
1141 else if (ix < 0x40038000) {
1143 x = (x - 1.5) / (
one + 1.5 * x);
1157 * (
aT[6] + w * (
aT[8] + w *
aT[10])))));
1158 s2 = w * (
aT[1] + w * (
aT[3] + w * (
aT[5] + w * (
aT[7] + w *
aT[9]))));
1160 return x - x * (s1 + s2);
1163 return (hx < 0) ? -z : z;
1231 static final double ln2_hi = 6.93147180369123816490e-01,
1234 Lg1 = 6.666666666666735130e-01,
1235 Lg2 = 3.999999999940941908e-01,
1236 Lg3 = 2.857142874366239149e-01,
1237 Lg4 = 2.222219843214978396e-01,
1238 Lg5 = 1.818357216161805012e-01,
1239 Lg6 = 1.531383769920937332e-01,
1240 Lg7 = 1.479819860511658591e-01;
1245 double hfsq, f, s, z, R, w, t1, t2, dk;
1249 long xBits = Double.doubleToLongBits(x);
1250 hx = (int) (xBits >> 32);
1254 if (hx < 0x00100000) {
1255 if (((hx & 0x7fffffff) | lx) == 0)
1258 return (x - x) /
zero;
1261 xBits = Double.doubleToLongBits(x);
1262 hx = (int) (xBits >> 32);
1264 if (hx >= 0x7ff00000)
1266 k += (hx >> 20) - 1023;
1268 i = (hx + 0x95f64) & 0x100000;
1269 xBits = Double.doubleToLongBits(x);
1270 int HIx = hx | (i ^ 0x3ff00000);
1271 xBits = ((HIx & 0xFFFFFFFFL) << 32) | (xBits & 0xFFFFFFFFL);
1272 x = Double.longBitsToDouble(xBits);
1275 if ((0x000fffff & (2 + hx)) < 3) {
1283 R = f * f * (0.5 - 0.33333333333333333 * f);
1304 return f - (hfsq - s * (hfsq + R));
1307 - ((hfsq - (s * (hfsq + R) + dk *
ln2_lo)) - f);
1309 return f - s * (f - R);
1311 return dk *
ln2_hi - ((s * (f - R) - dk *
ln2_lo) - f);
1389 static final double halF[] = { 0.5, -0.5, },
1394 -6.93147180369123816490e-01, },
1396 -1.90821492927058770002e-10, },
1398 P1 = 1.66666666666666019037e-01,
1399 P2 = -2.77777777770155933842e-03,
1400 P3 = 6.61375632143793436117e-05,
1401 P4 = -1.65339022054652515390e-06,
1402 P5 = 4.13813679705723846039e-08;
1406 double y, hi = 0, lo = 0, c, t;
1409 long xBits = Double.doubleToLongBits(x);
1410 int __HIx = (int) (xBits >> 32);
1411 int __LOx = (int) xBits;
1414 xsb = (hx >> 31) & 1;
1418 if (hx >= 0x40862E42) {
1419 if (hx >= 0x7ff00000)
1420 if (((hx & 0xfffff) | __LOx) != 0)
1423 return (xsb == 0) ? x : 0.0;
1431 if (hx > 0x3fd62e42) {
1432 if (hx < 0x3FF0A2B2) {
1433 hi = x -
ln2HI[xsb];
1439 hi = x - t *
ln2HI[0];
1443 }
else if (hx < 0x3e300000) {
1451 c = x - t * (
P1 + t * (
P2 + t * (
P3 + t * (
P4 + t *
P5))));
1453 return one - ((x * c) / (c - 2.0) - x);
1455 y =
one - ((lo - (x * c) / (2.0 - c)) - hi);
1456 long yBits = Double.doubleToLongBits(y);
1457 int __HIy = (int) (yBits >> 32);
1460 yBits = ((__HIy & 0xFFFFFFFFL) << 32) | (yBits & 0xFFFFFFFFL);
1461 y = Double.longBitsToDouble(yBits);
1464 __HIy += ((k + 1000) << 20);
1465 yBits = ((__HIy & 0xFFFFFFFFL) << 32) | (yBits & 0xFFFFFFFFL);
1466 y = Double.longBitsToDouble(yBits);