net.charlesames.utility.math
Class Ratio

java.lang.Object
  extended by net.charlesames.utility.math.Ratio
All Implemented Interfaces:
java.lang.Comparable<java.lang.Object>

public class Ratio
extends java.lang.Object
implements java.lang.Comparable<java.lang.Object>

Implements class of rational numbers; that is, numbers which can be expressed using a numerator and a denominator.

Author:
Charles Ames

Field Summary
static Ratio DUPLICITY
          Ratio with numerator 2 and denominator 1.
static Ratio MAX_VALUE
          Ratio with numerator Long.MAX_VALUE and denominator 1.
static Ratio MIN_VALUE
          Ratio with numerator Long.MIN_VALUE and denominator 1.
static java.lang.String REGEX
          Regular expression for evaluating ratio text.
static java.lang.String TYPE_NAME
          XSD type name for ratio text.
static Ratio UNITY
          Ratio with numerator 1 and denominator 1.
static Ratio ZERO
          Ratio with numerator 0 and denominator 1.
 
Method Summary
 Ratio add(long addend)
          Add this ratio to the indicated long.
 Ratio add(Ratio addend)
          Add this ratio to another ratio.
 int compareTo(java.lang.Object arg0)
           
static XmlType createXmlType()
          Check if an XmlType instance exists for Ratio data.
 Ratio divide(long divisor)
          Divide this ratio by the indicated long.
 Ratio divide(Ratio divisor)
          Divide another ratio into this ratio.
 boolean equals(java.lang.Object arg)
           
 boolean ge(Ratio other)
          Test if this ratio is greater than or equal to another ratio.
 long getDenominator()
          Get the denominator.
static Ratio getInstance(long numerator, long denominator)
          Get a Ratio instance.
 long getNumerator()
          Get the numerator.
 double getValue()
          Get the quotient obtained by dividing the denominator into the numerator.
 boolean gt(Ratio other)
          Test if this ratio is greater than another ratio.
 Ratio invert()
          Invert this ratio.
 boolean le(Ratio other)
          Test if this ratio is less than or equal to another ratio.
 boolean lt(Ratio other)
          Test if this ratio is less than another ratio.
 Ratio maximum(Ratio other)
          Select the maxumum of this and another Ratio.
 Ratio minimum(Ratio other)
          Select the minimum of this and another Ratio.
 Ratio multiply(long factor)
          Multiply this ratio by the indicated long.
 Ratio multiply(Ratio factor)
          Multiply this ratio by another ratio.
 Ratio negate()
          Negate this ratio.
 Ratio normalize()
          Map this ratio into the range from .
static Ratio parse(java.lang.String text)
          Parse a ratio in long-colon-long format.
 Ratio power(long exponent)
          Raise this ratio to the indicated power.
 int sign()
          Determine whether this ratio is positive, negative, or zero.
 Ratio subtract(long subtrahend)
          Subtract the indicated long from this ratio.
 Ratio subtract(Ratio subtrahend)
          Subtract another ratio from this ratio.
 java.lang.String toString()
           
static Ratio valueOf(double value)
          Find a Ratio with a denominator up to the indicated limit that approximates the value within 10 to the -7 power;
static Ratio valueOf(double value, int gcd)
          Find the Ratio with the indicated granularity that most closely approximates the indicated value; The result is calculated by constructing a fraction with gcd as the denominator, which the Ratio constructor simplifies.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TYPE_NAME

public static final java.lang.String TYPE_NAME
XSD type name for ratio text.

See Also:
Constant Field Values

REGEX

public static final java.lang.String REGEX
Regular expression for evaluating ratio text.

See Also:
Constant Field Values

ZERO

public static final Ratio ZERO
Ratio with numerator 0 and denominator 1.


UNITY

public static final Ratio UNITY
Ratio with numerator 1 and denominator 1.


DUPLICITY

public static final Ratio DUPLICITY
Ratio with numerator 2 and denominator 1.


MAX_VALUE

public static final Ratio MAX_VALUE
Ratio with numerator Long.MAX_VALUE and denominator 1.


MIN_VALUE

public static final Ratio MIN_VALUE
Ratio with numerator Long.MIN_VALUE and denominator 1.

Method Detail

getInstance

public static Ratio getInstance(long numerator,
                                long denominator)
Get a Ratio instance.

Parameters:
numerator - The long integer on top of the fraction.
denominator - The long integer on the bottom of the fraction. The greatest common denominator of the numerator and the denominator is divided out of both numbers. If the denominator is negative, the numerator and denominator signs are flipped.
Returns:
A Ratio representing the quotient of the numerator and the denominator, reduced to simplest terms.

createXmlType

public static XmlType createXmlType()
Check if an XmlType instance exists for Ratio data. If not, create one.

Returns:
The XmlType instance for Ratio data.

getNumerator

public long getNumerator()
Get the numerator.

Returns:
The long integer on top of the fraction.

getDenominator

public long getDenominator()
Get the denominator.

Returns:
The long integer on the bottom of the fraction. This value is always positive.

getValue

public double getValue()
Get the quotient obtained by dividing the denominator into the numerator.

Returns:
The quotient obtained by dividing the denominator into the numerator.

add

public Ratio add(Ratio addend)
Add this ratio to another ratio.

Parameters:
addend - The other ratio.
Returns:
The sum of the two ratios.

add

public Ratio add(long addend)
Add this ratio to the indicated long.

Parameters:
addend - The indicated long.
Returns:
The sum of this ratio and the long.

negate

public Ratio negate()
Negate this ratio.

Returns:
A ratio with the same denominator and the negated numerator.

normalize

public Ratio normalize()
Map this ratio into the range from .

Returns:
A ratio with the same denominator and the negated numerator.

subtract

public Ratio subtract(Ratio subtrahend)
Subtract another ratio from this ratio.

Parameters:
subtrahend - The other ratio.
Returns:
The remainder of the two ratios.

subtract

public Ratio subtract(long subtrahend)
Subtract the indicated long from this ratio.

Parameters:
subtrahend - The indicated long.
Returns:
The remainder of this ratio and the long.

invert

public Ratio invert()
Invert this ratio.

Returns:
A ratio with the numerator and denominator flipped.

multiply

public Ratio multiply(Ratio factor)
Multiply this ratio by another ratio.

Parameters:
factor - The other ratio.
Returns:
The product of the two ratios.

multiply

public Ratio multiply(long factor)
Multiply this ratio by the indicated long.

Parameters:
factor - The indicated long.
Returns:
The product of this ratio and the long.

divide

public Ratio divide(Ratio divisor)
Divide another ratio into this ratio.

Parameters:
divisor - The other ratio.
Returns:
The quotient of the two ratios.

divide

public Ratio divide(long divisor)
Divide this ratio by the indicated long.

Parameters:
divisor - The indicated long.
Returns:
The quotient of this ratio and the long.

power

public Ratio power(long exponent)
Raise this ratio to the indicated power.

Parameters:
exponent - The indicated power. Must not be negative.
Returns:
The quotient of this ratio and the long.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

parse

public static Ratio parse(java.lang.String text)
Parse a ratio in long-colon-long format.

Parameters:
text - The source text.
Returns:
The newly created Ratio instance.

valueOf

public static Ratio valueOf(double value)
Find a Ratio with a denominator up to the indicated limit that approximates the value within 10 to the -7 power;

Parameters:
value - The input.
Returns:
The output.

valueOf

public static Ratio valueOf(double value,
                            int gcd)
Find the Ratio with the indicated granularity that most closely approximates the indicated value; The result is calculated by constructing a fraction with gcd as the denominator, which the Ratio constructor simplifies.

Parameters:
value - The input.
gcd - The indicated granularity.
Returns:
The output. A Ratio approximating the input.

sign

public int sign()
Determine whether this ratio is positive, negative, or zero.

Returns:
1 if the numerator is positive; -1 if the numerator is negative; 0 otherwise.

compareTo

public int compareTo(java.lang.Object arg0)
Specified by:
compareTo in interface java.lang.Comparable<java.lang.Object>

gt

public boolean gt(Ratio other)
Test if this ratio is greater than another ratio.

Parameters:
other - The other ratio.
Returns:
True if this ratio is greater than the other ratio; false otherwise.

ge

public boolean ge(Ratio other)
Test if this ratio is greater than or equal to another ratio.

Parameters:
other - The other ratio.
Returns:
True if this ratio is greater than or equal to the other ratio; false otherwise.

lt

public boolean lt(Ratio other)
Test if this ratio is less than another ratio.

Parameters:
other - The other ratio.
Returns:
True if this ratio is less than the other ratio; false otherwise.

le

public boolean le(Ratio other)
Test if this ratio is less than or equal to another ratio.

Parameters:
other - The other ratio.
Returns:
True if this ratio is less than or equal to the other ratio; false otherwise.

minimum

public Ratio minimum(Ratio other)
Select the minimum of this and another Ratio.

Parameters:
other - The other Ratio.
Returns:
The minimum of this and the other Ratio.

maximum

public Ratio maximum(Ratio other)
Select the maxumum of this and another Ratio.

Parameters:
other - The other Ratio.
Returns:
The minimum of this and the other Ratio.

equals

public boolean equals(java.lang.Object arg)
Overrides:
equals in class java.lang.Object