Class NumVal

All Implemented Interfaces:
Comparable<NumVal>, HostResult, Just<NumVal>, OpResult<NumVal>

public abstract sealed class NumVal extends Val implements Comparable<NumVal>, Just<NumVal>
A num value.

A num value consists of two integer attributes: Mantissa and Scale. The num value represents an decimal number (Mantissa * 10**(-Scale)). Kink runtimes must support at least the range [-(2^8192), (2^8192)-1] for Mantissa, and at least the range [0, 32767] for Scale.

In this implementation, a num value is backed by a Java BigDecimal. Mantissa is the “unscaled value” of BigDecimal, and Scale is the “scale” of BigDecimal.

See Also:
  • Field Details

    • MAX_SCALE

      public static final int MAX_SCALE
      The max value of Scale attribute.
      See Also:
    • MANTISSA_BITS

      public static final int MANTISSA_BITS
      The number of bits of the mantissa.
      See Also:
    • MAX_MANTISSA

      public static final BigInteger MAX_MANTISSA
      The max value of Mantissa attribute.
    • MIN_MANTISSA

      public static final BigInteger MIN_MANTISSA
      The min value of Mantissa attribute.
  • Method Details

    • intValue

      public abstract OptionalInt intValue()
      Returns the int value representation, if the scale is zero and the value fits in the range of int.
      Returns:
      the int value representation.
    • longValue

      public abstract OptionalLong longValue()
      Returns the long value representation, if the scale is zero and the value fits in the range of long.
      Returns:
      the long value representation.
    • bigInteger

      public abstract Optional<BigInteger> bigInteger()
      Returns the BigInteger representation, if the scale is zero.
      Returns:
      the BigInteger value representation.
    • bigDecimal

      public abstract BigDecimal bigDecimal()
      Returns the big decimal number.
      Returns:
      the big decimal number.
    • scale

      public abstract int scale()
      Returns the scale of the number.
      Returns:
      the scale of the number.
    • mantissa

      public abstract NumVal mantissa()
      Returns the mantissa of the number.
      Returns:
      the mantissa of the number.
    • signum

      public abstract int signum()
      Returns -1/0/1 for the negative/0/positive numbers.
      Returns:
      -1/0/1 for the negative/0/positive numbers.
    • negate

      public abstract NumVal negate()
      Returns -this.
      Returns:
      -this.
    • just

      public NumVal just()
      Returns this.
      Specified by:
      just in interface Just<NumVal>
      Returns:
      this.
    • add

      public OpResult<NumVal> add(NumVal arg)
      Returns this + arg. Scale of the result will be max(this.scale(), arg.scale()). If overflow happens, the method returns OpError.
      Parameters:
      arg - the argument number.
      Returns:
      this + arg.
    • subtract

      public OpResult<NumVal> subtract(NumVal arg)
      Returns this - arg. Scale of the result will be max(this.scale(), arg.scale()). If overflow happens, the method returns OpError.
      Parameters:
      arg - the argument number.
      Returns:
      this - arg.
    • multiply

      public OpResult<NumVal> multiply(NumVal arg)
      Returns this * arg. Scale of the result will be this.scale() + arg.scale(). If overflow happens, the method returns OpError.
      Parameters:
      arg - the argument number.
      Returns:
      this * arg.
    • intDivide

      public OpResult<NumVal> intDivide(NumVal divisor)
      Returns the integral quotient of this//divisor. If divisor is equal to 0, or overflow happens, the method returns OpError.

      Post conditions:

      • (this//divisor).scale == 0
      • this//divisor * divisor + this%divisor == this
      Parameters:
      divisor - the second operand.
      Returns:
      the integral quotient of this//divisor.
    • remainder

      public OpResult<NumVal> remainder(NumVal divisor)
      Returns the remainder of division of this by divisor. If divisor is equal to 0, or overflow happens, the method returns OpError.

      Post conditions:

      • (this%divisor).scale == max(this.scale, divisor.scale)
      • this//divisor * divisor + this%divisor == this
      Parameters:
      divisor - the second operand.
      Returns:
      the remainder of integral division of this by divisor.
    • or

      public OpResult<NumVal> or(NumVal arg)
      Returns the bitwise or. If either operand is not an integer, or overflow happens, returns an OpError.
      Parameters:
      arg - the right operand.
      Returns:
      the bitwise or.
    • xor

      public OpResult<NumVal> xor(NumVal arg)
      Returns the bitwise xor. If either operand is not an integer, or overflow happens, returns an OpError.
      Parameters:
      arg - the right operand.
      Returns:
      the bitwise xor.
    • and

      public OpResult<NumVal> and(NumVal arg)
      Returns the bitwise and. If either operand is not an integer, or overflow happens, returns an OpError.
      Parameters:
      arg - the right operand.
      Returns:
      the bitwise and.
    • not

      public abstract OpResult<NumVal> not()
      Returns the bitwise not of this number. If the number is not an integer, or overflow happens, returns an OpError.
      Returns:
      the bitwise not of this number.
    • shiftLeft

      public OpResult<NumVal> shiftLeft(NumVal count)
      Returns this << count. If either operand is not an integer, or overflow happens, returns an OpError.
      Parameters:
      count - the count of bits.
      Returns:
      this << count.
    • shiftRight

      public OpResult<NumVal> shiftRight(NumVal count)
      Returns this >> count. If either operand is not an integer, or overflow happens, returns an OpError.
      Parameters:
      count - the count of bits.
      Returns:
      this >> count.
    • compareTo

      public int compareTo(NumVal arg)
      Specified by:
      compareTo in interface Comparable<NumVal>
    • toString

      public String toString()
      Returns the result of Num.repr.
      Overrides:
      toString in class Object
      Returns:
      the result of Num.repr.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object arg)
      Returns whether arg is a NumVal, this.vm == arg.vm, and this.bigDecimal().equals(arg.bigDecimal()).
      Overrides:
      equals in class Object
      Parameters:
      arg - the arg to test.
      Returns:
      whether arg is a NumVal, this.vm == arg.vm, and this.bigDecimal().equals(arg.bigDecimal())