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 intThe denominator part of the rational number.private static final Patternstatic final RationalThe largest positive value of type Rational, Integer.MAX_VALUE.static final RationalThe smallest positive nonzero value of type Rational, 1 / Integer.MAX_VALUE.private final intThe numerator part of the rational number.static final RationalThe number 1 as Rational.static final RationalThe number 0 as Rational. -
Konstruktorübersicht
Konstruktoren -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungReturns a Rational whose value is (this + r).intReturns a Rational whose value is (this / r).doublebooleanfloatvoidprivate static longgcd(long a, long b) Computes the greates common divisor using Euclid's algorithm.intReturns the denominator part of the reduced rational number.intReturns the numerator part of the reduced rational number.inthashCode()intintValue()invert()Returns a Rational with numerator and denominator swapped.longReturns a Rational whose value is (this * r).negate()Returns a Rational with inverted sign.intsignum()Returns the signum function of this Rational.Returns a Rational whose value is (this - r).toString()Returns a string representation of this Rational.static RationalvalueOf(int n) Converts an integer to a rational number.static RationalvalueOf(int n, int d) Rational number with numerator n and denominator d.static RationalConverts 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:
doubleValuein KlasseNumber
-
floatValue
- Angegeben von:
floatValuein KlasseNumber
-
compareTo
- Angegeben von:
compareToin SchnittstelleComparable<Rational>
-
formatTo
- Angegeben von:
formatToin SchnittstelleFormattable
-