public class Fraction extends java.lang.Number implements FieldElement<Fraction>, java.lang.Comparable<Fraction>, java.io.Serializable
Modifier and Type | Field and Description |
---|---|
static Fraction |
FOUR_FIFTHS
A fraction representing "4/5".
|
static Fraction |
MINUS_ONE
A fraction representing "-1 / 1".
|
static Fraction |
ONE
A fraction representing "1".
|
static Fraction |
ONE_FIFTH
A fraction representing "1/5".
|
static Fraction |
ONE_HALF
A fraction representing "1/2".
|
static Fraction |
ONE_QUARTER
A fraction representing "1/4".
|
static Fraction |
ONE_THIRD
A fraction representing "1/3".
|
static Fraction |
THREE_FIFTHS
A fraction representing "3/5".
|
static Fraction |
THREE_QUARTERS
A fraction representing "3/4".
|
static Fraction |
TWO
A fraction representing "2 / 1".
|
static Fraction |
TWO_FIFTHS
A fraction representing "2/5".
|
static Fraction |
TWO_QUARTERS
A fraction representing "2/4".
|
static Fraction |
TWO_THIRDS
A fraction representing "2/3".
|
static Fraction |
ZERO
A fraction representing "0".
|
Constructor and Description |
---|
Fraction(double value)
Create a fraction given the double value.
|
Fraction(double value,
double epsilon,
int maxIterations)
Create a fraction given the double value and maximum error allowed.
|
Fraction(double value,
int maxDenominator)
Create a fraction given the double value and maximum denominator.
|
Fraction(int num)
Create a fraction from an int.
|
Fraction(int num,
int den)
Create a fraction given the numerator and denominator.
|
Modifier and Type | Method and Description |
---|---|
Fraction |
abs()
Returns the absolute value of this fraction.
|
Fraction |
add(Fraction fraction)
Adds the value of this fraction to another, returning the result in reduced form.
|
Fraction |
add(int i)
Add an integer to the fraction.
|
int |
compareTo(Fraction object)
Compares this object to another based on size.
|
Fraction |
divide(Fraction fraction)
Divide the value of this fraction by another.
|
Fraction |
divide(int i)
Divide the fraction by an integer.
|
double |
doubleValue()
Gets the fraction as a double.
|
boolean |
equals(java.lang.Object other)
Test for the equality of two fractions.
|
float |
floatValue()
Gets the fraction as a float.
|
int |
getDenominator()
Access the denominator.
|
FractionField |
getField()
Get the
Field to which the instance belongs. |
int |
getNumerator()
Access the numerator.
|
static Fraction |
getReducedFraction(int numerator,
int denominator)
Creates a
Fraction instance with the 2 parts
of a fraction Y/Z. |
int |
hashCode()
Gets a hashCode for the fraction.
|
int |
intValue()
Gets the fraction as an int.
|
long |
longValue()
Gets the fraction as a long.
|
Fraction |
multiply(Fraction fraction)
Multiplies the value of this fraction by another, returning the
result in reduced form.
|
Fraction |
multiply(int i)
Multiply the fraction by an integer.
|
Fraction |
negate()
Return the additive inverse of this fraction.
|
Fraction |
reciprocal()
Return the multiplicative inverse of this fraction.
|
Fraction |
subtract(Fraction fraction)
Subtracts the value of another fraction from the value of this one,
returning the result in reduced form.
|
Fraction |
subtract(int i)
Subtract an integer from the fraction.
|
java.lang.String |
toString()
Returns the
String representing this fraction, ie
"num / dem" or just "num" if the denominator is one. |
public static final Fraction TWO
public static final Fraction ONE
public static final Fraction ZERO
public static final Fraction FOUR_FIFTHS
public static final Fraction ONE_FIFTH
public static final Fraction ONE_HALF
public static final Fraction ONE_QUARTER
public static final Fraction ONE_THIRD
public static final Fraction THREE_FIFTHS
public static final Fraction THREE_QUARTERS
public static final Fraction TWO_FIFTHS
public static final Fraction TWO_QUARTERS
public static final Fraction TWO_THIRDS
public static final Fraction MINUS_ONE
public Fraction(double value) throws FractionConversionException
value
- the double value to convert to a fraction.FractionConversionException
- if the continued fraction failed to
converge.public Fraction(double value, double epsilon, int maxIterations) throws FractionConversionException
References:
value
- the double value to convert to a fraction.epsilon
- maximum error allowed. The resulting fraction is within
epsilon
of value
, in absolute terms.maxIterations
- maximum number of convergentsFractionConversionException
- if the continued fraction failed to
converge.public Fraction(double value, int maxDenominator) throws FractionConversionException
References:
value
- the double value to convert to a fraction.maxDenominator
- The maximum allowed value for denominatorFractionConversionException
- if the continued fraction failed to
convergepublic Fraction(int num)
num
- the numerator.public Fraction(int num, int den)
num
- the numerator.den
- the denominator.java.lang.ArithmeticException
- if the denominator is zero
public Fraction abs()
public int compareTo(Fraction object)
compareTo
in interface java.lang.Comparable<Fraction>
object
- the object to compare topublic double doubleValue()
doubleValue
in class java.lang.Number
public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
other
- fraction to test for equality to this fractionFraction
, or not equal
to this fraction instance.public float floatValue()
floatValue
in class java.lang.Number
public int getDenominator()
public int getNumerator()
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 Fraction negate()
public Fraction reciprocal()
public Fraction add(Fraction fraction)
Adds the value of this fraction to another, returning the result in reduced form. The algorithm follows Knuth, 4.5.1.
add
in interface FieldElement<Fraction>
fraction
- the fraction to add, must not be null
Fraction
instance with the resulting valuesjava.lang.IllegalArgumentException
- if the fraction is null
java.lang.ArithmeticException
- if the resulting numerator or denominator exceeds
Integer.MAX_VALUE
public Fraction add(int i)
i
- the integer to add.public Fraction subtract(Fraction fraction)
Subtracts the value of another fraction from the value of this one, returning the result in reduced form.
subtract
in interface FieldElement<Fraction>
fraction
- the fraction to subtract, must not be null
Fraction
instance with the resulting valuesjava.lang.IllegalArgumentException
- if the fraction is null
java.lang.ArithmeticException
- if the resulting numerator or denominator
cannot be represented in an int
.public Fraction subtract(int i)
i
- the integer to subtract.public Fraction multiply(Fraction fraction)
Multiplies the value of this fraction by another, returning the result in reduced form.
multiply
in interface FieldElement<Fraction>
fraction
- the fraction to multiply by, must not be null
Fraction
instance with the resulting valuesjava.lang.IllegalArgumentException
- if the fraction is null
java.lang.ArithmeticException
- if the resulting numerator or denominator exceeds
Integer.MAX_VALUE
public Fraction multiply(int i)
i
- the integer to multiply by.public Fraction divide(Fraction fraction)
Divide the value of this fraction by another.
divide
in interface FieldElement<Fraction>
fraction
- the fraction to divide by, must not be null
Fraction
instance with the resulting valuesjava.lang.IllegalArgumentException
- if the fraction is null
java.lang.ArithmeticException
- if the fraction to divide by is zerojava.lang.ArithmeticException
- if the resulting numerator or denominator exceeds
Integer.MAX_VALUE
public Fraction divide(int i)
i
- the integer to divide by.public static Fraction getReducedFraction(int numerator, int denominator)
Creates a Fraction
instance with the 2 parts
of a fraction Y/Z.
Any negative signs are resolved to be on the numerator.
numerator
- the numerator, for example the three in 'three sevenths'denominator
- the denominator, for example the seven in 'three sevenths'java.lang.ArithmeticException
- if the denominator is zero
public java.lang.String toString()
Returns the String
representing this fraction, ie
"num / dem" or just "num" if the denominator is one.
toString
in class java.lang.Object
Object.toString()
public FractionField getField()
Field
to which the instance belongs.getField
in interface FieldElement<Fraction>
Field
to which the instance belongsCopyright © 2010 - 2020 Adobe. All Rights Reserved