public class Rational extends java.lang.Number implements java.lang.Comparable<Rational>
Modifier and Type | Field and Description |
---|---|
static Rational | ONE Representation of the number one, in rational format. |
static Rational | ONE_HALF Representation of the number 1/2, in rational format. |
static Rational | ZERO Representation of the number zero, in rational format. |
Constructor and Description |
---|
Rational(java.math.BigDecimal bd) Create a rational from a big decimal. |
Rational(java.math.BigInteger value) Create a rational representing the big integer. |
Rational(java.math.BigInteger numerator, java.math.BigInteger denominator) Create a rational from two big integers. |
Rational(long value) Create a rational representing the long value. |
Rational(long numerator, long denominator) Create a rational from two longs. |
Rational(java.lang.String rational) Create a rational from a string representing a decimal number or "x/y" where x and y are base10 numbers. |
Modifier and Type | Method and Description |
---|---|
Rational | abs() Returns the absolute value of this rational. |
Rational | add(java.math.BigDecimal other) Adds the rational to the big decimal, returning a new rational. |
Rational | add(java.math.BigInteger other) Adds the rational to the big integer, returning a new rational. |
Rational | add(long other) Adds the rational to the long value, returning a new rational. |
Rational | add(Rational other) Adds the rational to this one, returning a new rational. |
java.math.BigDecimal | bigDecimalValue() Converts this rational to a big decimal, throwing an exception if the division results in a non-terminating decimal expansion. |
java.math.BigDecimal | bigDecimalValue(java.math.MathContext mc) Converts this rational to a big decimal, using the math context for rounding. |
byte | byteValue() Returns the rational as a byte (rounding down). |
java.math.BigInteger | ceiling() Returns the ceiling of the rational (smallest integer greater than or equal to it). |
int | compareTo(Rational other) Compares this rational with the other one. |
java.math.BigInteger | denominator() Returns the denominator associated with the rational. |
Rational | divide(java.math.BigDecimal other) Divides the rational by the big decimal, returning a new rational. |
Rational | divide(java.math.BigInteger other) Divides the rational by the big integer, returning a new rational. |
Rational | divide(long other) Divides the rational by the long, returning a new rational. |
Rational | divide(Rational other) Divides the two rationals, returning a new rational. |
double | doubleValue() Returns the rational as a double. |
boolean | equals(java.lang.Object obj) Performs an equality check with the other object. |
float | floatValue() Returns the rational as a float. |
java.math.BigInteger | floor() Returns the floor of the rational (largest integer less than or equal to it). |
Rational | floorMod(java.math.BigDecimal moduloValue) Returns this value modded by the given big decimal. |
Rational | floorMod(java.math.BigInteger moduloValue) Returns this value modded by the given big integer. |
Rational | floorMod(long moduloValue) Returns this value modded by the given long. |
Rational | floorMod(Rational moduloValue) Returns this value modded by the given rational. |
int | hashCode() Computes the hash code of the rational. |
int | intValue() Returns the rational as an integer (rounding down). |
Rational | invert() Returns the reciprocal of the rational. |
long | longValue() Returns the rational as a long (rounding down). |
static Rational | max(java.lang.Iterable<Rational> rationals) Takes the maximum of the rationals. |
static Rational | max(Rational... rationals) Takes the maximum of the rationals. |
static Rational | min(java.lang.Iterable<Rational> rationals) Takes the minimum of the rationals. |
static Rational | min(Rational... rationals) Takes the minimum of the rationals. |
Rational | mod(java.math.BigDecimal moduloValue) Returns this value modded by the given big decimal. |
Rational | mod(java.math.BigInteger moduloValue) Returns this value modded by the given big integer. |
Rational | mod(long moduloValue) Returns this value modded by the given long. |
Rational | mod(Rational m) Returns this value modded by the given rational. |
Rational | multiply(java.math.BigDecimal other) Multiplies the rational with the big decimal, returning a new rational. |
Rational | multiply(java.math.BigInteger other) Multiplies the rational with the big integer, returning a new rational. |
Rational | multiply(long other) Multiplies the rational with the long, returning a new rational. |
Rational | multiply(Rational other) Multiplies the two rationals together, returning a new rational. |
Rational | negate() Returns a rational that is the negated version of this one. |
java.math.BigInteger | numerator() Returns the numerator associated with the rational. |
Rational | pow(int n) Raise this rational to the N-th power. |
static Rational | product(java.lang.Iterable<Rational> rationals) Convenience method to multiply an arbitrary number of rationals. |
static Rational | product(Rational... rationals) Convenience method to multiply an arbitrary number of rationals. |
java.math.BigInteger | round() Round the rational to the nearest integer (using the HALF_EVEN rounding mode). |
java.math.BigInteger | round(java.math.RoundingMode roundingMode) Round the rational to the nearest integer (using the specified rounding mode). |
short | shortValue() Returns the rational as a short (rounding down). |
int | sign() Returns the sign of this rational. |
Rational | subtract(java.math.BigDecimal other) Subtracts the big decimal from the rational, returning a new rational. |
Rational | subtract(java.math.BigInteger other) Subtracts the big integer from the rational, returning a new rational. |
Rational | subtract(long other) Subtracts the long from the rational, returning a new rational. |
Rational | subtract(Rational other) Subtracts the rational from this one, returning a new rational. |
static Rational | sum(java.lang.Iterable<Rational> rationals) Convenience method to sum an iterable of rationals. |
static Rational | sum(Rational... rationals) Convenience method to sum an arbitrary number of rationals. |
java.lang.String | toString() Convert this rational into a string. |
public static Rational ZERO
public static Rational ONE
public static Rational ONE_HALF
public Rational(java.math.BigInteger numerator, java.math.BigInteger denominator)
numerator
- the numeratordenominator
- the denominatorjava.lang.ArithmeticException
- if the denominator is zeropublic Rational(java.math.BigInteger value)
value
- the valuepublic Rational(java.math.BigDecimal bd)
bd
- the big decimalpublic Rational(java.lang.String rational)
rational
- the number in string format; it can be in any format that is accepted by the BigDecimal constructor, or in the "x/y" formatjava.lang.NumberFormatException
- if the string is not a valid representation of a decimal number or a rational numberjava.lang.ArithmeticException
- if the denominator is zeropublic Rational(long numerator, long denominator)
numerator
- the numeratordenominator
- the denominatorjava.lang.ArithmeticException
- if the denominator is zeropublic Rational(long value)
value
- the valuepublic java.math.BigInteger numerator()
public java.math.BigInteger denominator()
public Rational add(Rational other)
other
- the other rational numberpublic Rational add(java.math.BigInteger other)
other
- the other numberpublic Rational add(java.math.BigDecimal other)
other
- the other numberpublic Rational add(long other)
other
- the other numberpublic Rational subtract(Rational other)
other
- the other rational numberpublic Rational subtract(java.math.BigInteger other)
other
- the other numberpublic Rational subtract(java.math.BigDecimal other)
other
- the other numberpublic Rational subtract(long other)
other
- the other numberpublic Rational multiply(Rational other)
other
- the other rational numberpublic Rational multiply(java.math.BigInteger other)
other
- the other numberpublic Rational multiply(java.math.BigDecimal other)
other
- the other numberpublic Rational multiply(long other)
other
- the other numberpublic Rational divide(Rational other)
other
- the other rational numberjava.lang.ArithmeticException
- if other is zeropublic Rational divide(java.math.BigInteger other)
other
- the other numberjava.lang.ArithmeticException
- if other is zeropublic Rational divide(java.math.BigDecimal other)
other
- the other numberjava.lang.ArithmeticException
- if other is zeropublic Rational divide(long other)
other
- the other numberjava.lang.ArithmeticException
- if other is zeropublic Rational abs()
public Rational negate()
public Rational invert()
java.lang.ArithmeticException
- if this rational is zeropublic java.math.BigInteger floor()
public java.math.BigInteger ceiling()
public java.math.BigInteger round()
RoundingMode.HALF_EVEN
public java.math.BigInteger round(java.math.RoundingMode roundingMode)
roundingMode
- the rounding mode to usejava.lang.ArithmeticException
- if the rounding mode is UNNECESSARY but conversion to a big integer requires roundingjava.lang.IllegalArgumentException
- if roundingMode is not one of CEILING, DOWN, FLOOR, HALF_DOWN, HALF_EVEN, HALF_UP, UNNECESSARY, or UPpublic Rational floorMod(Rational moduloValue)
moduloValue
- the modulo valuejava.lang.ArithmeticException
- if the moduloValue is zeropublic Rational floorMod(java.math.BigInteger moduloValue)
moduloValue
- the modulo valuejava.lang.ArithmeticException
- if the moduloValue is zeropublic Rational floorMod(java.math.BigDecimal moduloValue)
moduloValue
- the modulo valuejava.lang.ArithmeticException
- if the moduloValue is zeropublic Rational floorMod(long moduloValue)
moduloValue
- the modulo valuejava.lang.ArithmeticException
- if the moduloValue is zeropublic Rational mod(Rational m)
m
- the modulo valuejava.lang.ArithmeticException
- if the moduloValue is zeropublic Rational mod(java.math.BigInteger moduloValue)
moduloValue
- the modulo valuejava.lang.ArithmeticException
- if the moduloValue is zeropublic Rational mod(java.math.BigDecimal moduloValue)
moduloValue
- the modulo valuejava.lang.ArithmeticException
- if the moduloValue is zeropublic Rational mod(long moduloValue)
moduloValue
- the modulo valuejava.lang.ArithmeticException
- if the moduloValue is zeropublic Rational pow(int n)
n
- the power to raise the rational topublic int sign()
public static Rational min(Rational... rationals)
rationals
- an arbitrary number of rationalsjava.lang.IllegalArgumentException
- if no rationals are passed inpublic static Rational min(java.lang.Iterable<Rational> rationals)
rationals
- an iterable of rationals (e.g., a list)java.lang.IllegalArgumentException
- if no rationals are passed inpublic static Rational max(Rational... rationals)
rationals
- an arbitrary number of rationalsjava.lang.IllegalArgumentException
- if no rationals are passed inpublic static Rational max(java.lang.Iterable<Rational> rationals)
rationals
- an iterable of rationals (e.g., a list)java.lang.IllegalArgumentException
- if no rationals are passed inpublic static Rational sum(Rational... rationals)
rationals
- an arbitrary number of rationalsjava.lang.IllegalArgumentException
- if no rationals are passed inpublic static Rational sum(java.lang.Iterable<Rational> rationals)
rationals
- an iterable of rationals (e.g., a list)java.lang.IllegalArgumentException
- if no rationals are passed inpublic static Rational product(Rational... rationals)
rationals
- an arbitrary number of rationalsjava.lang.IllegalArgumentException
- if no rationals are passed inpublic static Rational product(java.lang.Iterable<Rational> rationals)
rationals
- an iterable of rationals (e.g., a list)java.lang.IllegalArgumentException
- if no rationals are passed inpublic java.math.BigDecimal bigDecimalValue()
java.lang.ArithmeticException
- if the quotient does not have a terminating decimal expansionpublic java.math.BigDecimal bigDecimalValue(java.math.MathContext mc)
mc
- the math context to use for roundingjava.lang.ArithmeticException
- if the quotient does not have a terminating decimal expansion and the math context has either 1.) a precision of 0 (meaning exact precision) or 2.) a rounding mode of UNNECESSARYpublic byte byteValue()
byteValue
in class java.lang.Number
public double doubleValue()
doubleValue
in class java.lang.Number
BigDecimal.doubleValue()
public float floatValue()
floatValue
in class java.lang.Number
BigDecimal.floatValue()
, doubleValue()
public int intValue()
intValue
in class java.lang.Number
public long longValue()
longValue
in class java.lang.Number
public short shortValue()
shortValue
in class java.lang.Number
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int compareTo(Rational other)
compareTo
in interface java.lang.Comparable<Rational>
other
- the other rationalpublic java.lang.String toString()
toString
in class java.lang.Object