Package tech.units.indriya.function
Class RationalNumber
- java.lang.Object
-
- java.lang.Number
-
- tech.units.indriya.function.RationalNumber
-
- All Implemented Interfaces:
Serializable
public final class RationalNumber extends Number
Represents a rational numberdividend/divisorwithdividendanddivisorbeing integer numbers.- Since:
- 2.0
- Version:
- 1.2, June 21, 2019
- Author:
- Andi Huber, Werner Keil
- See Also:
- Serialized Form
- Implementation Requirements:
- This implementation uses
BigIntegerto represent 'dividend' and 'divisor'.
-
-
Field Summary
Fields Modifier and Type Field Description static charDIVISION_CHARACTERThe defaultDIVISION_CHARACTERis ÷ which (on Windows) can by typed using Alt+ 246.static RationalNumberONEstatic RationalNumberZERO
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description RationalNumberabs()Returns aRationalNumberwhose value is the absolute value of thisRationalNumber.RationalNumberadd(RationalNumber that)Returns a new instance ofRationalNumberrepresenting the additionthis + that.BigDecimalbigDecimalValue()TheBigDecimalrepresentation of thisRationalNumber.intcompareTo(RationalNumber that)Compares twoRationalNumbervalues numerically.RationalNumberdivide(RationalNumber that)Returns a new instance ofRationalNumberrepresenting the divisionthis / that.doubledoubleValue()booleanequals(Object x)Compares this RationalNumber with the specified Object for equality.floatfloatValue()BigIntegergetDividend()For a non-negative rational number, returns a non-negative dividend.BigIntegergetDivisor()By convention, returns a non-negative divisor.inthashCode()intintValue()booleanisInteger()longlongValue()RationalNumbermultiply(RationalNumber that)Returns a new instance ofRationalNumberrepresenting the multiplicationthis * that.RationalNumbernegate()Returns a new instance ofRationalNumberrepresenting the negation ofthis.static RationalNumberof(double number)Returns aRationalNumberthat represents the given double precisionnumber, with an accuracy equivalent toBigDecimal.valueOf(double).static RationalNumberof(long dividend, long divisor)Returns aRationalNumberthat represents the divisiondividend/divisor.static RationalNumberof(BigDecimal decimalValue)Returns aRationalNumberthat represents the given BigDecimal decimalValue.static RationalNumberof(BigInteger dividend, BigInteger divisor)Returns aRationalNumberthat represents the divisiondividend/divisor.static RationalNumberofInteger(long number)Returns aRationalNumberwith divisor ONE.static RationalNumberofInteger(BigInteger number)Returns aRationalNumberwith divisor ONE.RationalNumberpow(int exponent)Returns a new instance ofRationalNumberrepresenting the reciprocal ofthis.RationalNumberreciprocal()Returns a new instance ofRationalNumberrepresenting the reciprocal ofthis.intsignum()RationalNumbersubtract(RationalNumber that)Returns a new instance ofRationalNumberrepresenting the subtractionthis - that.StringtoRationalString()Returns a string representation of thisRationalNumber, using fractional notation, eg.StringtoRationalString(char divisionCharacter)Returns a string representation of thisRationalNumber, using fractional notation, eg.StringtoString()-
Methods inherited from class java.lang.Number
byteValue, shortValue
-
-
-
-
Field Detail
-
DIVISION_CHARACTER
public static char DIVISION_CHARACTER
The defaultDIVISION_CHARACTERis ÷ which (on Windows) can by typed using Alt+ 246.Note: Number parsing will fail if this is a white-space character.
-
ZERO
public static final RationalNumber ZERO
-
ONE
public static final RationalNumber ONE
-
-
Method Detail
-
ofInteger
public static RationalNumber ofInteger(long number)
Returns aRationalNumberwith divisor ONE. In other words, returns aRationalNumberthat represents given integernumber.- Parameters:
number-- Returns:
- number/1
- Throws:
NullPointerException- - if number isnull
-
ofInteger
public static RationalNumber ofInteger(BigInteger number)
Returns aRationalNumberwith divisor ONE. In other words, returns aRationalNumberthat represents given integernumber.- Parameters:
number-- Returns:
- number/1
- Throws:
NullPointerException- - if number isnull
-
of
public static RationalNumber of(long dividend, long divisor)
Returns aRationalNumberthat represents the divisiondividend/divisor.- Parameters:
dividend-divisor-- Returns:
- dividend/divisor
- Throws:
IllegalArgumentException- ifdivisor = 0
-
of
public static RationalNumber of(double number)
Returns aRationalNumberthat represents the given double precisionnumber, with an accuracy equivalent toBigDecimal.valueOf(double).- Parameters:
number-
-
of
public static RationalNumber of(BigDecimal decimalValue)
Returns aRationalNumberthat represents the given BigDecimal decimalValue.- Parameters:
decimalValue-
-
of
public static RationalNumber of(BigInteger dividend, BigInteger divisor)
Returns aRationalNumberthat represents the divisiondividend/divisor.- Implementation Note:
- this implementation stores dividend and divisor after canceling down from given parameters
- Parameters:
dividend- the dividenddivisor- the divisor- Returns:
- dividend/divisor
- Throws:
IllegalArgumentException- ifdivisor = 0NullPointerException- - if dividend isnullor divisor isnull
-
getDividend
public BigInteger getDividend()
For a non-negative rational number, returns a non-negative dividend. Otherwise returns a negative dividend. In other words, by convention, the integer returned includes the sign of thisRationalNumber, whereas @linkgetDivisor()does not and is always non-negative.- Returns:
- sign(a/b) * abs(a), (given rational number a/b)
-
getDivisor
public BigInteger getDivisor()
By convention, returns a non-negative divisor.- Returns:
- abs(b), (given rational number a/b)
-
isInteger
public boolean isInteger()
- Returns:
- whether this
RationalNumberrepresents an integer number
-
signum
public int signum()
- Returns:
- the sign of this
RationalNumber: -1, 0 or +1
-
bigDecimalValue
public BigDecimal bigDecimalValue()
TheBigDecimalrepresentation of thisRationalNumber.- Implementation Note:
- the conversion calculation is done lazily and thread-safe
- Returns:
- this
RationalNumberconverted toBigDecimalrepresentation
-
add
public RationalNumber add(RationalNumber that)
Returns a new instance ofRationalNumberrepresenting the additionthis + that.- Parameters:
that-- Returns:
- this + that
-
subtract
public RationalNumber subtract(RationalNumber that)
Returns a new instance ofRationalNumberrepresenting the subtractionthis - that.- Parameters:
that-- Returns:
- this - that
-
multiply
public RationalNumber multiply(RationalNumber that)
Returns a new instance ofRationalNumberrepresenting the multiplicationthis * that.- Parameters:
that-- Returns:
- this * that
-
divide
public RationalNumber divide(RationalNumber that)
Returns a new instance ofRationalNumberrepresenting the divisionthis / that.- Parameters:
that-- Returns:
- this / that
-
negate
public RationalNumber negate()
Returns a new instance ofRationalNumberrepresenting the negation ofthis.- Returns:
- -this
-
reciprocal
public RationalNumber reciprocal()
Returns a new instance ofRationalNumberrepresenting the reciprocal ofthis.- Returns:
- 1/this
-
pow
public RationalNumber pow(int exponent)
Returns a new instance ofRationalNumberrepresenting the reciprocal ofthis.- Parameters:
exponent-- Returns:
- this^exponent
-
abs
public RationalNumber abs()
Returns aRationalNumberwhose value is the absolute value of thisRationalNumber.- Returns:
abs(this)
-
compareTo
public int compareTo(RationalNumber that)
Compares twoRationalNumbervalues numerically.- Parameters:
that-- Returns:
- the value
0ifthisequals (numerically)that; a value less than0ifthis < that; and a value greater than0ifthis > that
-
floatValue
public float floatValue()
- Specified by:
floatValuein classNumber
-
doubleValue
public double doubleValue()
- Specified by:
doubleValuein classNumber
-
toRationalString
public String toRationalString()
Returns a string representation of thisRationalNumber, using fractional notation, eg.5÷3or-5÷3.- Returns:
- string representation of this
RationalNumber, using fractional notation. - Since:
- 2.0
-
toRationalString
public String toRationalString(char divisionCharacter)
Returns a string representation of thisRationalNumber, using fractional notation, eg.5÷3or-5÷3.- Parameters:
divisionCharacter- the character to use instead of the default÷- Returns:
- string representation of this
RationalNumber, using fractional notation. - Since:
- 2.0
-
-