Package com.upokecenter.cbor
Class CBORNumber
java.lang.Object
com.upokecenter.cbor.CBORNumber
- All Implemented Interfaces:
Comparable<CBORNumber>
An instance of a number that CBOR or certain CBOR tags can represent. For
this purpose, infinities and not-a-number or NaN values are considered
numbers. Currently, this class can store one of the following kinds of
numbers: 64-bit signed integers or binary floating-point numbers; or
arbitrary-precision integers, decimal numbers, binary numbers, or rational
numbers.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumSpecifies the underlying form of this CBOR number object. -
Method Summary
Modifier and TypeMethodDescriptionAbs()Returns the absolute value of this CBOR number.Add(CBORNumber b) Returns the sum of this number and another number.booleanReturns whether this object's value can be converted to a 64-bit floating point number without its value being rounded to another numerical value.booleanReturns whether this object's numerical value is an integer, is -(2^31) or greater, and is less than 2^31.booleanReturns whether this object's numerical value is an integer, is -(2^63) or greater, and is less than 2^63.booleanReturns whether this object's value can be converted to a 32-bit floating point number without its value being rounded to another numerical value.booleanReturns whether this object's numerical value is an integer, is 0 or greater, and is less than 2^64.booleanReturns whether this object's value, converted to an integer by discarding its fractional part, would be -(2^31) or greater, and less than 2^31.booleanReturns whether this object's value, converted to an integer by discarding its fractional part, would be -(2^63) or greater, and less than 2^63.booleanReturns whether this object's value, converted to an integer by discarding its fractional part, would be 0 or greater, and less than 2^64.intcompareTo(int other) Compares this CBOR number with a 32-bit signed integer.intcompareTo(long other) Compares this CBOR number with a 64-bit signed integer.intcompareTo(CBORNumber other) Compares this CBOR number with another.Divide(CBORNumber b) Returns the quotient of this number and another number.static CBORNumberFromByte(byte inputByte) Converts a byte (from 0 to 255) to an arbitrary-precision decimal number.static CBORNumberCreates a CBOR number object from a CBOR object representing a number (that is, one for which the IsNumber property in.NET or the isNumber() method in Java returns true).static CBORNumberFromInt16(short inputInt16) Converts a 16-bit signed integer to an arbitrary-precision decimal number.final CBORNumber.NumberKindgetKind()Gets the underlying form of this CBOR number object.booleanIsFinite()Gets a value indicating whether this CBOR object represents a finite number.booleanGets a value indicating whether this object represents infinity.booleanGets a value indicating whether this object represents an integer number, that is, a number without a fractional part.booleanIsNaN()Gets a value indicating whether this object represents a not-a-number value.booleanGets a value indicating whether this object is a negative number.booleanGets a value indicating whether this object represents negative infinity.booleanGets a value indicating whether this object represents positive infinity.booleanIsZero()Gets a value indicating whether this object's value equals 0.Returns a CBOR number expressing the product of this number and the given number.Negate()Returns a CBOR number with the same value as this one but with the sign reversed.Returns the remainder when this number is divided by another number.final intsignum()Gets this value's sign: -1 if nonzero and negative; 1 if nonzero and positive; 0 if zero.Returns a number that expresses this number minus another.byteConverts this number's value to a byte (from 0 to 255) if it can fit in a byte (from 0 to 255) after converting it to an integer by discarding its fractional part.byteConverts this number's value to a byte (from 0 to 255) if it can fit in a byte (from 0 to 255) without rounding to a different numerical value.byteConverts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a byte (from 0 to 255).Converts this object's value to a CBOR object.com.upokecenter.numbers.EDecimalConverts this object to a decimal number.com.upokecenter.numbers.EFloatToEFloat()Converts this object to an arbitrary-precision binary floating point number.com.upokecenter.numbers.EIntegerConverts this object to an arbitrary-precision integer.com.upokecenter.numbers.EIntegerConverts this object to an arbitrary-precision integer if its value is an integer.com.upokecenter.numbers.ERationalConverts this object to a rational number.shortConverts this number's value to a 16-bit signed integer if it can fit in a 16-bit signed integer after converting it to an integer by discarding its fractional part.shortConverts this number's value to a 16-bit signed integer if it can fit in a 16-bit signed integer without rounding to a different numerical value.shortConverts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a 16-bit signed integer.intConverts this number's value to a 32-bit signed integer if it can fit in a 32-bit signed integer after converting it to an integer by discarding its fractional part.intConverts this number's value to a 32-bit signed integer if it can fit in a 32-bit signed integer without rounding to a different numerical value.intConverts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a 32-bit signed integer.longConverts this number's value to a 64-bit signed integer if it can fit in a 64-bit signed integer after converting it to an integer by discarding its fractional part.longConverts this number's value to a 64-bit signed integer if it can fit in a 64-bit signed integer without rounding to a different numerical value.longConverts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a 64-bit signed integer.toString()Returns the value of this object in text form.
-
Method Details
-
ToCBORObject
Converts this object's value to a CBOR object.- Returns:
- A CBOR object that stores this object's value.
-
signum
public final int signum()Gets this value's sign: -1 if nonzero and negative; 1 if nonzero and positive; 0 if zero. Not-a-number (NaN) values are positive or negative depending on what sign is stored in their underlying forms.- Returns:
- This value's sign.
-
FromCBORObject
Creates a CBOR number object from a CBOR object representing a number (that is, one for which the IsNumber property in.NET or the isNumber() method in Java returns true).- Parameters:
o- The parameter is a CBOR object representing a number.- Returns:
- A CBOR number object, or null if the given CBOR object is null or does not represent a number.
-
getKind
Gets the underlying form of this CBOR number object.- Returns:
- The underlying form of this CBOR number object.
-
CanTruncatedIntFitInInt32
public boolean CanTruncatedIntFitInInt32()Returns whether this object's value, converted to an integer by discarding its fractional part, would be -(2^31) or greater, and less than 2^31.- Returns:
trueif this object's value, converted to an integer by discarding its fractional part, would be -(2^31) or greater, and less than 2^31; otherwise,false.
-
CanTruncatedIntFitInInt64
public boolean CanTruncatedIntFitInInt64()Returns whether this object's value, converted to an integer by discarding its fractional part, would be -(2^63) or greater, and less than 2^63.- Returns:
trueif this object's value, converted to an integer by discarding its fractional part, would be -(2^63) or greater, and less than 2^63; otherwise,false.
-
CanTruncatedIntFitInUInt64
public boolean CanTruncatedIntFitInUInt64()Returns whether this object's value, converted to an integer by discarding its fractional part, would be 0 or greater, and less than 2^64.- Returns:
trueif this object's value, converted to an integer by discarding its fractional part, would be 0 or greater, and less than 2^64; otherwise,false.
-
CanFitInSingle
public boolean CanFitInSingle()Returns whether this object's value can be converted to a 32-bit floating point number without its value being rounded to another numerical value.- Returns:
trueif this object's value can be converted to a 32-bit floating point number without its value being rounded to another numerical value, or if this is a not-a-number value, even if the value's diagnostic information can' t fit in a 32-bit floating point number; otherwise,false.
-
CanFitInDouble
public boolean CanFitInDouble()Returns whether this object's value can be converted to a 64-bit floating point number without its value being rounded to another numerical value.- Returns:
trueif this object's value can be converted to a 64-bit floating point number without its value being rounded to another numerical value, or if this is a not-a-number value, even if the value's diagnostic information can't fit in a 64-bit floating point number; otherwise,false.
-
IsFinite
public boolean IsFinite()Gets a value indicating whether this CBOR object represents a finite number.- Returns:
trueif this CBOR object represents a finite number; otherwise,false.
-
IsInteger
public boolean IsInteger()Gets a value indicating whether this object represents an integer number, that is, a number without a fractional part. Infinity and not-a-number are not considered integers.- Returns:
trueif this object represents an integer number, that is, a number without a fractional part; otherwise,false.
-
IsNegative
public boolean IsNegative()Gets a value indicating whether this object is a negative number.- Returns:
trueif this object is a negative number; otherwise,false.
-
IsZero
public boolean IsZero()Gets a value indicating whether this object's value equals 0.- Returns:
trueif this object's value equals 0; otherwise,false.
-
ToEInteger
public com.upokecenter.numbers.EInteger ToEInteger()Converts this object to an arbitrary-precision integer. See the ToObject overload taking a type for more information.- Returns:
- The closest arbitrary-precision integer to this object.
- Throws:
ArithmeticException- This value is infinity or not-a-number.
-
ToEIntegerIfExact
public com.upokecenter.numbers.EInteger ToEIntegerIfExact()Converts this object to an arbitrary-precision integer if its value is an integer.- Returns:
- The arbitrary-precision integer given by object.
- Throws:
ArithmeticException- This value is infinity or not-a-number or is not an exact integer.
-
ToByteChecked
public byte ToByteChecked()Converts this number's value to a byte (from 0 to 255) if it can fit in a byte (from 0 to 255) after converting it to an integer by discarding its fractional part.- Returns:
- This number's value, truncated to a byte (from 0 to 255).
- Throws:
ArithmeticException- This value is infinity or not-a-number, or the number, once converted to an integer by discarding its fractional part, is less than 0 or greater than 255.
-
ToByteUnchecked
public byte ToByteUnchecked()Converts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a byte (from 0 to 255).- Returns:
- This number, converted to a byte (from 0 to 255). Returns 0 if this value is infinity or not-a-number.
-
ToByteIfExact
public byte ToByteIfExact()Converts this number's value to a byte (from 0 to 255) if it can fit in a byte (from 0 to 255) without rounding to a different numerical value.- Returns:
- This number's value as a byte (from 0 to 255).
- Throws:
ArithmeticException- This value is infinity or not-a-number, is not an exact integer, or is less than 0 or greater than 255.
-
FromByte
Converts a byte (from 0 to 255) to an arbitrary-precision decimal number.- Parameters:
inputByte- The number to convert as a byte (from 0 to 255).- Returns:
- This number's value as an arbitrary-precision decimal number.
-
ToInt16Checked
public short ToInt16Checked()Converts this number's value to a 16-bit signed integer if it can fit in a 16-bit signed integer after converting it to an integer by discarding its fractional part.- Returns:
- This number's value, truncated to a 16-bit signed integer.
- Throws:
ArithmeticException- This value is infinity or not-a-number, or the number, once converted to an integer by discarding its fractional part, is less than -32768 or greater than 32767.
-
ToInt16Unchecked
public short ToInt16Unchecked()Converts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a 16-bit signed integer.- Returns:
- This number, converted to a 16-bit signed integer. Returns 0 if this value is infinity or not-a-number.
-
ToInt16IfExact
public short ToInt16IfExact()Converts this number's value to a 16-bit signed integer if it can fit in a 16-bit signed integer without rounding to a different numerical value.- Returns:
- This number's value as a 16-bit signed integer.
- Throws:
ArithmeticException- This value is infinity or not-a-number, is not an exact integer, or is less than -32768 or greater than 32767.
-
FromInt16
Converts a 16-bit signed integer to an arbitrary-precision decimal number.- Parameters:
inputInt16- The number to convert as a 16-bit signed integer.- Returns:
- This number's value as an arbitrary-precision decimal number.
-
ToInt32Checked
public int ToInt32Checked()Converts this number's value to a 32-bit signed integer if it can fit in a 32-bit signed integer after converting it to an integer by discarding its fractional part.- Returns:
- This number's value, truncated to a 32-bit signed integer.
- Throws:
ArithmeticException- This value is infinity or not-a-number, or the number, once converted to an integer by discarding its fractional part, is less than -2147483648 or greater than 2147483647.
-
ToInt32Unchecked
public int ToInt32Unchecked()Converts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a 32-bit signed integer.- Returns:
- This number, converted to a 32-bit signed integer. Returns 0 if this value is infinity or not-a-number.
-
ToInt32IfExact
public int ToInt32IfExact()Converts this number's value to a 32-bit signed integer if it can fit in a 32-bit signed integer without rounding to a different numerical value.- Returns:
- This number's value as a 32-bit signed integer.
- Throws:
ArithmeticException- This value is infinity or not-a-number, is not an exact integer, or is less than -2147483648 or greater than 2147483647.
-
ToInt64Checked
public long ToInt64Checked()Converts this number's value to a 64-bit signed integer if it can fit in a 64-bit signed integer after converting it to an integer by discarding its fractional part.- Returns:
- This number's value, truncated to a 64-bit signed integer.
- Throws:
ArithmeticException- This value is infinity or not-a-number, or the number, once converted to an integer by discarding its fractional part, is less than -9223372036854775808 or greater than 9223372036854775807.
-
ToInt64Unchecked
public long ToInt64Unchecked()Converts this number's value to an integer by discarding its fractional part, and returns the least-significant bits of its two's-complement form as a 64-bit signed integer.- Returns:
- This number, converted to a 64-bit signed integer. Returns 0 if this value is infinity or not-a-number.
-
ToInt64IfExact
public long ToInt64IfExact()Converts this number's value to a 64-bit signed integer if it can fit in a 64-bit signed integer without rounding to a different numerical value.- Returns:
- This number's value as a 64-bit signed integer.
- Throws:
ArithmeticException- This value is infinity or not-a-number, is not an exact integer, or is less than -9223372036854775808 or greater than 9223372036854775807.
-
toString
Returns the value of this object in text form. -
CanFitInInt32
public boolean CanFitInInt32()Returns whether this object's numerical value is an integer, is -(2^31) or greater, and is less than 2^31.- Returns:
trueif this object's numerical value is an integer, is -(2^31) or greater, and is less than 2^31; otherwise,false.
-
CanFitInInt64
public boolean CanFitInInt64()Returns whether this object's numerical value is an integer, is -(2^63) or greater, and is less than 2^63.- Returns:
trueif this object's numerical value is an integer, is -(2^63) or greater, and is less than 2^63; otherwise,false.
-
CanFitInUInt64
public boolean CanFitInUInt64()Returns whether this object's numerical value is an integer, is 0 or greater, and is less than 2^64.- Returns:
trueif this object's numerical value is an integer, is 0 or greater, and is less than 2^64; otherwise,false.
-
IsInfinity
public boolean IsInfinity()Gets a value indicating whether this object represents infinity.- Returns:
trueif this object represents infinity; otherwise,false.
-
IsPositiveInfinity
public boolean IsPositiveInfinity()Gets a value indicating whether this object represents positive infinity.- Returns:
trueif this object represents positive infinity; otherwise,false.
-
IsNegativeInfinity
public boolean IsNegativeInfinity()Gets a value indicating whether this object represents negative infinity.- Returns:
trueif this object represents negative infinity; otherwise,false.
-
IsNaN
public boolean IsNaN()Gets a value indicating whether this object represents a not-a-number value.- Returns:
trueif this object represents a not-a-number value; otherwise,false.
-
ToEDecimal
public com.upokecenter.numbers.EDecimal ToEDecimal()Converts this object to a decimal number.- Returns:
- A decimal number for this object's value.
-
ToEFloat
public com.upokecenter.numbers.EFloat ToEFloat()Converts this object to an arbitrary-precision binary floating point number. See the ToObject overload taking a type for more information.- Returns:
- An arbitrary-precision binary floating-point number for this object's value.
-
ToERational
public com.upokecenter.numbers.ERational ToERational()Converts this object to a rational number. See the ToObject overload taking a type for more information.- Returns:
- A rational number for this object's value.
-
Abs
Returns the absolute value of this CBOR number.- Returns:
- This object's absolute value without its negative sign.
-
Negate
Returns a CBOR number with the same value as this one but with the sign reversed.- Returns:
- A CBOR number with the same value as this one but with the sign reversed.
-
Add
Returns the sum of this number and another number.- Parameters:
b- The number to add with this one.- Returns:
- The sum of this number and another number.
- Throws:
NullPointerException- The parameterbis null.OutOfMemoryError- The exact result of the operation might be too big to fit in memory (or might require more than 2 gigabytes of memory to store).
-
Subtract
Returns a number that expresses this number minus another.- Parameters:
b- The second operand to the subtraction.- Returns:
- A CBOR number that expresses this number minus the given number.
- Throws:
NullPointerException- The parameterbis null.OutOfMemoryError- The exact result of the operation might be too big to fit in memory (or might require more than 2 gigabytes of memory to store).
-
Multiply
Returns a CBOR number expressing the product of this number and the given number.- Parameters:
b- The second operand to the multiplication operation.- Returns:
- A number expressing the product of this number and the given number.
- Throws:
NullPointerException- The parameterbis null.OutOfMemoryError- The exact result of the operation might be too big to fit in memory (or might require more than 2 gigabytes of memory to store).
-
Divide
Returns the quotient of this number and another number.- Parameters:
b- The right-hand side (divisor) to the division operation.- Returns:
- The quotient of this number and another one.
- Throws:
NullPointerException- The parameterbis null.OutOfMemoryError- The exact result of the operation might be too big to fit in memory (or might require more than 2 gigabytes of memory to store).
-
Remainder
Returns the remainder when this number is divided by another number.- Parameters:
b- The right-hand side (dividend) of the remainder operation.- Returns:
- The remainder when this number is divided by the other number.
- Throws:
NullPointerException- The parameterbis null.OutOfMemoryError- The exact result of the operation might be too big to fit in memory (or might require more than 2 gigabytes of memory to store).
-
compareTo
public int compareTo(int other) Compares this CBOR number with a 32-bit signed integer. In this implementation, the two numbers' mathematical values are compared. Here, NaN (not-a-number) is considered greater than any number.- Parameters:
other- A value to compare with. Can be null.- Returns:
- A negative number, if this value is less than the other object; or 0, if both values are equal; or a positive number, if this value is less than the other object or if the other object is null. This implementation returns a positive number if.
-
compareTo
public int compareTo(long other) Compares this CBOR number with a 64-bit signed integer. In this implementation, the two numbers' mathematical values are compared. Here, NaN (not-a-number) is considered greater than any number.- Parameters:
other- A value to compare with. Can be null.- Returns:
- A negative number, if this value is less than the other object; or 0, if both values are equal; or a positive number, if this value is less than the other object or if the other object is null. This implementation returns a positive number if.
-
compareTo
Compares this CBOR number with another. In this implementation, the two numbers' mathematical values are compared. Here, NaN (not-a-number) is considered greater than any number.- Specified by:
compareToin interfaceComparable<CBORNumber>- Parameters:
other- A value to compare with. Can be null.- Returns:
- A negative number, if this value is less than the other object; or 0, if both values are equal; or a positive number, if this value is less than the other object or if the other object is null. This implementation returns a positive number if.
-