public final class RationalNumber extends java.lang.Number implements java.lang.Comparable<RationalNumber>, java.io.Serializable
Modifier and Type | Field and Description |
---|---|
static RationalNumber |
E
This is the constant e, as approximated from Math.E
|
static RationalNumber |
MAX_VALUE
The maximum value that can be contained in a rational number
|
static RationalNumber |
MIN_VALUE
The minimum value that can be contained in a rational number
|
static RationalNumber |
NEGATIVE_ONE
The number -1
|
static RationalNumber |
ONE
The number 1
|
static RationalNumber |
ONE_FOURTH
The number 1/4
|
static RationalNumber |
ONE_HALF
The number 1/2
|
static RationalNumber |
ONE_THIRD
The number 1/3
|
static RationalNumber |
PI
This is the constant pi, as approximated from Math.PI
|
static RationalNumber |
THREE_FOURTHS
The number 3/4
|
static RationalNumber |
TWO_THIRDS
The number 2/3
|
static RationalNumber |
ZERO
The number 0
|
Constructor and Description |
---|
RationalNumber(double decimal)
Creates a new Rational Number from the given double.
|
RationalNumber(long numerator,
long denominator)
Creates a new rational number with the given numerator and denominator
|
RationalNumber(RationalNumber number)
Copy constructor
|
RationalNumber(java.lang.String s)
Creates a new Rational Number from the given value.
|
Modifier and Type | Method and Description |
---|---|
RationalNumber |
absoluteValue()
Returns the absolute value of this rational number
|
RationalNumber |
add(double amount)
Returns this number plus another number, and attempts to stave off overflow
|
RationalNumber |
add(RationalNumber amount)
Returns this number plus another number, and attempts to stave off overflow
|
int |
compareTo(RationalNumber otherNumber) |
RationalNumber |
dividedBy(double amount)
Returns this number divided by amount
|
RationalNumber |
dividedBy(long amount)
Returns this number divided by amount
|
RationalNumber |
dividedBy(RationalNumber amount)
Returns this number divided by amount
|
double |
doubleValue() |
boolean |
equals(java.lang.Object other) |
float |
floatValue() |
long |
getDenominator()
Returns the denominator of the current rational number
|
long |
getNumerator()
Returns the numerator of the current rational number
|
RationalNumber |
getRationalPart()
Gets the fraction portion of the number.
|
long |
getWholePart()
Gets the whole number (integer) portion of the number
|
boolean |
greaterThan(double other)
Determines whether this rational number is greater than the other number
|
boolean |
greaterThan(RationalNumber other)
Determines whether this rational number is greater than the other rational number
|
boolean |
greaterThanOrEqual(double other)
Determines whether this rational number is greater than or equal to the other number
|
boolean |
greaterThanOrEqual(RationalNumber other)
Determines whether this rational number is greater than or equal to the other rational number
|
int |
hashCode() |
int |
intValue() |
boolean |
isApproximated()
Returns true if this number has been approximated (overflow didn't allow a large enough numerator and denominator)
|
boolean |
lessThan(double other)
Determines whether this rational number is less than the other number
|
boolean |
lessThan(RationalNumber other)
Determines whether this rational number is less than the other rational number
|
boolean |
lessThanOrEqual(double other)
Determines whether this rational number is less than or equal to the other number
|
boolean |
lessThanOrEqual(RationalNumber other)
Determines whether this rational number is less than or equal to the other rational number
|
long |
longValue() |
static RationalNumber |
mediant(RationalNumber firstRationalNumber,
RationalNumber secondRationalNumber)
create and return a new rational (firstRationalNumber.numerator + secondRationalNumber.numerator) / (firstRationalNumber.denominator +
secondRationalNumber.denominator)
|
RationalNumber |
minus(double amount)
Returns this number minus another number
|
RationalNumber |
minus(long amount)
Returns this number minus another number
|
RationalNumber |
minus(RationalNumber amount)
Returns this number minus another number
|
RationalNumber |
negate()
Returns the negative value of this number
|
RationalNumber |
plus(double amount)
Returns this number plus another number, and attempts to stave off overflow
|
RationalNumber |
plus(long amount)
Returns this number plus another number, and attempts to stave off overflow
|
RationalNumber |
plus(RationalNumber amount)
Returns this number plus another number, and attempts to stave off overflow
|
RationalNumber |
power(double power)
Raises the rational number to a power
|
RationalNumber |
power(RationalNumber power)
Raises the rational number to a power
|
RationalNumber |
reciprocal()
Gets the reciprocal of this number
|
RationalNumber |
roundToNearestMultiple(RationalNumber multiple)
Returns a new rational number that is the nearest multiple of the given rational number.
|
RationalNumber |
times(double amount)
Returns this number times another number, and attempts to prevent overflow
|
RationalNumber |
times(long amount)
Returns this number times another number, and attempts to prevent overflow
|
RationalNumber |
times(RationalNumber amount)
Returns this number times another number, and attempts to prevent overflow
|
java.lang.String |
toString() |
java.lang.String |
toStringWithWholePart()
Converts the number to a fraction, such as 1 3/4
|
static RationalNumber |
valueOf(double rationalNumber)
Returns the rational number represented by the given string
|
static RationalNumber |
valueOf(long number)
Returns the rational number represented by the given string
|
static RationalNumber |
valueOf(java.lang.String rationalNumber)
Returns the rational number represented by the given string
|
public static final RationalNumber ZERO
public static final RationalNumber ONE
public static final RationalNumber NEGATIVE_ONE
public static final RationalNumber ONE_HALF
public static final RationalNumber ONE_THIRD
public static final RationalNumber ONE_FOURTH
public static final RationalNumber TWO_THIRDS
public static final RationalNumber THREE_FOURTHS
public static final RationalNumber MAX_VALUE
public static final RationalNumber MIN_VALUE
@NotNull public static final RationalNumber E
@NotNull public static final RationalNumber PI
public RationalNumber(long numerator, long denominator)
numerator
- The numerator in the rational numberdenominator
- The denominator in the rational numberpublic RationalNumber(@NotNull RationalNumber number)
number
- The number to copypublic RationalNumber(double decimal)
decimal
- The decimal value to store for the value of the fractionpublic RationalNumber(@NotNull java.lang.String s) throws java.text.ParseException
s
- The value to store for the value of the fraction.java.text.ParseException
- If there was a parse exceptionpublic static RationalNumber valueOf(@NotNull java.lang.String rationalNumber) throws java.text.ParseException
rationalNumber
- The text containing the rational numberjava.text.ParseException
- If the text could not be parsed as a rational numberpublic static RationalNumber valueOf(long number)
number
- The whole number to convert to a rational numberpublic static RationalNumber valueOf(double rationalNumber)
rationalNumber
- The rational number as best represented by a doublepublic RationalNumber roundToNearestMultiple(@NotNull RationalNumber multiple)
multiple
- The multiple to returnpublic long getNumerator()
public long getDenominator()
public long getWholePart()
public RationalNumber getRationalPart()
public static RationalNumber mediant(@NotNull RationalNumber firstRationalNumber, @NotNull RationalNumber secondRationalNumber)
firstRationalNumber
- The first rational numbersecondRationalNumber
- The second rational number@NotNull public RationalNumber times(long amount)
amount
- The other number@NotNull public RationalNumber times(double amount)
amount
- The other number@NotNull public RationalNumber times(@NotNull RationalNumber amount)
amount
- The other numberpublic RationalNumber add(double amount)
amount
- The other numberpublic RationalNumber add(@NotNull RationalNumber amount)
amount
- The other numberpublic RationalNumber plus(long amount)
amount
- The other numberpublic RationalNumber plus(double amount)
amount
- The other numberpublic RationalNumber plus(@NotNull RationalNumber amount)
amount
- The other numberpublic RationalNumber negate()
public RationalNumber minus(long amount)
amount
- The other numberpublic RationalNumber minus(double amount)
amount
- The other numberpublic RationalNumber minus(@NotNull RationalNumber amount)
amount
- The other numberpublic RationalNumber reciprocal()
@NotNull public RationalNumber dividedBy(long amount)
amount
- The other number@NotNull public RationalNumber dividedBy(double amount)
amount
- The other number@NotNull public RationalNumber dividedBy(@NotNull RationalNumber amount)
amount
- The other numberpublic RationalNumber power(double power)
power
- The power to raise the number to@NotNull public RationalNumber absoluteValue()
public RationalNumber power(@NotNull RationalNumber power)
power
- The power to raise the number topublic boolean lessThan(double other)
other
- The other numberpublic boolean lessThan(@NotNull RationalNumber other)
other
- The other rational numberpublic boolean lessThanOrEqual(double other)
other
- The other numberpublic boolean lessThanOrEqual(@NotNull RationalNumber other)
other
- The other rational numberpublic boolean greaterThan(double other)
other
- The other numberpublic boolean greaterThan(@NotNull RationalNumber other)
other
- The other rational numberpublic boolean greaterThanOrEqual(double other)
other
- The other numberpublic boolean greaterThanOrEqual(@NotNull RationalNumber other)
other
- The other rational numberpublic boolean isApproximated()
public java.lang.String toStringWithWholePart()
public java.lang.String toString()
toString
in class java.lang.Object
public int compareTo(RationalNumber otherNumber)
compareTo
in interface java.lang.Comparable<RationalNumber>
public boolean equals(@Nullable java.lang.Object other)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public int intValue()
intValue
in class java.lang.Number
public long longValue()
longValue
in class java.lang.Number
public float floatValue()
floatValue
in class java.lang.Number
public double doubleValue()
doubleValue
in class java.lang.Number