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 even
round to nearest, ties away from zero
round toward 0
round 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 Rounding
The rounding method used byFloatingCodec.encode(at.syntaxerror.ieee754.Floating)
and its descendents.static final Rounding
Round to nearest, ties away from zerostatic final Rounding
Round to nearest, ties to even.static final Rounding
Round toward -∞static final Rounding
Round toward +∞static final Rounding
Round toward 0 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract boolean
roundBinary
(boolean sign, boolean guard, boolean round, boolean sticky) Rounds a binary floating-point number.abstract BigDecimal
roundDecimal
(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 returnstrue
when rounding up andfalse
when 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
-