Module ieee754java

Class Rounding

java.lang.Object
at.syntaxerror.ieee754.rounding.Rounding

public abstract class Rounding extends Object
This class is used for rounding IEEE 754 floating-point numbers.

There are 5 predefined rounding modes:

DEFAULT_ROUNDING controls the default rounding mode used by FloatingCodec.encode(Floating) and its descendents.
Author:
Thomas Kasper
  • Field Details

    • TIES_EVEN

      public static final Rounding 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

      public static final Rounding TIES_AWAY
      Round to nearest, ties away from zero

      Rounds 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

      public static final Rounding TOWARD_ZERO
      Round toward 0

      Rounds toward zero. Effectively discards any decimal places.

    • TOWARD_POSITIVE

      public static final Rounding TOWARD_POSITIVE
      Round toward +∞

      Rounds toward positive infinity, also known as rounding up or ceiling.

    • TOWARD_NEGATIVE

      public static final Rounding TOWARD_NEGATIVE
      Round toward -∞

      Rounds toward negative infinity, also known as rounding down or floor.

    • DEFAULT_ROUNDING

      @NonNull public static @NonNull Rounding DEFAULT_ROUNDING
      The rounding method used by FloatingCodec.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 returns true when rounding up and false 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 bit
      guard - the guard bit
      round - the round bit
      sticky - the sticky bit
      Returns:
      whether the number is rounded up
    • roundDecimal

      public abstract BigDecimal roundDecimal(BigDecimal value)
      Rounds the BigDecimal (to 0 decimal places) according to the rounding mode's rules
      Parameters:
      value - the input value
      Returns:
      the rounded value