Class Rational
java.lang.Object
java.lang.Number
Rational
- All Implemented Interfaces:
Serializable
,Comparable<Rational>
,Formattable
A rational number, stored as numerator and denominator integer pair.
Rational is a value-based class.
- Version:
- 22.1.2024
- Author:
- H.Drachenfels
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int
The denominator part of the rational number.private static final Pattern
static final Rational
The largest positive value of type Rational, Integer.MAX_VALUE.static final Rational
The smallest positive nonzero value of type Rational, 1 / Integer.MAX_VALUE.private final int
The numerator part of the rational number.static final Rational
The number 1 as Rational.static final Rational
The number 0 as Rational. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns a Rational whose value is (this + r).int
Returns a Rational whose value is (this / r).double
boolean
float
void
private static long
gcd
(long a, long b) Computes the greates common divisor using Euclids algorithm.int
Returns the denominator part of the reduced rational number.int
Returns the numerator part of the reduced rational number.int
hashCode()
int
intValue()
invert()
Returns a Rational with numerator and denominator swapped.long
Returns a Rational whose value is (this * r).negate()
Returns a Rational with inverted sign.int
signum()
Returns the signum function of this Rational.Returns a Rational whose value is (this - r).toString()
Returns a string representation of this Rational.static Rational
valueOf
(int n) Converts an integer to a rational number.static Rational
valueOf
(int n, int d) Rational number with numerator n and denominator d.static Rational
Converts a string to a rational number.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
The number 0 as Rational. -
ONE
The number 1 as Rational. -
MAX_VALUE
The largest positive value of type Rational, Integer.MAX_VALUE. -
MIN_VALUE
The smallest positive nonzero value of type Rational, 1 / Integer.MAX_VALUE. -
numerator
private final int numeratorThe numerator part of the rational number. -
denominator
private final int denominatorThe denominator part of the rational number. Always greater or equal 1. The denominator is 1 if numerator is 0. -
FORMAT
-
-
Constructor Details
-
Rational
private Rational(long n, long d)
-
-
Method Details
-
valueOf
Rational number with numerator n and denominator d. For denominator 0 an IllegalArgumentException is thrown.- Parameters:
n
- the numeratord
- the denominator (must not be 0)- Returns:
- a Rational with the specified value
-
valueOf
Converts an integer to a rational number. This factory method is provided in preference to an (int) constructor because it allows for reuse of frequently used Rationals.- Parameters:
n
- the integer to convert- Returns:
- a Rational with the specified value
-
valueOf
Converts a string to a rational number. The string must have the format specified by toString. Otherwise NumberFormatException is thrown.- Parameters:
s
- the string to convert- Returns:
- a Rational with the specified value
-
gcd
private static long gcd(long a, long b) Computes the greates common divisor using Euclids algorithm. For negative numbers the result is undefined.- Parameters:
a
- a positive numberb
- a positive number- Returns:
- the gcd of a and b
-
getNumerator
public int getNumerator()Returns the numerator part of the reduced rational number.- Returns:
- the numerator
-
getDenominator
public int getDenominator()Returns the denominator part of the reduced rational number.- Returns:
- the denominator
-
signum
public int signum()Returns the signum function of this Rational.- Returns:
- -1, 0 or 1 as the value of this Rational is negative,zero or positive.
-
negate
Returns a Rational with inverted sign.- Returns:
- -this
-
invert
Returns a Rational with numerator and denominator swapped.- Returns:
- 1/this
-
add
Returns a Rational whose value is (this + r).- Parameters:
r
- value to be added to this Rational- Returns:
- this + r
-
subtract
Returns a Rational whose value is (this - r).- Parameters:
r
- value to be subtracted from this Rational- Returns:
- this - r
-
multiply
Returns a Rational whose value is (this * r).- Parameters:
r
- value to be multiplied by this Rational- Returns:
- this * r
-
divide
Returns a Rational whose value is (this / r).- Parameters:
r
- value by which this Rational is to be divided- Returns:
- this / r
-
toString
Returns a string representation of this Rational. The string decimal representation including sign of the numerator is followed by '/' and the string representation of the denominator. -
equals
-
hashCode
public int hashCode() -
intValue
public int intValue() -
longValue
public long longValue() -
doubleValue
public double doubleValue()- Specified by:
doubleValue
in classNumber
-
floatValue
public float floatValue()- Specified by:
floatValue
in classNumber
-
compareTo
- Specified by:
compareTo
in interfaceComparable<Rational>
-
formatTo
- Specified by:
formatTo
in interfaceFormattable
-