Top Description Fields Constructors Methods
tech.units.indriya.function

public final Class RationalNumber

extends Number
Class Inheritance
Imports
java.math.BigDecimal, .BigInteger, java.util.Objects

Represents a rational number dividend/divisor with dividend and divisor being integer numbers.

Implementation Specification

This implementation uses BigInteger to represent 'dividend' and 'divisor'.

Authors
Andi Huber, Werner Keil
Since
2.0
Version
1.2, June 21, 2019

Field Summary

Modifier and TypeField and Description
private final Object
private final Object
private final BigInteger
private final BigInteger
public static char
DIVISION_CHARACTER

The default DIVISION_CHARACTER is ÷ which (on Windows) can by typed using Alt+ 246.

private transient BigDecimal
private final int
private final boolean
private transient Long
public static final RationalNumber
private static final long
private final int
public static final RationalNumber

Constructor Summary

AccessConstructor and Description
private
RationalNumber(int signum, BigInteger absDividend, BigInteger absDivisor)

Method Summary

Modifier and TypeMethod and Description
public RationalNumber

Returns:

abs(this)
abs
()

Returns a RationalNumber whose value is the absolute value of this RationalNumber.

public RationalNumber

Returns:

this + that
add
(RationalNumber that)

Returns a new instance of RationalNumber representing the addition this + that.

public BigDecimal

Returns:

this RationalNumber converted to BigDecimal representation
bigDecimalValue
()

The BigDecimal representation of this RationalNumber.

public int

Returns:

the value 0 if this equals (numerically) that; a value less than 0 if this < that; and a value greater than 0 if this > that
compareTo
(RationalNumber that)

Compares two RationalNumber values numerically.

public RationalNumber

Returns:

this / that
divide
(RationalNumber that)

Returns a new instance of RationalNumber representing the division this / that.

public double
doubleValue()

Implements abstract java.lang.Number.doubleValue.

Returns the value of the specified number as a double.

public boolean

Returns:

true if and only if the specified Object is a RationalNumber whose value is numerically equal to this RationalNumber.
equals
(Object
Object to which this RationalNumber is to be compared.
x
)

Overrides java.lang.Object.equals.

Compares this RationalNumber with the specified Object for equality.

public float
floatValue()

Implements abstract java.lang.Number.floatValue.

Returns the value of the specified number as a float.

public BigInteger

Returns:

sign(a/b) * abs(a), (given rational number a/b)
getDividend
()

For a non-negative rational number, returns a non-negative dividend.

public BigInteger

Returns:

abs(b), (given rational number a/b)
getDivisor
()

By convention, returns a non-negative divisor.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public int
intValue()

Implements abstract java.lang.Number.intValue.

Returns the value of the specified number as an int.

public boolean

Returns:

whether this RationalNumber represents an integer number
isInteger
()

private String

Returns:

string with canonical string representation of this RationalNumber
layoutChars
(boolean
true for fractional representation 5÷3; false for decimal 1.66667.
useFractionalRepresentation
,
char divisionCharacter)

Lay out this RationalNumber into a String.

public long
longValue()

Implements abstract java.lang.Number.longValue.

Returns the value of the specified number as a long.

public RationalNumber

Returns:

this * that
multiply
(RationalNumber that)

Returns a new instance of RationalNumber representing the multiplication this * that.

public RationalNumber

Returns:

-this
negate
()

Returns a new instance of RationalNumber representing the negation of this.

public static RationalNumber

Returns:

dividend/divisor
of
(long dividend, long divisor)

Returns a RationalNumber that represents the division dividend/divisor.

public static RationalNumber
of(double number)

Returns a RationalNumber that represents the given double precision number, with an accuracy equivalent to BigDecimal#valueOf(double).

public static RationalNumber
of(BigDecimal decimalValue)

Returns a RationalNumber that represents the given BigDecimal decimalValue.

public static RationalNumber

Returns:

dividend/divisor
of
(BigInteger
the dividend
dividend
,
BigInteger
the divisor
divisor
)

Returns a RationalNumber that represents the division dividend/divisor.

public static RationalNumber

Returns:

number/1
ofInteger
(long number)

Returns a RationalNumber with divisor ONE.

public static RationalNumber

Returns:

number/1
ofInteger
(BigInteger number)

Returns a RationalNumber with divisor ONE.

public RationalNumber

Returns:

this^exponent
pow
(int exponent)

Returns a new instance of RationalNumber representing the reciprocal of this.

public RationalNumber

Returns:

1/this
reciprocal
()

Returns a new instance of RationalNumber representing the reciprocal of this.

public int

Returns:

the sign of this RationalNumber: -1, 0 or +1
signum
()

public RationalNumber

Returns:

this - that
subtract
(RationalNumber that)

Returns a new instance of RationalNumber representing the subtraction this - that.

public String

Returns:

string representation of this RationalNumber, using fractional notation.
toRationalString
()

Returns a string representation of this RationalNumber, using fractional notation, eg.

public String

Returns:

string representation of this RationalNumber, using fractional notation.
toRationalString
(char
the character to use instead of the default ÷
divisionCharacter
)

Returns a string representation of this RationalNumber, using fractional notation, eg.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Number:
byteValueshortValue

Field Detail

$lock1back to summary
private final Object $lock1
$lock2back to summary
private final Object $lock2
absDividendback to summary
private final BigInteger absDividend
absDivisorback to summary
private final BigInteger absDivisor
DIVISION_CHARACTERback to summary
public static char DIVISION_CHARACTER

The default DIVISION_CHARACTER is ÷ which (on Windows) can by typed using Alt+ 246.

Note

Number parsing will fail if this is a white-space character.

divisionResultback to summary
private transient BigDecimal divisionResult
hashCodeback to summary
private final int hashCode
isIntegerback to summary
private final boolean isInteger
longValueback to summary
private transient Long longValue
ONEback to summary
public static final RationalNumber ONE
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.Number.serialVersionUID.

signumback to summary
private final int signum
ZEROback to summary
public static final RationalNumber ZERO

Constructor Detail

RationalNumberback to summary
private RationalNumber(int signum, BigInteger absDividend, BigInteger absDivisor)

Method Detail

absback to summary
public RationalNumber abs()

Returns a RationalNumber whose value is the absolute value of this RationalNumber.

Returns:RationalNumber

abs(this)

addback to summary
public RationalNumber add(RationalNumber that)

Returns a new instance of RationalNumber representing the addition this + that.

Returns:RationalNumber

this + that

bigDecimalValueback to summary
public BigDecimal bigDecimalValue()

The BigDecimal representation of this RationalNumber.

Implementation Note:
the conversion calculation is done lazily and thread-safe
Returns:BigDecimal

this RationalNumber converted to BigDecimal representation

compareToback to summary
public int compareTo(RationalNumber that)

Compares two RationalNumber values numerically.

Returns:int

the value 0 if this equals (numerically) that; a value less than 0 if this < that; and a value greater than 0 if this > that

divideback to summary
public RationalNumber divide(RationalNumber that)

Returns a new instance of RationalNumber representing the division this / that.

Returns:RationalNumber

this / that

doubleValueback to summary
public double doubleValue()

Implements abstract java.lang.Number.doubleValue.

Doc from java.lang.Number.doubleValue.

Returns the value of the specified number as a double.

Returns:double

the numeric value represented by this object after conversion to type double.

Annotations
@Override
equalsback to summary
public boolean equals(Object x)

Overrides java.lang.Object.equals.

Compares this RationalNumber with the specified Object for equality.

Parameters
x:Object

Object to which this RationalNumber is to be compared.

Returns:boolean

true if and only if the specified Object is a RationalNumber whose value is numerically equal to this RationalNumber.

Annotations
@Override
floatValueback to summary
public float floatValue()

Implements abstract java.lang.Number.floatValue.

Doc from java.lang.Number.floatValue.

Returns the value of the specified number as a float.

Returns:float

the numeric value represented by this object after conversion to type float.

Annotations
@Override
getDividendback to summary
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 this RationalNumber, whereas @link getDivisor() does not and is always non-negative.

Returns:BigInteger

sign(a/b) * abs(a), (given rational number a/b)

getDivisorback to summary
public BigInteger getDivisor()

By convention, returns a non-negative divisor.

Returns:BigInteger

abs(b), (given rational number a/b)

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
intValueback to summary
public int intValue()

Implements abstract java.lang.Number.intValue.

Doc from java.lang.Number.intValue.

Returns the value of the specified number as an int.

Returns:int

the numeric value represented by this object after conversion to type int.

Annotations
@Override
isIntegerback to summary
public boolean isInteger()
Returns:boolean

whether this RationalNumber represents an integer number

layoutCharsback to summary
private String layoutChars(boolean useFractionalRepresentation, char divisionCharacter)

Lay out this RationalNumber into a String.

Parameters
useFractionalRepresentation:boolean

true for fractional representation 5÷3; false for decimal 1.66667.

Returns:String

string with canonical string representation of this RationalNumber

longValueback to summary
public long longValue()

Implements abstract java.lang.Number.longValue.

Doc from java.lang.Number.longValue.

Returns the value of the specified number as a long.

Returns:long

the numeric value represented by this object after conversion to type long.

Annotations
@Override
multiplyback to summary
public RationalNumber multiply(RationalNumber that)

Returns a new instance of RationalNumber representing the multiplication this * that.

Returns:RationalNumber

this * that

negateback to summary
public RationalNumber negate()

Returns a new instance of RationalNumber representing the negation of this.

Returns:RationalNumber

-this

ofback to summary
public static RationalNumber of(long dividend, long divisor)

Returns a RationalNumber that represents the division dividend/divisor.

Returns:RationalNumber

dividend/divisor

Exceptions
IllegalArgumentException:
if divisor = 0
ofback to summary
public static RationalNumber of(double number)

Returns a RationalNumber that represents the given double precision number, with an accuracy equivalent to BigDecimal#valueOf(double).

ofback to summary
public static RationalNumber of(BigDecimal decimalValue)

Returns a RationalNumber that represents the given BigDecimal decimalValue.

ofback to summary
public static RationalNumber of(BigInteger dividend, BigInteger divisor)

Returns a RationalNumber that represents the division dividend/divisor.

Implementation Note:
this implementation stores dividend and divisor after canceling down from given parameters
Parameters
dividend:BigInteger

the dividend

divisor:BigInteger

the divisor

Returns:RationalNumber

dividend/divisor

Exceptions
IllegalArgumentException:
if divisor = 0
NullPointerException:
- if dividend is null or divisor is null
ofIntegerback to summary
public static RationalNumber ofInteger(long number)

Returns a RationalNumber with divisor ONE. In other words, returns a RationalNumber that represents given integer number.

Returns:RationalNumber

number/1

Exceptions
NullPointerException:
- if number is null
ofIntegerback to summary
public static RationalNumber ofInteger(BigInteger number)

Returns a RationalNumber with divisor ONE. In other words, returns a RationalNumber that represents given integer number.

Returns:RationalNumber

number/1

Exceptions
NullPointerException:
- if number is null
powback to summary
public RationalNumber pow(int exponent)

Returns a new instance of RationalNumber representing the reciprocal of this.

Returns:RationalNumber

this^exponent

reciprocalback to summary
public RationalNumber reciprocal()

Returns a new instance of RationalNumber representing the reciprocal of this.

Returns:RationalNumber

1/this

signumback to summary
public int signum()
Returns:int

the sign of this RationalNumber: -1, 0 or +1

subtractback to summary
public RationalNumber subtract(RationalNumber that)

Returns a new instance of RationalNumber representing the subtraction this - that.

Returns:RationalNumber

this - that

toRationalStringback to summary
public String toRationalString()

Returns a string representation of this RationalNumber, using fractional notation, eg. 5÷3 or -5÷3.

Returns:String

string representation of this RationalNumber, using fractional notation.

Since
2.0
toRationalStringback to summary
public String toRationalString(char divisionCharacter)

Returns a string representation of this RationalNumber, using fractional notation, eg. 5÷3 or -5÷3.

Parameters
divisionCharacter:char

the character to use instead of the default ÷

Returns:String

string representation of this RationalNumber, using fractional notation.

Since
2.0
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override