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 aroundBigDecimal
to 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 MathNumber
MINUS_INFINITY
The constant for minus infinity.static MathNumber
MINUS_ONE
The constant-1
.static MathNumber
NaN
A constant for representing numbers obtained from an operation that does not produce a result (e.g.static MathNumber
ONE
The constant1
.static MathNumber
PLUS_INFINITY
The constant for plus infinity.static MathNumber
ZERO
The 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 MathNumber
add(MathNumber other)
Yields the result ofthis + other
.int
compareTo(MathNumber other)
MathNumber
divide(MathNumber other)
Yields the result ofthis / other
.boolean
equals(java.lang.Object obj)
int
hashCode()
boolean
is(int n)
Yieldstrue
if this number is number represents exactly the given integer.boolean
isFinite()
Yieldstrue
if this number is finite (i.e.boolean
isInfinite()
Yieldstrue
if this number is infinite (i.e.boolean
isMinusInfinity()
Yieldstrue
if this number is minus infinity.boolean
isNaN()
Yieldstrue
if this number is not a number, that is, obtained from an operation that does not produce a result (e.g.boolean
isNegative()
Yieldstrue
if this number is negative.boolean
isPlusInfinity()
Yieldstrue
if this number is plus infinity.boolean
isPositiveOrZero()
Yieldstrue
if this number is positive or equal to zero.MathNumber
max(MathNumber other)
Yields the maximum number betweenthis
andother
.MathNumber
min(MathNumber other)
Yields the minimum number betweenthis
andother
.MathNumber
multiply(MathNumber other)
Yields the result ofthis * other
.MathNumber
roundDown()
Rounds down this number to the next integer value towards minus infinity (seeRoundingMode.FLOOR
).MathNumber
roundUp()
Rounds down this number to the next integer value towards plus infinity (seeRoundingMode.CEILING
).MathNumber
subtract(MathNumber other)
Yields the result ofthis - other
.java.lang.String
toString()
-
-
-
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()
Yieldstrue
if this number is minus infinity.- Returns:
true
if that condition holds
-
isPlusInfinity
public boolean isPlusInfinity()
Yieldstrue
if this number is plus infinity.- Returns:
true
if that condition holds
-
isInfinite
public boolean isInfinite()
Yieldstrue
if this number is infinite (i.e. plus or minus infinity).- Returns:
true
if that condition holds
-
isFinite
public boolean isFinite()
Yieldstrue
if this number is finite (i.e. not infinity).- Returns:
true
if that condition holds
-
is
public boolean is(int n)
Yieldstrue
if this number is number represents exactly the given integer.- Parameters:
n
- the integer to test- Returns:
true
if that condition holds
-
isPositiveOrZero
public boolean isPositiveOrZero()
Yieldstrue
if this number is positive or equal to zero.- Returns:
true
if that condition holds
-
isNegative
public boolean isNegative()
Yieldstrue
if this number is negative.- Returns:
true
if that condition holds
-
isNaN
public boolean isNaN()
Yieldstrue
if 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:
true
if that condition holds
-
add
public MathNumber add(MathNumber other)
Yields the result ofthis + other
. If one of them is not a number (according toisNaN()
), thenNaN
is 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()
), thenNaN
is 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()
), thenNaN
is 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()
), thenNaN
is returned. Ifother
is zero (according tois(int)
), then anArithmeticException
is thrown. If both are infinite (according toisInfinite()
), thenNaN
is returned.- Parameters:
other
- the divisor- Returns:
this / other
- Throws:
java.lang.ArithmeticException
- ifother
is 0
-
compareTo
public int compareTo(MathNumber other)
- Specified by:
compareTo
in interfacejava.lang.Comparable<MathNumber>
-
min
public MathNumber min(MathNumber other)
Yields the minimum number betweenthis
andother
. If one of them is not a number (according toisNaN()
), thenNaN
is returned.- Parameters:
other
- the other number- Returns:
- the minimum between
this
andother
-
max
public MathNumber max(MathNumber other)
Yields the maximum number betweenthis
andother
. If one of them is not a number (according toisNaN()
), thenNaN
is returned.- Parameters:
other
- the other number- Returns:
- the maximum between
this
andother
-
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),this
is 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),this
is returned without rounding.- Returns:
- this number rounded down towards minus infinity
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-