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)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.booleanisPositiveOrZero()Yieldstrueif this number is positive or equal to zero.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.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
-
isPositiveOrZero
public boolean isPositiveOrZero()
Yieldstrueif this number is positive or equal to zero.- Returns:
trueif that condition holds
-
isNegative
public boolean isNegative()
Yieldstrueif this number is negative.- 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. If one of them is not a number (according toisNaN()), thenNaNis returned. Ifotheris zero (according tois(int)), then anArithmeticExceptionis thrown. If both are infinite (according toisInfinite()), thenNaNis returned.- Parameters:
other- the divisor- Returns:
this / other- Throws:
java.lang.ArithmeticException- ifotheris 0
-
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
-
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
-
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
-
-