Package it.unive.lisa.util.numeric
Class MathNumber
- java.lang.Object
-
- it.unive.lisa.util.numeric.MathNumber
-
- All Implemented Interfaces:
java.lang.Comparable<MathNumber>
public class MathNumber extends java.lang.Object implements java.lang.Comparable<MathNumber>
A wrapper aroundBigDecimalto represent the mathematical concept of a number, that can be also plus or minus infinity, in a convenient way.
-
-
Field Summary
Fields Modifier and Type Field Description static MathNumberMINUS_INFINITYThe constant for minus infinity.static MathNumberMINUS_ONEThe constant-1.static MathNumberNaNA constant for representing numbers obtained from an operation that does not produce a result (e.g.static MathNumberONEThe constant1.static MathNumberPLUS_INFINITYThe constant for plus infinity.static MathNumberZEROThe constant0.
-
Constructor Summary
Constructors Constructor Description MathNumber(double number)Builds a math number representing the given value.MathNumber(long number)Builds a math number representing the given value.MathNumber(java.math.BigDecimal number)Builds a math number representing the given value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MathNumberabs()Yields the absolute value of this number.MathNumberadd(MathNumber other)Yields the result ofthis + other.intcompareTo(MathNumber other)MathNumberdivide(MathNumber other)Yields the result ofthis / other.booleanequals(java.lang.Object obj)booleangeq(MathNumber other)Yieldstrueif this number is greater than or equal to other.java.math.BigDecimalgetNumber()Yields the BigDecimal of this abstract value.booleangt(MathNumber other)Yieldstrueif this number is greater than other.inthashCode()booleanis(int n)Yieldstrueif this number is number represents exactly the given integer.booleanisFinite()Yieldstrueif this number is finite (i.e.booleanisInfinite()Yieldstrueif this number is infinite (i.e.booleanisMinusInfinity()Yieldstrueif this number is minus infinity.booleanisNaN()Yieldstrueif this number is not a number, that is, obtained from an operation that does not produce a result (e.g.booleanisNegative()Yieldstrueif this number is negative.booleanisPlusInfinity()Yieldstrueif this number is plus infinity.booleanisPositive()Yieldstrueif this number is positive.booleanisZero()Yieldstrueif this number is equal to zero.booleanleq(MathNumber other)Yieldstrueif this number is less than or equals to other.booleanlt(MathNumber other)Yieldstrueif this number is less than other.MathNumbermax(MathNumber other)Yields the maximum number betweenthisandother.MathNumbermin(MathNumber other)Yields the minimum number betweenthisandother.MathNumbermultiply(MathNumber other)Yields the result ofthis * other.MathNumberroundDown()Rounds down this number to the next integer value towards minus infinity (seeRoundingMode.FLOOR).MathNumberroundUp()Rounds down this number to the next integer value towards plus infinity (seeRoundingMode.CEILING).MathNumbersubtract(MathNumber other)Yields the result ofthis - other.bytetoByte()Yields the byte value of this math number.doubletoDouble()Yields the double value of this math number.floattoFloat()Yields the float value of this math number.inttoInt()Yields the integer value of this math number.longtoLong()Yields the long value of this math number.shorttoShort()Yields the short value of this math number.java.lang.StringtoString()
-
-
-
Field Detail
-
PLUS_INFINITY
public static final MathNumber PLUS_INFINITY
The constant for plus infinity.
-
MINUS_INFINITY
public static final MathNumber MINUS_INFINITY
The constant for minus infinity.
-
ZERO
public static final MathNumber ZERO
The constant0.
-
ONE
public static final MathNumber ONE
The constant1.
-
MINUS_ONE
public static final MathNumber MINUS_ONE
The constant-1.
-
NaN
public static final MathNumber NaN
A constant for representing numbers obtained from an operation that does not produce a result (e.g. infinity divided by infinity).
-
-
Constructor Detail
-
MathNumber
public MathNumber(long number)
Builds a math number representing the given value.- Parameters:
number- the value
-
MathNumber
public MathNumber(double number)
Builds a math number representing the given value.- Parameters:
number- the value
-
MathNumber
public MathNumber(java.math.BigDecimal number)
Builds a math number representing the given value.- Parameters:
number- the value
-
-
Method Detail
-
isMinusInfinity
public boolean isMinusInfinity()
Yieldstrueif this number is minus infinity.- Returns:
trueif that condition holds
-
isPlusInfinity
public boolean isPlusInfinity()
Yieldstrueif this number is plus infinity.- Returns:
trueif that condition holds
-
isInfinite
public boolean isInfinite()
Yieldstrueif this number is infinite (i.e. plus or minus infinity).- Returns:
trueif that condition holds
-
isFinite
public boolean isFinite()
Yieldstrueif this number is finite (i.e. not infinity).- Returns:
trueif that condition holds
-
is
public boolean is(int n)
Yieldstrueif this number is number represents exactly the given integer.- Parameters:
n- the integer to test- Returns:
trueif that condition holds
-
isZero
public boolean isZero()
Yieldstrueif this number is equal to zero.- Returns:
trueif that condition holds
-
isNegative
public boolean isNegative()
Yieldstrueif this number is negative.- Returns:
trueif that condition holds
-
isPositive
public boolean isPositive()
Yieldstrueif this number is positive.- Returns:
trueif that condition holds
-
isNaN
public boolean isNaN()
Yieldstrueif this number is not a number, that is, obtained from an operation that does not produce a result (e.g. infinity divided by infinity).- Returns:
trueif that condition holds
-
add
public MathNumber add(MathNumber other)
Yields the result ofthis + other. If one of them is not a number (according toisNaN()), thenNaNis returned.- Parameters:
other- the other operand- Returns:
this + other
-
subtract
public MathNumber subtract(MathNumber other)
Yields the result ofthis - other. If one of them is not a number (according toisNaN()), thenNaNis returned.- Parameters:
other- the other operand- Returns:
this - other
-
multiply
public MathNumber multiply(MathNumber other)
Yields the result ofthis * other. If one of them is not a number (according toisNaN()), thenNaNis returned.- Parameters:
other- the other factor- Returns:
this * other
-
divide
public MathNumber divide(MathNumber other)
Yields the result ofthis / other. This method returnsNaNif one of the argument is not a number (according toisNaN()), ifotheris zero (according toisZero()), or if both arguments are infinite (according toisInfinite()).- Parameters:
other- the divisor- Returns:
this / other
-
compareTo
public int compareTo(MathNumber other)
- Specified by:
compareToin interfacejava.lang.Comparable<MathNumber>
-
min
public MathNumber min(MathNumber other)
Yields the minimum number betweenthisandother. If one of them is not a number (according toisNaN()), thenNaNis returned.- Parameters:
other- the other number- Returns:
- the minimum between
thisandother
-
max
public MathNumber max(MathNumber other)
Yields the maximum number betweenthisandother. If one of them is not a number (according toisNaN()), thenNaNis returned.- Parameters:
other- the other number- Returns:
- the maximum between
thisandother
-
leq
public boolean leq(MathNumber other)
Yieldstrueif this number is less than or equals to other.- Parameters:
other- the other number- Returns:
- true if @code{this} is less or equals than @code{other}.
-
gt
public boolean gt(MathNumber other)
Yieldstrueif this number is greater than other.- Parameters:
other- the other number- Returns:
- true if
-
lt
public boolean lt(MathNumber other)
Yieldstrueif this number is less than other.- Parameters:
other- the other number- Returns:
- true if
-
geq
public boolean geq(MathNumber other)
Yieldstrueif this number is greater than or equal to other.- Parameters:
other- the other number- Returns:
- true if
-
abs
public MathNumber abs()
Yields the absolute value of this number. If it is not a number (according toisNaN()), thenNaNis returned.- Returns:
- the absolute value of
this
-
roundUp
public MathNumber roundUp()
Rounds down this number to the next integer value towards plus infinity (seeRoundingMode.CEILING). If this number is infinite or is not a number (according toisInfinite()andisNaN(), respectively),thisis returned without rounding.- Returns:
- this number rounded up towards plus infinity
-
roundDown
public MathNumber roundDown()
Rounds down this number to the next integer value towards minus infinity (seeRoundingMode.FLOOR). If this number is infinite or is not a number (according toisInfinite()andisNaN(), respectively),thisis returned without rounding.- Returns:
- this number rounded down towards minus infinity
-
toInt
public int toInt() throws MathNumberConversionExceptionYields the integer value of this math number.- Returns:
- the integer value of this math number
- Throws:
MathNumberConversionException- when this math number is NaN or infinite
-
toDouble
public double toDouble() throws MathNumberConversionExceptionYields the double value of this math number.- Returns:
- the double value of this math number
- Throws:
MathNumberConversionException- when this math number is NaN or infinite
-
toByte
public byte toByte() throws MathNumberConversionExceptionYields the byte value of this math number.- Returns:
- the byte value of this math number
- Throws:
MathNumberConversionException- when this math number is NaN or infinite
-
toShort
public short toShort() throws MathNumberConversionExceptionYields the short value of this math number.- Returns:
- the short value of this math number
- Throws:
MathNumberConversionException- when this math number is NaN or infinite
-
toFloat
public float toFloat() throws MathNumberConversionExceptionYields the float value of this math number.- Returns:
- the float value of this math number
- Throws:
MathNumberConversionException- when this math number is NaN or infinite
-
toLong
public long toLong() throws MathNumberConversionExceptionYields the long value of this math number.- Returns:
- the long value of this math number
- Throws:
MathNumberConversionException- when this math number is NaN or infinite
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getNumber
public java.math.BigDecimal getNumber()
Yields the BigDecimal of this abstract value.- Returns:
- the BigDecimal of this abstract value
- Throws:
java.lang.IllegalStateException- If this number is not a number or is plus infinite or is minus infinite (according toisNaN(),isPlusInfinity()andisMinusInfinity()respectively).
-
-