/** * Returns the largest power of 2 that is less than or equal to the * the specified positive value. * * @param d the <code>double</code> number. * @return <code>floor(Log2(abs(d)))</code> * @throws ArithmeticException if <code>d <= 0<code> or <code>d</code> * is <code>NaN</code> or <code>Infinity</code>. **/ public static int floorLog2(double d) { if (d <= 0) throw new ArithmeticException("Negative number or zero"); long bits = Double.doubleToLongBits(d); int exp = ((int) (bits >> 52)) & 0x7FF; if (exp == 0x7FF) throw new ArithmeticException("Infinity or NaN"); if (exp == 0) return floorLog2(d * 18014398509481984L) - 54; // 2^54 Exact. return exp - 1023; }
/** * Returns the largest power of 2 that is less than or equal to the * the specified positive value. * * @param d the <code>double</code> number. * @return <code>floor(Log2(abs(d)))</code> * @throws ArithmeticException if <code>d <= 0<code> or <code>d</code> * is <code>NaN</code> or <code>Infinity</code>. **/ public static int floorLog2(double d) { if (d <= 0) throw new ArithmeticException("Negative number or zero"); long bits = Double.doubleToLongBits(d); int exp = ((int) (bits >> 52)) & 0x7FF; if (exp == 0x7FF) throw new ArithmeticException("Infinity or NaN"); if (exp == 0) return floorLog2(d * 18014398509481984L) - 54; // 2^54 Exact. return exp - 1023; }
/** * Returns the largest power of 2 that is less than or equal to the * the specified positive value. * * @param d the <code>double</code> number. * @return <code>floor(Log2(abs(d)))</code> * @throws ArithmeticException if <code>d <= 0<code> or <code>d</code> * is <code>NaN</code> or <code>Infinity</code>. **/ public static int floorLog2(double d) { if (d <= 0) throw new ArithmeticException("Negative number or zero"); long bits = Double.doubleToLongBits(d); int exp = ((int) (bits >> 52)) & 0x7FF; if (exp == 0x7FF) throw new ArithmeticException("Infinity or NaN"); if (exp == 0) return floorLog2(d * 18014398509481984L) - 54; // 2^54 Exact. return exp - 1023; } /**/
/** * Returns the largest power of 2 that is less than or equal to the * the specified positive value. * * @param d the <code>double</code> number. * @return <code>floor(Log2(abs(d)))</code> * @throws ArithmeticException if <code>d <= 0<code> or <code>d</code> * is <code>NaN</code> or <code>Infinity</code>. **/ public static int floorLog2(double d) { if (d <= 0) throw new ArithmeticException("Negative number or zero"); long bits = Double.doubleToLongBits(d); int exp = ((int) (bits >> 52)) & 0x7FF; if (exp == 0x7FF) throw new ArithmeticException("Infinity or NaN"); if (exp == 0) return floorLog2(d * 18014398509481984L) - 54; // 2^54 Exact. return exp - 1023; }
/** * Returns the largest power of 10 that is less than or equal to the * the specified positive value. * * @param d the <code>double</code> number. * @return <code>floor(Log10(abs(d)))</code> * @throws ArithmeticException if <code>d <= 0<code> or <code>d</code> * is <code>NaN</code> or <code>Infinity</code>. **/ public static int floorLog10(double d) { int guess = (int) (LOG2_DIV_LOG10 * MathLib.floorLog2(d)); double pow10 = MathLib.toDoublePow10(1, guess); if ((pow10 <= d) && (pow10 * 10 > d)) return guess; if (pow10 > d) return guess - 1; return guess + 1; }
/** * Returns the largest power of 10 that is less than or equal to the * the specified positive value. * * @param d the <code>double</code> number. * @return <code>floor(Log10(abs(d)))</code> * @throws ArithmeticException if <code>d <= 0<code> or <code>d</code> * is <code>NaN</code> or <code>Infinity</code>. **/ public static int floorLog10(double d) { int guess = (int) (LOG2_DIV_LOG10 * MathLib.floorLog2(d)); double pow10 = MathLib.toDoublePow10(1, guess); if ((pow10 <= d) && (pow10 * 10 > d)) return guess; if (pow10 > d) return guess - 1; return guess + 1; }
/** * Returns the largest power of 10 that is less than or equal to the * the specified positive value. * * @param d the <code>double</code> number. * @return <code>floor(Log10(abs(d)))</code> * @throws ArithmeticException if <code>d <= 0<code> or <code>d</code> * is <code>NaN</code> or <code>Infinity</code>. **/ public static int floorLog10(double d) { int guess = (int) (LOG2_DIV_LOG10 * MathLib.floorLog2(d)); double pow10 = MathLib.toDoublePow10(1, guess); if ((pow10 <= d) && (pow10 * 10 > d)) return guess; if (pow10 > d) return guess - 1; return guess + 1; } private static final double LOG2_DIV_LOG10 = 0.3010299956639811952137388947;
/** * Returns the largest power of 10 that is less than or equal to the * the specified positive value. * * @param d the <code>double</code> number. * @return <code>floor(Log10(abs(d)))</code> * @throws ArithmeticException if <code>d <= 0<code> or <code>d</code> * is <code>NaN</code> or <code>Infinity</code>. **/ public static int floorLog10(double d) { int guess = (int) (LOG2_DIV_LOG10 * MathLib.floorLog2(d)); double pow10 = MathLib.toDoublePow10(1, guess); if ((pow10 <= d) && (pow10 * 10 > d)) return guess; if (pow10 > d) return guess - 1; return guess + 1; }