⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 microdouble.java

📁 This is a Java library for performing floating-point calculations on small devices such as mobile p
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        r = m << (64 + x);
        m >>>= -x;
      }
      if ((n ^ ceil) && (r != 0)) {
        m++;
      }
    }
    return pack(n, 0, m);
  }

  
  /////////////////////////////////////////////////////////////////////////////
  // String Conversion
  /////////////////////////////////////////////////////////////////////////////
  
  // decimal -> binary 
  
  // base 2 mantissas for 10**-345 through 10**309, at intervals of 1000
  private static final long[] pow10m = {
          0xf4b0769e47eb5a79L, 0xeef453d6923bd65aL, 0xe95a99df8ace6f54L, 
          0xe3e27a444d8d98b8L, 0xde8b2b66b3bc4724L, 0xd953e8624b85dd79L, 
          0xd43bf0effdc0ba48L, 0xcf42894a5dce35eaL, 0xca66fa129f9b60a7L, 
          0xc5a890362fddbc63L, 0xc1069cd4eabe89f9L, 0xbc807527ed3e12bdL, 
          0xb8157268fdae9e4cL, 0xb3c4f1ba87bc8697L, 0xaf8e5410288e1b6fL, 
          0xab70fe17c79ac6caL, 0xa76c582338ed2622L, 0xa37fce126597973dL, 
          0x9faacf3df73609b1L, 0x9becce62836ac577L, 0x9845418c345644d7L, 
          0x94b3a202eb1c3f39L, 0x91376c36d99995beL, 0x8dd01fad907ffc3cL, 
          0x8a7d3eef7f1cfc52L, 0x873e4f75e2224e68L, 0x8412d9991ed58092L, 
          0x80fa687f881c7f8eL, 0xfbe9141915d7a922L, 0xf6019da07f549b2bL, 
          0xf03d93eebc589f88L, 0xea9c227723ee8bcbL, 0xe51c79a85916f485L, 
          0xdfbdcece67006ac9L, 0xda7f5bf590966849L, 0xd5605fcdcf32e1d7L, 
          0xd0601d8efc57b08cL, 0xcb7ddcdda26da269L, 0xc6b8e9b0709f109aL, 
          0xc21094364dfb5637L, 0xbd8430bd08277231L, 0xb913179899f68584L, 
          0xb4bca50b065abe63L, 0xb080392cc4349dedL, 0xac5d37d5b79b6239L, 
          0xa8530886b54dbdecL, 0xa46116538d0deb78L, 0xa086cfcd97bf97f4L, 
          0x9cc3a6eec6311a64L, 0x991711052d8bf3c5L, 0x9580869f0e7aac0fL, 
          0x91ff83775423cc06L, 0x8e938662882af53eL, 0x8b3c113c38f9f37fL, 
          0x87f8a8d4cfa417caL, 0x84c8d4dfd2c63f3bL, 0x81ac1fe293d599c0L, 
          0xfd442e4688bd304bL, 0xf7549530e188c129L, 0xf18899b1bc3f8ca2L, 
          0xebdf661791d60f56L, 0xe65829b3046b0afaL, 0xe0f218b8d25088b8L, 
          0xdbac6c247d62a584L, 0xd686619ba27255a3L, 0xd17f3b51fca3a7a1L, 
          0xcc963fee10b7d1b3L, 0xc7caba6e7c5382c9L, 0xc31bfa0fe5698db8L, 
          0xbe89523386091466L, 0xba121a4650e4ddecL, 0xb5b5ada8aaff80b8L, 
          0xb1736b96b6fd83b4L, 0xad4ab7112eb3929eL, 0xa93af6c6c79b5d2eL, 
          0xa54394fe1eedb8ffL, 0xa163ff802a3426a9L, 0x9d9ba7832936edc1L, 
          0x99ea0196163fa42eL, 0x964e858c91ba2655L, 0x92c8ae6b464fc96fL, 
          0x8f57fa54c2a9eab7L, 0x8bfbea76c619ef36L, 0x88b402f7fd75539bL, 
          0x857fcae62d8493a5L, 0x825ecc24c8737830L, 0xfea126b7d78186bdL, 
          0xf8a95fcf88747d94L, 0xf2d56790ab41c2a3L, 0xed246723473e3813L, 
          0xe7958cb87392c2c3L, 0xe2280b6c20dd5232L, 0xdcdb1b2798182245L, 
          0xd7adf884aa879177L, 0xd29fe4b18e88640fL, 0xcdb02555653131b6L, 
          0xc8de047564d20a8cL, 0xc428d05aa4751e4dL, 0xbf8fdb78849a5f97L, 
          0xbb127c53b17ec159L, 0xb6b00d69bb55c8d1L, 0xb267ed1940f1c61cL, 
          0xae397d8aa96c1b78L, 0xaa242499697392d3L, 0xa6274bbdd0fadd62L, 
          0xa2425ff75e14fc32L, 0x9e74d1b791e07e48L, 0x9abe14cd44753b53L, 
          0x971da05074da7befL, 0x9392ee8e921d5d07L, 0x901d7cf73ab0acd9L, 
          0x8cbccc096f5088ccL, 0x89705f4136b4a597L, 0x8637bd05af6c69b6L, 
          0x83126e978d4fdf3bL, 0x8000000000000000L, 0xfa00000000000000L, 
          0xf424000000000000L, 0xee6b280000000000L, 0xe8d4a51000000000L, 
          0xe35fa931a0000000L, 0xde0b6b3a76400000L, 0xd8d726b7177a8000L, 
          0xd3c21bcecceda100L, 0xcecb8f27f4200f3aL, 0xc9f2c9cd04674edfL, 
          0xc5371912364ce305L, 0xc097ce7bc90715b3L, 0xbc143fa4e250eb31L, 
          0xb7abc627050305aeL, 0xb35dbf821ae4f38cL, 0xaf298d050e4395d7L, 
          0xab0e93b6efee0054L, 0xa70c3c40a64e6c52L, 0xa321f2d7226895c8L, 
          0x9f4f2726179a2245L, 0x9b934c3b330c8577L, 0x97edd871cfda3a57L, 
          0x945e455f24fb1cf9L, 0x90e40fbeea1d3a4bL, 0x8d7eb76070a08aedL, 
          0x8a2dbf142dfcc7abL, 0x86f0ac99b4e8dafdL, 0x83c7088e1aab65dbL, 
          0x80b05e5ac60b6178L, 0xfb5878494ace3a5fL, 0xf5746577930d6501L, 
          0xefb3ab16c59b14a3L, 0xea1575143cf97227L, 0xe498f455c38b997aL, 
          0xdf3d5e9bc0f653e1L, 0xda01ee641a708deaL, 0xd4e5e2cdc1d1ea96L, 
          0xcfe87f7cef46ff17L, 0xcb090c8001ab551cL, 0xc646d63501a1511eL, 
          0xc1a12d2fc3978937L, 0xbd176620a501fc00L, 0xb8a8d9bbe123f018L, 
          0xb454e4a179dd1877L, 0xb01ae745b101e9e4L, 0xabfa45da0edbde69L, 
          0xa7f26836f282b733L, 0xa402b9c5a8d3a6e7L, 0xa02aa96b06deb0feL, 
          0x9c69a97284b578d8L, 0x98bf2f79d5993803L, 0x952ab45cfa97a0b3L, 
          0x91abb422ccb812efL, 0x8e41ade9fbebc27dL, 0x8aec23d680043beeL, 
          0x87aa9aff79042287L, 0x847c9b5d7c2e09b7L, 0x8161afb94b44f57dL, 
          0xfcb2cb35e702af78L, 0xf6c69a72a3989f5cL, 0xf0fdf2d3f3c30b9fL, 
          0xeb57ff22fc0c795aL, 0xe5d3ef282a242e82L, 0xe070f78d3927556bL, 
          0xdb2e51bfe9d0696aL, 0xd60b3bd56a5586f2L, 0xd106f86e69d785c8L, 
          0xcc20ce9bd35c78a5L, 0xc75809c42c684dd1L, 0xc2abf989935ddbfeL, 
          0xbe1bf1b059e9a8d6L, 0xb9a74a0637ce2ee1L, 0xb54d5e4a127f59c8L, 
          0xb10d8e1456105dadL, 0xace73cbfdc0bfb7bL, 0xa8d9d1535ce3b396L, 
          0xa4e4b66b68b65d61L, 0xa1075a24e4421731L, 0x9d412e0806e88aa6L, 
          0x9991a6f3d6bf1766L, 0x95f83d0a1fb69cd9L, 0x92746b9be2f8552cL, 
          0x8f05b1163ba6832dL, 0x8bab8eefb6409c1aL, 0x8865899617fb1871L, 
          0x8533285c936b35dfL, 0x8213f56a67f6b29cL, 0xfe0efb53d30dd4d8L, 
          0xf81aa16fdc1b81dbL, 0xf24a01a73cf2dcd0L, 0xec9c459d51852ba3L, 
          0xe7109bfba19c0c9dL, 0xe1a63853bbd26451L, 0xdc5c5301c56b75f7L, 
          0xd732290fbacaf134L, 0xd226fc195c6a2f8cL, 0xcd3a1230c43fb26fL, 
          0xc86ab5c39fa63441L, 0xc3b8358109e84f07L, 0xbf21e44003acdd2dL, 
          0xbaa718e68396cffeL, 0xb6472e511c81471eL, 0xb201833b35d63f73L, 
  };
  
  // base 2 exponents for 10**-345 through 10**309, at intervals of 1000
  private static final short[] pow10x = {
          -1146, -1136, -1126, -1116, -1106, -1096, -1086, -1076, 
          -1066, -1056, -1046, -1036, -1026, -1016, -1006, -996, 
          -986, -976, -966, -956, -946, -936, -926, -916, 
          -906, -896, -886, -876, -867, -857, -847, -837, 
          -827, -817, -807, -797, -787, -777, -767, -757, 
          -747, -737, -727, -717, -707, -697, -687, -677, 
          -667, -657, -647, -637, -627, -617, -607, -597, 
          -587, -578, -568, -558, -548, -538, -528, -518, 
          -508, -498, -488, -478, -468, -458, -448, -438, 
          -428, -418, -408, -398, -388, -378, -368, -358, 
          -348, -338, -328, -318, -308, -298, -289, -279, 
          -269, -259, -249, -239, -229, -219, -209, -199, 
          -189, -179, -169, -159, -149, -139, -129, -119, 
          -109, -99, -89, -79, -69, -59, -49, -39, 
          -29, -19, -9, 1, 10, 20, 30, 40, 
          50, 60, 70, 80, 90, 100, 110, 120, 
          130, 140, 150, 160, 170, 180, 190, 200, 
          210, 220, 230, 240, 250, 260, 270, 280, 
          290, 299, 309, 319, 329, 339, 349, 359, 
          369, 379, 389, 399, 409, 419, 429, 439, 
          449, 459, 469, 479, 489, 499, 509, 519, 
          529, 539, 549, 559, 569, 579, 588, 598, 
          608, 618, 628, 638, 648, 658, 668, 678, 
          688, 698, 708, 718, 728, 738, 748, 758, 
          768, 778, 788, 798, 808, 818, 828, 838, 
          848, 858, 868, 877, 887, 897, 907, 917, 
          927, 937, 947, 957, 967, 977, 987, 997, 
          1007, 1017, 1027, 
  };

  private static long decToDouble(boolean negative, int base10x, long base10m) {
    if (base10m == 0) {
      return (negative ? NEGATIVE_ZERO : ZERO);
    }
    // maximize base10m to ensure consistency between toString and parseDouble
    while ((base10m > 0) && (base10m <= 0x1999999999999999L)) { // (Long.MAX_VALUE / 5))) {
      base10m = (base10m << 3) + (base10m << 1);
      base10x--;
    }
    // base10x needs to be a multiple of 3, because the tables are
    // spaced at intervals of 1000 (not 10).
    base10x += 345;
    int mod = base10x % 3;
    base10x /= 3;
    if (base10x < 0) { // -345
      return (negative ? NEGATIVE_ZERO : ZERO);
    } else if (base10x > 218) { // 309
      return (negative ? NEGATIVE_INFINITY : POSITIVE_INFINITY);
    }
    int base2x = pow10x[base10x];
    int s = BitUtils.countLeadingZeros(base10m);
    base10m <<= s;
    base2x -= s;
    long base2m = dpMul(base10m, pow10m[base10x]);
    while (mod > 0) {
      if (base2m < 0) {
        base2m >>>= 1;
        base2x++;
      }
      base2m += base2m >>> 2;
      base2x += 3;
      mod--;
    }
    return pack(negative, base2x, base2m);
  }

  /**
   * Double-precision integer multiplication of x1 and x2.
   */
  private static final long dpMul(long x1, long x2) {
    long v1 = (x1 >>> 32)        * (x2 >>> 32);
    long v2 = (x1 & 0xffffffffL) * (x2 >>> 32);
    long v3 = (x1 >>> 32)        * (x2 & 0xffffffffL);
    v1 += v2 >>> 32;
    v1 += v3 >>> 32;
    if (((v2 + v3) << 32) < 0) {
      v1++;
    }
    return v1;
  }
  
  /**
   * Mimics <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Double.html#parseDouble(String)">Double.parseDouble(String)</a>.
   * <p>
   * See the notes on <code>toString</code> for some caveats on String 
   * conversion.
   *
   * @see #toString
   * @exception  NumberFormatException  if the string does not contain a
   *               parsable number.
   */
  public static long parseDouble(String s) {
    // remove leading & trailing whitespace
    s = s.trim().toUpperCase();
    
    // check length
    int len = s.length();
    if (len == 0) {
      throw new NumberFormatException(s);
    }
    
    // check for NaN
    if ("NAN".equals(s)) {
      return NaN;
    }
    
    // begin parsing, one character at a time
    int idx = 0;
    
    // read sign
    boolean negative = false;
    char c = s.charAt(0);
    negative = (c == '-');
    if (negative || (c == '+')) {
      idx = 1;
    }

    // check for "Infinity"
    if (idx < len) {
      c = s.charAt(idx);
      if ((c == 'I') || (c == 'i')) {
        if ("INFINITY".equals(s.substring(idx))) {
          return (negative ? NEGATIVE_INFINITY : POSITIVE_INFINITY);
        }
      }
    }

    // read Digits.Digits
    long mantissa = 0;
    int exponent = 0;
    int fractionChars = 0;
    boolean sticky = false;
    boolean readingFraction = false;
    while (idx < len) {
      c = s.charAt(idx);
      if (c == '.') {
        if (readingFraction) {
          throw new NumberFormatException(s);
        }
        readingFraction = true;
      } else if ((c < '0') || (c > '9')) {
        break;
      } else {
        fractionChars++;
        if (mantissa <= 0x1999999999999998L) { // ((Long.MAX_VALUE / 5) - 1)
          mantissa = (mantissa << 3) + (mantissa << 1) + (c - '0');
          if (readingFraction) {
            exponent--;
          }
        } else {
          if (! readingFraction) {
            exponent++;
          }
          sticky |= (c != '0');
        }
      }
      idx++;
    }
    if (fractionChars == 0) {
      throw new NumberFormatException(s);
    }
    
    // read exponent
    if (((idx + 1) < len) && ((s.charAt(idx) == 'E') || (s.charAt(idx) == 'e'))) {
      try {
        exponent += Integer.parseInt(s.substring(idx + 1));
      } catch (NumberFormatException e) {
        throw new NumberFormatException(s);
      }
      idx = len;
    } else if (idx != len) {
      // check that we parsed the entire string
      throw new NumberFormatException(s);
    }

    // convert the decimal to a float
    return decToDouble(negative, exponent, mantissa);
  }

  // binary -> decimal
  
  // base 10 mantissas for 2**-1075 through 2**972, at intervals of 2**11
  private static final long[] pow2m = {
          0x3f3d8b077b8e0b11L, 0x81842f29f2cce376L, 0x1a8662f3b3919708L, 
          0x3652b62c71ce021dL, 0x6f40f20501a5e7a8L, 0xe3d8f9e563a198e5L, 
          0x2ea9c639a0e5b3ffL, 0x5f90f22001d66e96L, 0xc3b8358109e84f07L, 
          0x2815578d865470daL, 0x52173a79e8197a93L, 0xa81f301449ee8c70L, 
          0x226e6cf846d8ca6fL, 0x4683f19a2ab1bf59L, 0x906a617d450187e2L, 
          0x1d9388b3aa30a574L, 0x3c928069cf3cb734L, 0x7c0d50b7ee0dc0edL, 
          0xfe0efb53d30dd4d8L, 0x3407fbc42995e10bL, 0x6a8f537d42bc2b19L, 
          0xda3c0f568cc4f3e9L, 0x2cb1c756f2a408feL, 0x5b88c3416ddb353cL, 
          0xbb764c4ca7a44410L, 0x266469bcf5afc5d9L, 0x4ea0970403744553L, 
          0xa1075a24e4421731L, 0x20fa8ae248247913L, 0x438a53baf1f4ae3cL, 
          0x8a5296ffe33cc930L, 0x1c5416bb92e3e607L, 0x3a044721f1706ea6L, 
          0x76d1770e38320986L, 0xf356f7ebf83552feL, 0x31d602710b1a1374L, 
          0x6610674de9ae3c53L, 0xd106f86e69d785c8L, 0x2acf0bf77baab497L, 
          0x57ac20b32a535d5eL, 0xb38d92d760ec4455L, 0x24c5bfdd7761f2f6L, 
          0x4b4f5be23c2cf3a2L, 0x9a3c2087a63f6399L, 0x1f965966bce055efL, 
          0x40b0d7dca5a27abfL, 0x847c9b5d7c2e09b7L, 0x1b221effe500d3b5L, 
          0x3791a7ef666817f9L, 0x71ce24bb2fefcecaL, 0xe912b9d1478ceb17L, 
          0x2fbbbed612bfe181L, 0x61c209e792f16b87L, 0xc83553c5c8965d3dL, 
          0x2900ae716a34e9baL, 0x53f9341b79415b99L, 0xabfa45da0edbde69L, 
          0x233894a789cd2ec7L, 0x4821f50d63f209c9L, 0x93ba47c980e98ce0L, 
          0x1e412f0f768fad71L, 0x3df622f090826959L, 0x7ee5a7d0010b1532L, 
          0x19fd0fef9de8dfe3L, 0x353978b370747aa6L, 0x6d00f7320d3846f5L, 
          0xdf3d5e9bc0f653e1L, 0x2db830ddf3e8b84cL, 0x5da22ed4e5309410L, 
          0xbfc2ef456ae276e9L, 0x2745d2cb73b0391fL, 0x506e3af8bbc71cebL, 
          0xa4b8cab1a1563f52L, 0x21bc2b266d3a36bfL, 0x4516df8a16fe63d6L, 
          0x8d7eb76070a08aedL, 0x1cfa698c95390ba9L, 0x3b58e88c75313ecaL, 
          0x798b138e3fe1c845L, 0xf8ebad2b84e0d58cL, 0x32fa9be33ac0aeceL, 
          0x6867a5a867f103b3L, 0xd5d238a4abe98068L, 0x2bca63414390e576L, 
          0x59aedfc10d7279c6L, 0xb7abc627050305aeL, 0x259da6542d43623dL, 
          0x4d0985cb1d3608aeL, 0x9dc5ada82b70b59eL, 0x204fce5e3e250261L, 
          0x422ca8b0a00a4250L, 0x878678326eac9000L, 0x1bc16d674ec80000L, 
          0x38d7ea4c68000000L, 0x746a528800000000L, 0xee6b280000000000L, 
          0x30d4000000000000L, 0x6400000000000000L, 0xcccccccccccccccdL, 
          0x29f16b11c6d1e109L, 0x55e63b88c230e77eL, 0xafebff0bcb24aaffL, 
          0x24075f3dceac2b36L, 0x49c97747490eae84L, 0x971da05074da7befL, 
          0x1ef2d0f5da7dd8aaL, 0x3f61ed7ca0c03283L, 0x81ceb32c4b43fcf5L, 
          0x1a95a5b7f87a0ef1L, 0x3671f73b54f1c895L, 0x6f80f42fc8971bd2L, 
          0xe45c10c42a2b3b06L, 0x2ec49f14ec5fb056L, 0x5fc7edbc424d2fcbL, 
          0xc428d05aa4751e4dL, 0x282c674aadc39bb6L, 0x524675555bad4716L, 
          0xa87fea27a539e9a5L, 0x22823c3e2fc3c55bL, 0x46ac8391ca4529b0L, 
          0x90bd77f3483bb9baL, 0x1da48ce468e7c702L, 0x3cb559e42ad070a9L, 
          0x7c54afe7c43a3ecaL, 0xfea126b7d78186bdL, 0x3425eb41e9c7c9adL, 
          0x6acca251be03a951L, 0xdab99e59958885c5L, 0x2ccb7e3a7cd51959L, 
          0x5bbd6d030bf1dde6L, 0xbbe226efb628afebL, 0x267a8065858fe90cL, 
          0x4ecdd3c1949b76e0L, 0xa163ff802a3426a9L, 0x210d8432d2fc5833L, 
          0x43b12f82b63e2546L, 0x8aa22c0dbef60ee4L, 0x1c6463225ab7ec1dL, 
          0x3a25a835f947855aL, 0x7715d36033c5acc0L, 0xf3e2f893dec3f126L, 
          0x31f2ae9b9f14e0b2L, 0x664b1ff7085be8daL, 0xd17f3b51fca3a7a1L, 
          0x2ae7ad1f207d4454L, 0x57de91a832277568L, 0xb3f4e093db73a093L, 
          0x24dae7f3aec97265L, 0x4b7ab0078ad3dbf3L, 0x9a94dd3e8cf578baL, 
          0x1fa885c8d117a609L, 0x40d60ff149eacce0L, 0x84c8d4dfd2c63f3bL, 
          0x1b31bb5dc320d18fL, 0x37b1a07e7d30c7ccL, 0x720f9eb539bbf765L, 
          0xe998d258869facd7L, 0x2fd735519e3bbc2eL, 0x61fa48553bdeb07eL, 
          0xc8a883c0fdaf7df0L, 0x29184594e3437adeL, 0x542984435aa6def6L, 
          0xac5d37d5b79b6239L, 0x234cd83c273db92fL, 0x484b75379c244c28L, 
          0x940f4613ae5ed137L, 0x1e5297287c2f4579L, 0x3e19c9072331b530L, 
          0x7f2eaa0a85848581L, 0x1a0c03b1df8af611L, 0x355817f373ccb876L, 
          0x6d3fadfac84b3424L, 0xdfbdcece67006ac9L, 0x2dd27ebb4504974eL, 
          0x5dd80dc941929e51L, 0xc0314325637a193aL, 0x275c6b23eb69b26dL, 
          0x509c814fb511cfb9L, 0xa5178fff668ae0b6L, 0x21cf93dd7888939aL, 
          0x453e9f77bf8e7e29L, 0x8dd01fad907ffc3cL, 0x1d0b15a491eb8459L, 
          0x3b7b0d9ac471b2e4L, 0x79d1013cf6ab6a45L, 0xf97ae3d0d2446f25L, 
          0x3317f065bfbf5f43L
 };
          
  // base 10 exponents for 2**-1075 through 2**972, at intervals of 2**11
  private static final short[] pow2x = {
          -323, -320, -316, -313, -310, -307, -303, -300, 
          -297, -293, -290, -287, -283, -280, -277, -273, 
          -270, -267, -264, -260, -257, -254, -250, -247, 
          -244, -240, -237, -234, -230, -227, -224, -220, 
          -217, -214, -211, -207, -204, -201, -197, -194, 
          -191, -187, -184, -181, -177, -174, -171, -167, 
          -164, -161, -158, -154, -151, -148, -144, -141, 
          -138, -134, -131, -128, -124, -121, -118, -114, 
          -111, -108, -105, -101, -98, -95, -91, -88, 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -