|
Uncommons Maths API (Version 1.2.3) |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.lang.Number org.uncommons.maths.number.Rational
public final class Rational
Immutable value object for representing a rational number (or vulgar fraction). Instances of this class provide a way to perform arithmetic on fractional values without loss of precision. This implementation automatically simplifies fractions (3/6 is stored as 1/2). The implementation also requires that the denominator is positive. The numerator may be negative.
Field Summary | |
---|---|
static Rational |
HALF
Convenient constant representing a value of a half (1/2 as a rational). |
static Rational |
ONE
Convenient constant representing a value of one (1/1 as a rational). |
static Rational |
QUARTER
Convenient constant representing a value of a quarter (1/4 as a rational). |
static Rational |
THIRD
Convenient constant representing a value of a third (1/3 as a rational). |
static Rational |
THREE_QUARTERS
Convenient constant representing a value of three quarters (3/4 as a rational). |
static Rational |
TWO_THIRDS
Convenient constant representing a value of two thirds (2/3 as a rational). |
static Rational |
ZERO
Convenient constant representing a value of zero (0/1 as a rational). |
Constructor Summary | |
---|---|
Rational(BigDecimal value)
Creates a rational value equivalent to the specified decimal value. |
|
Rational(long value)
Creates a rational value equivalent to the specified integer value. |
|
Rational(long numerator,
long denominator)
Creates a vulgar fraction with the specified numerator and denominator. |
Method Summary | |
---|---|
Rational |
add(Rational value)
Add the specified value to this value and return the result as a new object (also a rational). |
int |
compareTo(Rational other)
Compares this value with the specified object for order. |
Rational |
divide(Rational value)
Divide this rational by the specified value and return the result as a new object (also a Rational). |
double |
doubleValue()
Returns the result of dividing the numerator by the denominator. |
boolean |
equals(Object other)
Determines whether this rational value is equal to some other object. |
float |
floatValue()
Returns the result of dividing the numerator by the denominator. |
long |
getDenominator()
Returns the denominator (divisor) of the fraction. |
long |
getNumerator()
Returns the numerator of the fraction. |
int |
hashCode()
Over-ridden to be consistent with equals(Object) . |
int |
intValue()
Returns the integer equivalent of this rational number. |
long |
longValue()
Returns the integer equivalent of this rational number as a long. |
Rational |
multiply(Rational value)
Multiply this rational by the specified value and return the result as a new object (also a Rational). |
Rational |
subtract(Rational value)
Subtract the specified value from this value and return the result as a new object (also a rational). |
String |
toString()
Returns a String representation of the rational number, expressed as a vulgar fraction (i.e. |
Methods inherited from class java.lang.Number |
---|
byteValue, shortValue |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final Rational ZERO
public static final Rational QUARTER
public static final Rational THIRD
public static final Rational HALF
public static final Rational TWO_THIRDS
public static final Rational THREE_QUARTERS
public static final Rational ONE
Constructor Detail |
---|
public Rational(long numerator, long denominator)
numerator
- The fraction's numerator (may be negative).denominator
- The fraction's denominator (must be greater than or
equal to 1).public Rational(long value)
value
- The value of this rational as an integer.public Rational(BigDecimal value)
value
- The value of this rational as a fractional decimal.
ArithmeticException
- If the BigDecimal value is too large to be
represented as a Rational.Method Detail |
---|
public long getNumerator()
public long getDenominator()
public Rational add(Rational value)
value
- The value to add to this rational.
public Rational subtract(Rational value)
value
- The value to subtract from this rational.
public Rational multiply(Rational value)
value
- The amount to multiply by.
public Rational divide(Rational value)
value
- The amount to divide by.
public int intValue()
doubleValue()
).
intValue
in class Number
public long longValue()
doubleValue()
).
longValue
in class Number
public float floatValue()
floatValue
in class Number
public double doubleValue()
doubleValue
in class Number
public boolean equals(Object other)
equals
in class Object
other
- The object to compare against.
public int hashCode()
equals(Object)
.
hashCode
in class Object
public String toString()
toString
in class Object
public int compareTo(Rational other)
compareTo
in interface Comparable<Rational>
other
- Another Rational value.
|
Uncommons Maths API (Version 1.2.3) |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |