Module ieee754java

Class BinaryCodec<T extends Binary<T>>

java.lang.Object
at.syntaxerror.ieee754.FloatingCodec<T>
at.syntaxerror.ieee754.binary.BinaryCodec<T>

public final class BinaryCodec<T extends Binary<T>> extends FloatingCodec<T>
This class represents a codec capable of encoding and decoding IEEE 754 binary floating point numbers as well as computing common values (such as NaN, maximum value, ...)
Author:
Thomas Kasper
  • Constructor Details

    • BinaryCodec

      public BinaryCodec(int exponent, int significand, boolean implicit, @NonNull @NonNull FloatingFactory<T> factory)
      Creates a new binary codec
      Parameters:
      exponent - the number of exponent bits (> 0, < 32)
      significand - the number of significand bits (> 0)
      implicit - whether there is an implicit significand bit
      factory - the factory for creating Binary objects
  • Method Details

    • getExponentBits

      public int getExponentBits()
      Returns the number of bits occupied by the exponent
      Returns:
      the number of exponent's bits
    • getSignificandBits

      public int getSignificandBits()
      Returns the number of bits occupied by the significand
      Returns:
      the number of significand's bits
    • isImplicit

      public boolean isImplicit()
      Returns whether there is an implicit bit used for the binary representation
      Returns:
      whether there is an implicit bit
    • encode

      public BigInteger encode(T value)
      Encodes the floating point into its binary representation
      Specified by:
      encode in class FloatingCodec<T extends Binary<T>>
      Parameters:
      value - the floating point number
      Returns:
      the encoded binary representation
    • decode

      public T decode(BigInteger value)
      Decodes the floating point's binary representation
      Specified by:
      decode in class FloatingCodec<T extends Binary<T>>
      Parameters:
      value - the binary representation
      Returns:
      the decoded floating point number
    • getBias

      public int getBias()
      Returns the exponent bias
      Returns:
      the bias
    • getUnbiasedExponent

      public BigInteger getUnbiasedExponent(BigInteger value)
      Returns the value's unbiased exponent part
      Parameters:
      value - the value
      Returns:
      the unbiased exponent
    • getExponent

      public BigInteger getExponent(BigInteger value)
      Returns the value's exponent part
      Parameters:
      value - the value
      Returns:
      the exponent
    • getSignificand

      public BigInteger getSignificand(BigInteger value)
      Returns the value's significand part.

      If there is an explicit bit, it is not included

      Parameters:
      value - the value
      Returns:
      the significand
    • getFullSignificand

      public BigInteger getFullSignificand(BigInteger value)
      Returns the value's significand part.

      If there is an explicit bit, it is included

      Parameters:
      value - the value
      Returns:
      the significand
    • isPositive

      public boolean isPositive(BigInteger value)
      Checks if the value is positive
      Specified by:
      isPositive in class FloatingCodec<T extends Binary<T>>
      Parameters:
      value - the value
      Returns:
      whether the value is positive
    • isNegative

      public boolean isNegative(BigInteger value)
      Checks if the value is negative
      Specified by:
      isNegative in class FloatingCodec<T extends Binary<T>>
      Parameters:
      value - the value
      Returns:
      whether the value is negative
    • isInfinity

      public boolean isInfinity(BigInteger value)
      Checks if the value is Infinity's binary representation
      Specified by:
      isInfinity in class FloatingCodec<T extends Binary<T>>
      Parameters:
      value - the value
      Returns:
      whether the value is Infinity
    • isPositiveInfinity

      public boolean isPositiveInfinity(BigInteger value)
      Checks if the value is +Infinity's binary representation
      Specified by:
      isPositiveInfinity in class FloatingCodec<T extends Binary<T>>
      Parameters:
      value - the value
      Returns:
      whether the value is +Infinity
    • isNegativeInfinity

      public boolean isNegativeInfinity(BigInteger value)
      Checks if the value is -Infinity's binary representation
      Specified by:
      isNegativeInfinity in class FloatingCodec<T extends Binary<T>>
      Parameters:
      value - the value
      Returns:
      whether the value is -Infinity
    • getPositiveInfinity

      public BigInteger getPositiveInfinity()
      Returns +Infinity's (like Double.POSITIVE_INFINITY) binary representation
      Specified by:
      getPositiveInfinity in class FloatingCodec<T extends Binary<T>>
      Returns:
      +Infinity
    • getNegativeInfinity

      public BigInteger getNegativeInfinity()
      Returns -Infinity's (like Double.NEGATIVE_INFINITY) binary representation
      Specified by:
      getNegativeInfinity in class FloatingCodec<T extends Binary<T>>
      Returns:
      -Infinity
    • isNaN

      public boolean isNaN(BigInteger value)
      Checks if the value is NaN's binary representation
      Specified by:
      isNaN in class FloatingCodec<T extends Binary<T>>
      Parameters:
      value - the value
      Returns:
      whether the value is NaN
    • isQuietNaN

      public boolean isQuietNaN(BigInteger value)
      Checks if the value is qNaN's binary representation
      Specified by:
      isQuietNaN in class FloatingCodec<T extends Binary<T>>
      Parameters:
      value - the value
      Returns:
      whether the value is qNaN
    • isSignalingNaN

      public boolean isSignalingNaN(BigInteger value)
      Checks if the value is sNaN's binary representation
      Specified by:
      isSignalingNaN in class FloatingCodec<T extends Binary<T>>
      Parameters:
      value - the value
      Returns:
      whether the value is sNaN
    • getQuietNaN

      public BigInteger getQuietNaN(int signum)
      Returns qNaN binary representation (on most processors)
      Specified by:
      getQuietNaN in class FloatingCodec<T extends Binary<T>>
      Parameters:
      signum - the signum
      Returns:
      qNaN
    • getSignalingNaN

      public BigInteger getSignalingNaN(int signum)
      Returns sNaN's binary representation (on most processors)
      Specified by:
      getSignalingNaN in class FloatingCodec<T extends Binary<T>>
      Parameters:
      signum - the signum
      Returns:
      sNaN
    • getNaN

      public BigInteger getNaN(int signum)
      Returns NaN (qNaN on most processors)
      Specified by:
      getNaN in class FloatingCodec<T extends Binary<T>>
      Returns:
      NaN
    • getZero

      public BigInteger getZero(int signum)
      Returns (possibly negative) zero's binary representation
      Specified by:
      getZero in class FloatingCodec<T extends Binary<T>>
      Returns:
      zero
    • getMinSubnormalValue

      public T getMinSubnormalValue()
      Returns the smallest postive (> 0) subnormal value
      Specified by:
      getMinSubnormalValue in class FloatingCodec<T extends Binary<T>>
      Returns:
      the smallest postive value
    • getMinValue

      public T getMinValue()
      Returns the smallest postive (> 0) normalized value.
      Specified by:
      getMinValue in class FloatingCodec<T extends Binary<T>>
      Returns:
      the smallest postive value
    • getMaxValue

      public T getMaxValue()
      Returns the largest possible value
      Specified by:
      getMaxValue in class FloatingCodec<T extends Binary<T>>
      Returns:
      the largest value
    • getEpsilon

      public BigDecimal getEpsilon()
      Returns the difference between 1 and the smallest number greater than 1
      Specified by:
      getEpsilon in class FloatingCodec<T extends Binary<T>>
      Returns:
      the difference
    • getExponentRange

      public Map.Entry<Integer,Integer> getExponentRange()
      Returns the smallest and largest possible exponent
      Specified by:
      getExponentRange in class FloatingCodec<T extends Binary<T>>
      Returns:
      the smallest and largest exponent
    • get10ExponentRange

      public Map.Entry<Integer,Integer> get10ExponentRange()
      Returns the smallest and largest possible exponent so that 10 to the power of the exponent is a normalized number
      Returns:
      the smallest and largest exponent
    • getDecimalDigits

      public int getDecimalDigits()
      Computes the number of decimal digits that can be converted back and forth without loss of precision
      Returns:
      the number of decimal digits