Klasse Rational
java.lang.Object
java.lang.Number
Rational
- Alle implementierten Schnittstellen:
Serializable
,Comparable<Rational>
,Formattable
A rational number is stored as an integer pair (numerator, denominator).
Rational is a value-based class.
- Version:
- 30.6.2025
- Autor:
- H.Drachenfels
- Siehe auch:
-
Feldübersicht
FelderModifizierer und TypFeldBeschreibungprivate 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. -
Konstruktorübersicht
Konstruktoren -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungReturns 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 Euclid's 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.Von Klasse geerbte Methoden java.lang.Number
byteValue, shortValue
-
Felddetails
-
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
The numerator part of the rational number. -
denominator
The denominator part of the rational number. Always greater or equal 1. The denominator is 1 if numerator is 0. -
FORMAT
-
-
Konstruktordetails
-
Rational
-
-
Methodendetails
-
valueOf
Rational number with numerator n and denominator d. For denominator 0 an IllegalArgumentException is thrown.- Parameter:
n
- the numeratord
- the denominator (must not be 0)- Gibt zurück:
- 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.- Parameter:
n
- the integer to convert- Gibt zurück:
- 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.- Parameter:
s
- the string to convert- Gibt zurück:
- a Rational with the specified value
-
gcd
Computes the greates common divisor using Euclid's algorithm. For negative numbers, the result is undefined.- Parameter:
a
- a positive numberb
- a positive number- Gibt zurück:
- the gcd of a and b
-
getNumerator
Returns the numerator part of the reduced rational number.- Gibt zurück:
- the numerator
-
getDenominator
Returns the denominator part of the reduced rational number.- Gibt zurück:
- the denominator
-
signum
Returns the signum function of this Rational.- Gibt zurück:
- -1, 0 or 1 as the value of this Rational is negative, zero or positive.
-
negate
Returns a Rational with inverted sign.- Gibt zurück:
- -this
-
invert
Returns a Rational with numerator and denominator swapped.- Gibt zurück:
- 1/this
-
add
Returns a Rational whose value is (this + r).- Parameter:
r
- value to be added to this Rational- Gibt zurück:
- this + r
-
subtract
Returns a Rational whose value is (this - r).- Parameter:
r
- value to be subtracted from this Rational- Gibt zurück:
- this - r
-
multiply
Returns a Rational whose value is (this * r).- Parameter:
r
- value to be multiplied by this Rational- Gibt zurück:
- this * r
-
divide
Returns a Rational whose value is (this / r).- Parameter:
r
- value by which this Rational is to be divided- Gibt zurück:
- this / r
-
toString
Returns a string representation of this Rational. The string representation of the numerator, including the sign, is followed by '/' and the string representation of the denominator. -
equals
-
hashCode
-
intValue
-
longValue
-
doubleValue
- Angegeben von:
doubleValue
in KlasseNumber
-
floatValue
- Angegeben von:
floatValue
in KlasseNumber
-
compareTo
- Angegeben von:
compareTo
in SchnittstelleComparable<Rational>
-
formatTo
- Angegeben von:
formatTo
in SchnittstelleFormattable
-