Class FastFloatMath
java.lang.Object
ch.randelshofer.fastdoubleparser.FastFloatMath
This class complements
FastDoubleMath with methods for
converting FloatingPointLiteral productions to floats.
See JavaDoubleParser for a description of
FloatingPointLiteral.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intBias used in the exponent of a float.private static final intprivate static final intprivate static final intprivate static final intprivate static final float[]Precomputed powers of ten from 10^0 to 10^10.private static final intThe number of bits in the significand, including the implicit bit. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(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 Details
-
FLOAT_EXPONENT_BIAS
private static final int FLOAT_EXPONENT_BIASBias used in the exponent of a float.- See Also:
-
FLOAT_SIGNIFICAND_WIDTH
private static final int FLOAT_SIGNIFICAND_WIDTHThe number of bits in the significand, including the implicit bit.- See Also:
-
FLOAT_MIN_EXPONENT_POWER_OF_TEN
private static final int FLOAT_MIN_EXPONENT_POWER_OF_TEN- See Also:
-
FLOAT_MAX_EXPONENT_POWER_OF_TEN
private static final int FLOAT_MAX_EXPONENT_POWER_OF_TEN- See Also:
-
FLOAT_MIN_EXPONENT_POWER_OF_TWO
private static final int FLOAT_MIN_EXPONENT_POWER_OF_TWO- See Also:
-
FLOAT_MAX_EXPONENT_POWER_OF_TWO
private static final int FLOAT_MAX_EXPONENT_POWER_OF_TWO- See Also:
-
FLOAT_POWER_OF_TEN
private static final float[] FLOAT_POWER_OF_TENPrecomputed powers of ten from 10^0 to 10^10. These can be represented exactly using the float type.
-
-
Constructor Details
-
FastFloatMath
private FastFloatMath()Don't let anyone instantiate this class.
-
-
Method Details
-
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
-