Here you can find the source of toDoublePow2(long m, int n)
double
representation of the specified long
number multiplied by a power of two.
Parameter | Description |
---|---|
m | the <code>long</code> multiplier. (must be non-negative) |
n | the power of two exponent. |
m * 2n
.
private static double toDoublePow2(long m, int n)
//package com.java2s; public class Main { /**/*from w w w . j a v a2 s. c om*/ * Returns the closest <code>double</code> representation of the * specified <code>long</code> number multiplied by a power of two. * * @param m the <code>long</code> multiplier. (must be non-negative) * @param n the power of two exponent. * @return <code>m * 2<sup>n</sup></code>. */ private static double toDoublePow2(long m, int n) { assert m >= 0; if (m == 0) return 0.0; int bitLength = Long.SIZE - Long.numberOfLeadingZeros(m); int shift = bitLength - 53; long exp = 1023L + 52 + n + shift; // Use long to avoid overflow. if (exp >= 0x7FF) return Double.POSITIVE_INFINITY; if (exp <= 0) { // Degenerated number (subnormal, assume 0 for bit 52) if (exp <= -54) return 0.0; return toDoublePow2(m, n + 54) / 18014398509481984L; // 2^54 Exact. } // Normal number. long bits = (shift > 0) ? (m >> shift) + ((m >> (shift - 1)) & 1) : // Rounding. m << -shift; if (((bits >> 52) != 1) && (++exp >= 0x7FF)) return Double.POSITIVE_INFINITY; bits &= 0x000fffffffffffffL; // Clears MSB (bit 52) bits |= exp << 52; return Double.longBitsToDouble(bits); } }