java.lang.Object
org.kink_lang.kink.HostResultCore
org.kink_lang.kink.Val
org.kink_lang.kink.NumVal
- All Implemented Interfaces:
Comparable<NumVal>,HostResult,Just<NumVal>,OpResult<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:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe number of bits of the mantissa.static final BigIntegerThe max value of Mantissa attribute.static final intThe max value of Scale attribute.static final BigIntegerThe min value of Mantissa attribute. -
Method Summary
Modifier and TypeMethodDescriptionReturns this + arg.Returns the bitwise and.abstract BigDecimalReturns the big decimal number.abstract Optional<BigInteger> Returns the BigInteger representation, if the scale is zero.intbooleanReturns whetherargis a NumVal, this.vm == arg.vm, and this.bigDecimal().equals(arg.bigDecimal()).inthashCode()Returns the integral quotient of this//divisor.abstract OptionalIntintValue()Returns the int value representation, if the scale is zero and the value fits in the range of int.just()Returns this.abstract OptionalLongReturns the long value representation, if the scale is zero and the value fits in the range of long.abstract NumValmantissa()Returns the mantissa of the number.Returns this * arg.abstract NumValnegate()Returns -this.not()Returns the bitwise not of this number.Returns the bitwise or.Returns the remainder of division of this by divisor.abstract intscale()Returns the scale of the number.Returnsthis << count.shiftRight(NumVal count) Returnsthis >> count.abstract intsignum()Returns -1/0/1 for the negative/0/positive numbers.Returns this - arg.toString()Returns the result of Num.repr.Returns the bitwise xor.Methods inherited from class org.kink_lang.kink.Val
getVar, getVarSymHandleSet, hasVar, makeHostResultCore, setVarMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.kink_lang.kink.op.Just
justOrNull
-
Field Details
-
MAX_SCALE
public static final int MAX_SCALEThe max value of Scale attribute.- See Also:
-
MANTISSA_BITS
public static final int MANTISSA_BITSThe number of bits of the mantissa.- See Also:
-
MAX_MANTISSA
The max value of Mantissa attribute. -
MIN_MANTISSA
The min value of Mantissa attribute.
-
-
Method Details
-
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
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
Returns the BigInteger representation, if the scale is zero.- Returns:
- the BigInteger value representation.
-
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
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
Returns -this.- Returns:
- -this.
-
just
Returns this. -
add
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
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
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
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
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
Returns the bitwise or. If either operand is not an integer, or overflow happens, returns anOpError.- Parameters:
arg- the right operand.- Returns:
- the bitwise or.
-
xor
Returns the bitwise xor. If either operand is not an integer, or overflow happens, returns anOpError.- Parameters:
arg- the right operand.- Returns:
- the bitwise xor.
-
and
Returns the bitwise and. If either operand is not an integer, or overflow happens, returns anOpError.- Parameters:
arg- the right operand.- Returns:
- the bitwise and.
-
not
Returns the bitwise not of this number. If the number is not an integer, or overflow happens, returns anOpError.- Returns:
- the bitwise not of this number.
-
shiftLeft
Returnsthis << count. If either operand is not an integer, or overflow happens, returns anOpError.- Parameters:
count- the count of bits.- Returns:
this << count.
-
shiftRight
Returnsthis >> count. If either operand is not an integer, or overflow happens, returns anOpError.- Parameters:
count- the count of bits.- Returns:
this >> count.
-
compareTo
- Specified by:
compareToin interfaceComparable<NumVal>
-
toString
Returns the result of Num.repr. -
hashCode
public int hashCode() -
equals
Returns whetherargis a NumVal, this.vm == arg.vm, and this.bigDecimal().equals(arg.bigDecimal()).
-