Package ch.randelshofer.fastdoubleparser
Class FastFloatMath
- java.lang.Object
-
- ch.randelshofer.fastdoubleparser.FastFloatMath
-
final class FastFloatMath extends java.lang.ObjectThis class complementsFastDoubleMathwith methods for convertingFloatingPointLiteralproductions to floats.See
JavaDoubleParserfor a description ofFloatingPointLiteral.
-
-
Field Summary
Fields Modifier and Type Field Description private static intFLOAT_EXPONENT_BIASBias used in the exponent of a float.private static intFLOAT_MAX_EXPONENT_POWER_OF_TENprivate static intFLOAT_MAX_EXPONENT_POWER_OF_TWOprivate static intFLOAT_MIN_EXPONENT_POWER_OF_TENprivate static intFLOAT_MIN_EXPONENT_POWER_OF_TWOprivate static float[]FLOAT_POWER_OF_TENPrecomputed powers of ten from 10^0 to 10^10.private static intFLOAT_SIGNIFICAND_WIDTHThe number of bits in the significand, including the implicit bit.
-
Constructor Summary
Constructors Modifier Constructor Description privateFastFloatMath()Don't let anyone instantiate this class.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static floatfastScalb(float number, int scaleFactor)This is a faster alternative toMath.scalb(float, int).(package private) static floattryDecFloatToFloatTruncated(boolean isNegative, long significand, int exponent, boolean isSignificandTruncated, int exponentOfTruncatedSignificand)(package private) static floattryDecToFloatWithFastAlgorithm(boolean isNegative, long significand, int power)Attempts to compute digits * 10^(power) exactly; and if "negative" is true, negate the result.(package private) static floattryHexFloatToFloatTruncated(boolean isNegative, long significand, int exponent, boolean isSignificandTruncated, int exponentOfTruncatedSignificand)Tries to computesignificand * 2^exponentexactly using a fast algorithm; and ifisNegativeis true, negate the result; the significand can be truncated.
-
-
-
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.
-
-
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 computesignificand * 2^exponentexactly using a fast algorithm; and ifisNegativeis true, negate the result; the significand can be truncated.- Parameters:
isNegative- true if the sign is negativesignificand- the significand (unsigned long, uint64)exponent- the exponent number (the power)isSignificandTruncated- true if significand has been truncatedexponentOfTruncatedSignificand- the exponent number of the truncated significand- Returns:
- the double value,
or
Double.NaNif 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 negativesignificand- uint64 the significandpower- int32 the exponent of the number- Returns:
- the computed double on success,
Double.NaNon failure
-
fastScalb
static float fastScalb(float number, int scaleFactor)This is a faster alternative toMath.scalb(float, int).This method only works if scaleFactor is within the range of
Float.MIN_EXPONENTthroughFloat.MAX_EXPONENT(inclusive), so that we do not underflow or overflow.- Parameters:
number- a double numberscaleFactor- the scale factor- Returns:
- number × 2scaleFactor
-
-