java.lang.Object
at.syntaxerror.ieee754.rounding.Rounding
This class is used for rounding
IEEE 754 floating-point numbers.
There are 5 predefined rounding modes:
round to nearest, ties to evenround to nearest, ties away from zeroround toward 0round toward +∞round toward -∞
DEFAULT_ROUNDING controls the default rounding mode used by FloatingCodec.encode(Floating) and its descendents.- Author:
- Thomas Kasper
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic @NonNull RoundingThe rounding method used byFloatingCodec.encode(at.syntaxerror.ieee754.Floating)and its descendents.static final RoundingRound to nearest, ties away from zerostatic final RoundingRound to nearest, ties to even.static final RoundingRound toward -∞static final RoundingRound toward +∞static final RoundingRound toward 0 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract booleanroundBinary(boolean sign, boolean guard, boolean round, boolean sticky) Rounds a binary floating-point number.abstract BigDecimalroundDecimal(BigDecimal value) Rounds theBigDecimal(to 0 decimal places) according to the rounding mode's rules
-
Field Details
-
TIES_EVEN
Round to nearest, ties to even.Rounds to the nearest value. If the number falls midway, it is rounded to the nearest even value.
-
TIES_AWAY
Round to nearest, ties away from zeroRounds to the nearest value. If the number falls midway, it is rounded to the nearest value above (positive numbers) or below (negative numbers).
-
TOWARD_ZERO
Round toward 0Rounds toward zero. Effectively discards any decimal places.
-
TOWARD_POSITIVE
Round toward +∞Rounds toward positive infinity, also known as rounding up or ceiling.
-
TOWARD_NEGATIVE
Round toward -∞Rounds toward negative infinity, also known as rounding down or floor.
-
DEFAULT_ROUNDING
The rounding method used byFloatingCodec.encode(at.syntaxerror.ieee754.Floating)and its descendents.
-
-
Constructor Details
-
Rounding
public Rounding()
-
-
Method Details
-
roundBinary
public abstract boolean roundBinary(boolean sign, boolean guard, boolean round, boolean sticky) Rounds a binary floating-point number. The method returnstruewhen rounding up andfalsewhen rounding down.The sign bit specifies whether the number is negative. The guard bit is the least significant bit than can be stored (the ULP). The round bit is the bit following the guard bit. The sticky bit is the bitwise OR of all other following bits.
- Parameters:
sign- the sign bitguard- the guard bitround- the round bitsticky- the sticky bit- Returns:
- whether the number is rounded up
-
roundDecimal
Rounds theBigDecimal(to 0 decimal places) according to the rounding mode's rules- Parameters:
value- the input value- Returns:
- the rounded value
-