Java Double Number From toDoublePow2(long m, int n)

Here you can find the source of toDoublePow2(long m, int n)

Description

Returns the closest double representation of the specified long number multiplied by a power of two.

License

Open Source License

Parameter

Parameter Description
m the <code>long</code> multiplier. (must be non-negative)
n the power of two exponent.

Return

m * 2n.

Declaration

private static double toDoublePow2(long m, int n) 

Method Source Code

//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);
    }
}

Related

  1. toDoubleByObject(Object obj)
  2. toDoubleDigit(int number)
  3. toDoubleMatrix(Number[][] matrix)
  4. toDoubleObject(Object obj)
  5. toDoublePow10(long m, int n)
  6. toDoublePrecision(long fixedPrecisionOrdinate)
  7. toDoublePrimitiveArray(final Double[] wrappedArray)
  8. toDoubleQuotes(String str)
  9. toDoubles(byte[] bytes)