Class FastFloatMath


  • final class FastFloatMath
    extends java.lang.Object
    This class complements FastDoubleMath with methods for converting FloatingPointLiteral productions to floats.

    See JavaDoubleParser for a description of FloatingPointLiteral.

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private FastFloatMath()
      Don't let anyone instantiate this class.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static float fastScalb​(float number, int scaleFactor)
      This is a faster alternative to Math.scalb(float, int).
      (package private) static float tryDecFloatToFloatTruncated​(boolean isNegative, long significand, int exponent, boolean isSignificandTruncated, int exponentOfTruncatedSignificand)  
      (package private) static float tryDecToFloatWithFastAlgorithm​(boolean isNegative, long significand, int power)
      Attempts to compute digits * 10^(power) exactly; and if "negative" is true, negate the result.
      (package private) static float tryHexFloatToFloatTruncated​(boolean isNegative, long significand, int exponent, boolean isSignificandTruncated, int exponentOfTruncatedSignificand)
      Tries to compute significand * 2^exponent exactly using a fast algorithm; and if isNegative is true, negate the result; the significand can be truncated.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • FLOAT_EXPONENT_BIAS

        private static final int FLOAT_EXPONENT_BIAS
        Bias used in the exponent of a float.
        See Also:
        Constant Field Values
      • FLOAT_SIGNIFICAND_WIDTH

        private static final int FLOAT_SIGNIFICAND_WIDTH
        The number of bits in the significand, including the implicit bit.
        See Also:
        Constant Field Values
      • FLOAT_MIN_EXPONENT_POWER_OF_TEN

        private static final int FLOAT_MIN_EXPONENT_POWER_OF_TEN
        See Also:
        Constant Field Values
      • FLOAT_MAX_EXPONENT_POWER_OF_TEN

        private static final int FLOAT_MAX_EXPONENT_POWER_OF_TEN
        See Also:
        Constant Field Values
      • FLOAT_MIN_EXPONENT_POWER_OF_TWO

        private static final int FLOAT_MIN_EXPONENT_POWER_OF_TWO
        See Also:
        Constant Field Values
      • FLOAT_MAX_EXPONENT_POWER_OF_TWO

        private static final int FLOAT_MAX_EXPONENT_POWER_OF_TWO
        See Also:
        Constant Field Values
      • FLOAT_POWER_OF_TEN

        private static final float[] FLOAT_POWER_OF_TEN
        Precomputed powers of ten from 10^0 to 10^10. These can be represented exactly using the float type.
    • Constructor Detail

      • FastFloatMath

        private FastFloatMath()
        Don't let anyone instantiate this class.
    • Method Detail

      • tryDecFloatToFloatTruncated

        static float tryDecFloatToFloatTruncated​(boolean isNegative,
                                                 long significand,
                                                 int exponent,
                                                 boolean isSignificandTruncated,
                                                 int exponentOfTruncatedSignificand)
      • tryHexFloatToFloatTruncated

        static float tryHexFloatToFloatTruncated​(boolean isNegative,
                                                 long significand,
                                                 int exponent,
                                                 boolean isSignificandTruncated,
                                                 int exponentOfTruncatedSignificand)
        Tries to compute significand * 2^exponent exactly using a fast algorithm; and if isNegative is true, negate the result; the significand can be truncated.
        Parameters:
        isNegative - true if the sign is negative
        significand - the significand (unsigned long, uint64)
        exponent - the exponent number (the power)
        isSignificandTruncated - true if significand has been truncated
        exponentOfTruncatedSignificand - the exponent number of the truncated significand
        Returns:
        the double value, or Double.NaN if the fast path failed.
      • tryDecToFloatWithFastAlgorithm

        static float tryDecToFloatWithFastAlgorithm​(boolean isNegative,
                                                    long significand,
                                                    int power)
        Attempts to compute digits * 10^(power) exactly; and if "negative" is true, negate the result.

        This function will only work in some cases, when it does not work it returns null. This should work *most of the time* (like 99% of the time). We assume that power is in the [-325, 308] interval: the caller is responsible for this check.

        References:

        Noble Mushtak, Daniel Lemire. (2023) Fast Number Parsing Without Fallback.
        arxiv.org
        Parameters:
        isNegative - whether the number is negative
        significand - uint64 the significand
        power - int32 the exponent of the number
        Returns:
        the computed double on success, Double.NaN on failure
      • fastScalb

        static float fastScalb​(float number,
                               int scaleFactor)
        This is a faster alternative to Math.scalb(float, int).

        This method only works if scaleFactor is within the range of Float.MIN_EXPONENT through Float.MAX_EXPONENT (inclusive), so that we do not underflow or overflow.

        Parameters:
        number - a double number
        scaleFactor - the scale factor
        Returns:
        number × 2scaleFactor