public class Fraction extends Number implements FieldElement<Fraction>, Comparable<Fraction>, 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(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.
|
double |
percentageValue()
Gets the fraction percentage as a
double . |
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.
|
String |
toString()
Returns the
String representing this fraction, ie
"num / dem" or just "num" if the denominator is one. |
byteValue, shortValue
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.MathArithmeticException
- if the denominator is zero
public Fraction abs()
public int compareTo(Fraction object)
compareTo
in interface Comparable<Fraction>
object
- the object to compare toobject
, +1 if this is greater
than object
, 0 if they are equal.public double doubleValue()
double
. This calculates the fraction as
the numerator divided by denominator.doubleValue
in class Number
double
public boolean equals(Object other)
public float floatValue()
float
. This calculates the fraction as
the numerator divided by denominator.floatValue
in class Number
float
public int getDenominator()
public int getNumerator()
public int hashCode()
public int intValue()
int
. This returns the whole number part
of the fraction.public long longValue()
long
. This returns the whole number part
of the fraction.public Fraction negate()
negate
in interface FieldElement<Fraction>
public Fraction reciprocal()
reciprocal
in interface FieldElement<Fraction>
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 valuesNullArgumentException
- if the fraction is null
MathArithmeticException
- 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 valuesNullArgumentException
- if the fraction is null
MathArithmeticException
- 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 valuesNullArgumentException
- if the fraction is null
MathArithmeticException
- if the resulting numerator or denominator exceeds
Integer.MAX_VALUE
public Fraction multiply(int i)
multiply
in interface FieldElement<Fraction>
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 valuesIllegalArgumentException
- if the fraction is null
MathArithmeticException
- if the fraction to divide by is zeroMathArithmeticException
- if the resulting numerator or denominator exceeds
Integer.MAX_VALUE
public Fraction divide(int i)
i
- the integer
to divide by.public double percentageValue()
Gets the fraction percentage as a double
. This calculates the
fraction as the numerator divided by denominator multiplied by 100.
double
.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'MathArithmeticException
- if the denominator is zero
public String toString()
Returns the String
representing this fraction, ie
"num / dem" or just "num" if the denominator is one.
toString
in class Object
Object.toString()
public FractionField getField()
Field
to which the instance belongs.getField
in interface FieldElement<Fraction>
Field
to which the instance belongsCopyright © 2003–2016 The Apache Software Foundation. All rights reserved.