Top Description Fields Constructors Methods
org.apache.commons.math3.fraction

public Class BigFraction

extends Number
implements FieldElement<BigFraction>, Comparable<BigFraction>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Comparable, org.apache.commons.math3.FieldElement
Imports
java.io.Serializable, java.math.BigDecimal, .BigInteger, org.apache.commons.math3.FieldElement, org.apache.commons.math3.exception.MathArithmeticException, .MathIllegalArgumentException, .NullArgumentException, .ZeroException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.util.ArithmeticUtils, .FastMath, .MathUtils

Representation of a rational number without any overflow. This class is immutable.
Since
2.0

Field Summary

Modifier and TypeField and Description
private final BigInteger
denominator

The denominator.

public static final BigFraction
FOUR_FIFTHS

A fraction representing "4/5".

public static final BigFraction
MINUS_ONE

A fraction representing "-1 / 1".

private final BigInteger
numerator

The numerator.

public static final BigFraction
ONE

A fraction representing "1".

public static final BigFraction
ONE_FIFTH

A fraction representing "1/5".

public static final BigFraction
ONE_HALF

A fraction representing "1/2".

private static final BigInteger
ONE_HUNDRED

BigInteger representation of 100.

public static final BigFraction
ONE_QUARTER

A fraction representing "1/4".

public static final BigFraction
ONE_THIRD

A fraction representing "1/3".

private static final long
serialVersionUID

Hides java.lang.Number.serialVersionUID.

Serializable version identifier.
public static final BigFraction
THREE_FIFTHS

A fraction representing "3/5".

public static final BigFraction
THREE_QUARTERS

A fraction representing "3/4".

public static final BigFraction
TWO

A fraction representing "2 / 1".

public static final BigFraction
TWO_FIFTHS

A fraction representing "2/5".

public static final BigFraction
TWO_QUARTERS

A fraction representing "2/4".

public static final BigFraction
TWO_THIRDS

A fraction representing "2/3".

public static final BigFraction
ZERO

A fraction representing "0".

Constructor Summary

AccessConstructor and Description
public
BigFraction(final BigInteger
the numerator.
num
)

Create a BigFraction equivalent to the passed BigInteger, ie "num / 1".

public
BigFraction(BigInteger
the numerator, must not be null.
num
,
BigInteger
the denominator, must not be null.
den
)

Create a BigFraction given the numerator and denominator as BigInteger.

public
BigFraction(final double
the double value to convert to a fraction.
value
)

Create a fraction given the double value.

public
BigFraction(final double
the double value to convert to a fraction.
value
,
final double
maximum error allowed. The resulting fraction is within epsilon of value, in absolute terms.
epsilon
,
final int
maximum number of convergents.
maxIterations
)

Create a fraction given the double value and maximum error allowed.

private
BigFraction(final double
the double value to convert to a fraction.
value
,
final double
maximum error allowed. The resulting fraction is within epsilon of value, in absolute terms.
epsilon
,
final int
maximum denominator value allowed.
maxDenominator
,
int
maximum number of convergents.
maxIterations
)

Create a fraction given the double value and either the maximum error allowed or the maximum number of denominator digits.

public
BigFraction(final double
the double value to convert to a fraction.
value
,
final int
The maximum allowed value for denominator.
maxDenominator
)

Create a fraction given the double value and maximum denominator.

public
BigFraction(final int
the numerator.
num
)

Create a BigFraction equivalent to the passed int, ie "num / 1".

public
BigFraction(final int
the numerator.
num
,
final int
the denominator.
den
)

Create a BigFraction given the numerator and denominator as simple int.

public
BigFraction(final long
the numerator.
num
)

Create a BigFraction equivalent to the passed long, ie "num / 1".

public
BigFraction(final long
the numerator.
num
,
final long
the denominator.
den
)

Create a BigFraction given the numerator and denominator as simple long.

Method Summary

Modifier and TypeMethod and Description
public BigFraction

Returns:

the absolute value as a BigFraction.
abs
()

Returns the absolute value of this BigFraction.

public BigFraction

Returns:

a BigFraction instance with the resulting values.
add
(final BigInteger
the BigInteger to add, must'nt be null.
bg
)

Implements org.apache.commons.math3.FieldElement.add.

Adds the value of this fraction to the passed BigInteger, returning the result in reduced form.

public BigFraction

Returns:

a BigFraction instance with the resulting values.
add
(final int
the integer to add.
i
)

Implements org.apache.commons.math3.FieldElement.add.

Adds the value of this fraction to the passed integer, returning the result in reduced form.

public BigFraction

Returns:

a BigFraction instance with the resulting values.
add
(final long
the long to add.
l
)

Implements org.apache.commons.math3.FieldElement.add.

Adds the value of this fraction to the passed long, returning the result in reduced form.

public BigFraction

Returns:

a BigFraction instance with the resulting values.
add
(final BigFraction
the BigFraction to add, must not be null.
fraction
)

Implements org.apache.commons.math3.FieldElement.add.

Adds the value of this fraction to another, returning the result in reduced form.

public BigDecimal

Returns:

the fraction as a BigDecimal.
bigDecimalValue
()

Gets the fraction as a BigDecimal.

public BigDecimal

Returns:

the fraction as a BigDecimal.
bigDecimalValue
(final int
rounding mode to apply. see BigDecimal constants.
roundingMode
)

Gets the fraction as a BigDecimal following the passed rounding mode.

public BigDecimal

Returns:

the fraction as a BigDecimal.
bigDecimalValue
(final int
scale of the BigDecimal quotient to be returned. see BigDecimal for more information.
scale
,
final int
rounding mode to apply. see BigDecimal constants.
roundingMode
)

Gets the fraction as a BigDecimal following the passed scale and rounding mode.

public int

Returns:

-1 if this is less than object, +1 if this is greater than object, 0 if they are equal.
compareTo
(final BigFraction
the object to compare to, must not be null.
object
)

Implements java.lang.Comparable.compareTo.

Compares this object to another based on size.

public BigFraction

Returns:

a BigFraction instance with the resulting values
divide
(final BigInteger
the BigInteger to divide by, must not be null
bg
)

Implements org.apache.commons.math3.FieldElement.divide.

Divide the value of this fraction by the passed BigInteger, ie this * 1 / bg, returning the result in reduced form.

public BigFraction

Returns:

a BigFraction instance with the resulting values
divide
(final int
the int to divide by
i
)

Implements org.apache.commons.math3.FieldElement.divide.

Divide the value of this fraction by the passed int, ie this * 1 / i, returning the result in reduced form.

public BigFraction

Returns:

a BigFraction instance with the resulting values
divide
(final long
the long to divide by
l
)

Implements org.apache.commons.math3.FieldElement.divide.

Divide the value of this fraction by the passed long, ie this * 1 / l, returning the result in reduced form.

public BigFraction

Returns:

a BigFraction instance with the resulting values.
divide
(final BigFraction
Fraction to divide by, must not be null.
fraction
)

Implements org.apache.commons.math3.FieldElement.divide.

Divide the value of this fraction by another, returning the result in reduced form.

public double

Returns:

the fraction as a double
doubleValue
()

Implements abstract java.lang.Number.doubleValue.

Gets the fraction as a double.

public boolean

Returns:

true if two fractions are equal, false if object is null, not an instance of BigFraction, or not equal to this fraction instance.
equals
(final Object
fraction to test for equality to this fraction, can be null.
other
)

Overrides java.lang.Object.equals.

Test for the equality of two fractions.

public float

Returns:

the fraction as a float.
floatValue
()

Implements abstract java.lang.Number.floatValue.

Gets the fraction as a float.

public BigInteger

Returns:

the denominator as a BigInteger.
getDenominator
()

Access the denominator as a BigInteger.

public int

Returns:

the denominator as a int.
getDenominatorAsInt
()

Access the denominator as a int.

public long

Returns:

the denominator as a long.
getDenominatorAsLong
()

Access the denominator as a long.

public BigFractionField
getField()

Implements org.apache.commons.math3.FieldElement.getField.

Get the Field to which the instance belongs.

public BigInteger

Returns:

the numerator as a BigInteger.
getNumerator
()

Access the numerator as a BigInteger.

public int

Returns:

the numerator as a int.
getNumeratorAsInt
()

Access the numerator as a int.

public long

Returns:

the numerator as a long.
getNumeratorAsLong
()

Access the numerator as a long.

public static BigFraction

Returns:

a new fraction instance, with the numerator and denominator reduced.
getReducedFraction
(final int
the numerator, for example the three in 'three sevenths'.
numerator
,
final int
the denominator, for example the seven in 'three sevenths'.
denominator
)

Creates a BigFraction instance with the 2 parts of a fraction Y/Z.

public int

Returns:

a hash code value for this object.
hashCode
()

Overrides java.lang.Object.hashCode.

Gets a hashCode for the fraction.

public int

Returns:

the whole number fraction part.
intValue
()

Implements abstract java.lang.Number.intValue.

Gets the fraction as an int.

public long

Returns:

the whole number fraction part.
longValue
()

Implements abstract java.lang.Number.longValue.

Gets the fraction as a long.

public BigFraction

Returns:

a BigFraction instance with the resulting values.
multiply
(final BigInteger
the BigInteger to multiply by.
bg
)

Implements org.apache.commons.math3.FieldElement.multiply.

Multiplies the value of this fraction by the passed BigInteger, returning the result in reduced form.

public BigFraction

Returns:

a BigFraction instance with the resulting values.
multiply
(final int
the int to multiply by.
i
)

Implements org.apache.commons.math3.FieldElement.multiply, org.apache.commons.math3.FieldElement.multiply.

Multiply the value of this fraction by the passed int, returning the result in reduced form.

public BigFraction

Returns:

a BigFraction instance with the resulting values.
multiply
(final long
the long to multiply by.
l
)

Implements org.apache.commons.math3.FieldElement.multiply.

Multiply the value of this fraction by the passed long, returning the result in reduced form.

public BigFraction

Returns:

a BigFraction instance with the resulting values.
multiply
(final BigFraction
Fraction to multiply by, must not be null.
fraction
)

Implements org.apache.commons.math3.FieldElement.multiply.

Multiplies the value of this fraction by another, returning the result in reduced form.

public BigFraction

Returns:

the negation of this fraction.
negate
()

Implements org.apache.commons.math3.FieldElement.negate.

Return the additive inverse of this fraction, returning the result in reduced form.

public double

Returns:

the fraction percentage as a double.
percentageValue
()

Gets the fraction percentage as a double.

public BigFraction

Returns:

thisexponent.
pow
(final int
exponent to which this BigFraction is to be raised.
exponent
)

Returns a BigFraction whose value is (this<sup>exponent</sup>), returning the result in reduced form.

public BigFraction

Returns:

thisexponent as a BigFraction.
pow
(final long
exponent to which this BigFraction is to be raised.
exponent
)

Returns a BigFraction whose value is (thisexponent), returning the result in reduced form.

public BigFraction

Returns:

thisexponent as a BigFraction.
pow
(final BigInteger
exponent to which this BigFraction is to be raised.
exponent
)

Returns a BigFraction whose value is (thisexponent), returning the result in reduced form.

public double

Returns:

thisexponent.
pow
(final double
exponent to which this BigFraction is to be raised.
exponent
)

Returns a double whose value is (thisexponent), returning the result in reduced form.

public BigFraction

Returns:

the reciprocal fraction.
reciprocal
()

Implements org.apache.commons.math3.FieldElement.reciprocal.

Return the multiplicative inverse of this fraction.

public BigFraction

Returns:

the reduced BigFraction. It doesn't change anything if the fraction can be reduced.
reduce
()

Reduce this BigFraction to its lowest terms.

public BigFraction

Returns:

a BigFraction instance with the resulting values.
subtract
(final BigInteger
the BigInteger to subtract, cannot be null.
bg
)

Implements org.apache.commons.math3.FieldElement.subtract.

Subtracts the value of an BigInteger from the value of this BigFraction, returning the result in reduced form.

public BigFraction

Returns:

a BigFraction instance with the resulting values.
subtract
(final int
the integer to subtract.
i
)

Implements org.apache.commons.math3.FieldElement.subtract.

Subtracts the value of an integer from the value of this BigFraction, returning the result in reduced form.

public BigFraction

Returns:

a BigFraction instance with the resulting values.
subtract
(final long
the long to subtract.
l
)

Implements org.apache.commons.math3.FieldElement.subtract.

Subtracts the value of a long from the value of this BigFraction, returning the result in reduced form.

public BigFraction

Returns:

a BigFraction instance with the resulting values
subtract
(final BigFraction
BigFraction to subtract, must not be null.
fraction
)

Implements org.apache.commons.math3.FieldElement.subtract.

Subtracts the value of another fraction from the value of this one, returning the result in reduced form.

public String

Returns:

a string representation of the fraction.
toString
()

Overrides java.lang.Object.toString.

Returns the String representing this fraction, ie "num / dem" or just "num" if the denominator is one.

Inherited from java.lang.Number:
byteValueshortValue

Field Detail

denominatorback to summary
private final BigInteger denominator

The denominator.

FOUR_FIFTHSback to summary
public static final BigFraction FOUR_FIFTHS

A fraction representing "4/5".

MINUS_ONEback to summary
public static final BigFraction MINUS_ONE

A fraction representing "-1 / 1".

numeratorback to summary
private final BigInteger numerator

The numerator.

ONEback to summary
public static final BigFraction ONE

A fraction representing "1".

ONE_FIFTHback to summary
public static final BigFraction ONE_FIFTH

A fraction representing "1/5".

ONE_HALFback to summary
public static final BigFraction ONE_HALF

A fraction representing "1/2".

ONE_HUNDREDback to summary
private static final BigInteger ONE_HUNDRED

BigInteger representation of 100.

ONE_QUARTERback to summary
public static final BigFraction ONE_QUARTER

A fraction representing "1/4".

ONE_THIRDback to summary
public static final BigFraction ONE_THIRD

A fraction representing "1/3".

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.Number.serialVersionUID.

Serializable version identifier.

THREE_FIFTHSback to summary
public static final BigFraction THREE_FIFTHS

A fraction representing "3/5".

THREE_QUARTERSback to summary
public static final BigFraction THREE_QUARTERS

A fraction representing "3/4".

TWOback to summary
public static final BigFraction TWO

A fraction representing "2 / 1".

TWO_FIFTHSback to summary
public static final BigFraction TWO_FIFTHS

A fraction representing "2/5".

TWO_QUARTERSback to summary
public static final BigFraction TWO_QUARTERS

A fraction representing "2/4".

TWO_THIRDSback to summary
public static final BigFraction TWO_THIRDS

A fraction representing "2/3".

ZEROback to summary
public static final BigFraction ZERO

A fraction representing "0".

Constructor Detail

BigFractionback to summary
public BigFraction(final BigInteger num)

Create a BigFraction equivalent to the passed BigInteger, ie "num / 1".

Parameters
num:BigInteger

the numerator.

BigFractionback to summary
public BigFraction(BigInteger num, BigInteger den)

Create a BigFraction given the numerator and denominator as BigInteger. The BigFraction is reduced to lowest terms.

Parameters
num:BigInteger

the numerator, must not be null.

den:BigInteger

the denominator, must not be null.

Exceptions
ZeroException:
if the denominator is zero.
NullArgumentException:
if either of the arguments is null
BigFractionback to summary
public BigFraction(final double value) throws MathIllegalArgumentException

Create a fraction given the double value.

This constructor behaves differently from BigFraction(double, double, int). It converts the double value exactly, considering its internal bits representation. This works for all values except NaN and infinities and does not requires any loop or convergence threshold.

Since this conversion is exact and since double numbers are sometimes approximated, the fraction created may seem strange in some cases. For example, calling new BigFraction(1.0 / 3.0) does not create the fraction 1/3, but the fraction 6004799503160661 / 18014398509481984 because the double number passed to the constructor is not exactly 1/3 (this number cannot be stored exactly in IEEE754).

Parameters
value:double

the double value to convert to a fraction.

Exceptions
MathIllegalArgumentException:
if value is NaN or infinite
See Also
BigFraction(double, double, int)
BigFractionback to summary
public BigFraction(final double value, final double epsilon, final int maxIterations) throws FractionConversionException

Create a fraction given the double value and maximum error allowed.

References:

Parameters
value:double

the double value to convert to a fraction.

epsilon:double

maximum error allowed. The resulting fraction is within epsilon of value, in absolute terms.

maxIterations:int

maximum number of convergents.

Exceptions
FractionConversionException:
if the continued fraction failed to converge.
See Also
BigFraction(double)
BigFractionback to summary
private BigFraction(final double value, final double epsilon, final int maxDenominator, int maxIterations) throws FractionConversionException

Create a fraction given the double value and either the maximum error allowed or the maximum number of denominator digits.

Note

This constructor is called with EITHER - a valid epsilon value and the maxDenominator set to Integer.MAX_VALUE (that way the maxDenominator has no effect). OR - a valid maxDenominator value and the epsilon value set to zero (that way epsilon only has effect if there is an exact match before the maxDenominator value is reached).

It has been done this way so that the same code can be (re)used for both scenarios. However this could be confusing to users if it were part of the public API and this constructor should therefore remain PRIVATE.

See JIRA issue ticket MATH-181 for more details: https://issues.apache.org/jira/browse/MATH-181
Parameters
value:double

the double value to convert to a fraction.

epsilon:double

maximum error allowed. The resulting fraction is within epsilon of value, in absolute terms.

maxDenominator:int

maximum denominator value allowed.

maxIterations:int

maximum number of convergents.

Exceptions
FractionConversionException:
if the continued fraction failed to converge.
BigFractionback to summary
public BigFraction(final double value, final int maxDenominator) throws FractionConversionException

Create a fraction given the double value and maximum denominator.

References:

Parameters
value:double

the double value to convert to a fraction.

maxDenominator:int

The maximum allowed value for denominator.

Exceptions
FractionConversionException:
if the continued fraction failed to converge.
BigFractionback to summary
public BigFraction(final int num)

Create a BigFraction equivalent to the passed int, ie "num / 1".

Parameters
num:int

the numerator.

BigFractionback to summary
public BigFraction(final int num, final int den)

Create a BigFraction given the numerator and denominator as simple int. The BigFraction is reduced to lowest terms.

Parameters
num:int

the numerator.

den:int

the denominator.

BigFractionback to summary
public BigFraction(final long num)

Create a BigFraction equivalent to the passed long, ie "num / 1".

Parameters
num:long

the numerator.

BigFractionback to summary
public BigFraction(final long num, final long den)

Create a BigFraction given the numerator and denominator as simple long. The BigFraction is reduced to lowest terms.

Parameters
num:long

the numerator.

den:long

the denominator.

Method Detail

absback to summary
public BigFraction abs()

Returns the absolute value of this BigFraction.

Returns:BigFraction

the absolute value as a BigFraction.

addback to summary
public BigFraction add(final BigInteger bg) throws NullArgumentException

Implements org.apache.commons.math3.FieldElement.add.

Adds the value of this fraction to the passed BigInteger, returning the result in reduced form.

Parameters
bg:BigInteger

the BigInteger to add, must'nt be null.

Returns:BigFraction

a BigFraction instance with the resulting values.

Exceptions
NullArgumentException:
if the BigInteger is null.
addback to summary
public BigFraction add(final int i)

Implements org.apache.commons.math3.FieldElement.add.

Adds the value of this fraction to the passed integer, returning the result in reduced form.

Parameters
i:int

the integer to add.

Returns:BigFraction

a BigFraction instance with the resulting values.

addback to summary
public BigFraction add(final long l)

Implements org.apache.commons.math3.FieldElement.add.

Adds the value of this fraction to the passed long, returning the result in reduced form.

Parameters
l:long

the long to add.

Returns:BigFraction

a BigFraction instance with the resulting values.

addback to summary
public BigFraction add(final BigFraction fraction)

Implements org.apache.commons.math3.FieldElement.add.

Adds the value of this fraction to another, returning the result in reduced form.

Parameters
fraction:BigFraction

the BigFraction to add, must not be null.

Returns:BigFraction

a BigFraction instance with the resulting values.

Exceptions
NullArgumentException:
if the BigFraction is null.
bigDecimalValueback to summary
public BigDecimal bigDecimalValue()

Gets the fraction as a BigDecimal. This calculates the fraction as the numerator divided by denominator.

Returns:BigDecimal

the fraction as a BigDecimal.

Exceptions
ArithmeticException:
if the exact quotient does not have a terminating decimal expansion.
See Also
BigDecimal
bigDecimalValueback to summary
public BigDecimal bigDecimalValue(final int roundingMode)

Gets the fraction as a BigDecimal following the passed rounding mode. This calculates the fraction as the numerator divided by denominator.

Parameters
roundingMode:int

rounding mode to apply. see BigDecimal constants.

Returns:BigDecimal

the fraction as a BigDecimal.

Exceptions
IllegalArgumentException:
if roundingMode does not represent a valid rounding mode.
See Also
BigDecimal
bigDecimalValueback to summary
public BigDecimal bigDecimalValue(final int scale, final int roundingMode)

Gets the fraction as a BigDecimal following the passed scale and rounding mode. This calculates the fraction as the numerator divided by denominator.

Parameters
scale:int

scale of the BigDecimal quotient to be returned. see BigDecimal for more information.

roundingMode:int

rounding mode to apply. see BigDecimal constants.

Returns:BigDecimal

the fraction as a BigDecimal.

See Also
BigDecimal
compareToback to summary
public int compareTo(final BigFraction object)

Implements java.lang.Comparable.compareTo.

Compares this object to another based on size.

Parameters
object:BigFraction

the object to compare to, must not be null.

Returns:int

-1 if this is less than object, +1 if this is greater than object, 0 if they are equal.

See Also
java.lang.Comparable#compareTo(java.lang.Object)
divideback to summary
public BigFraction divide(final BigInteger bg)

Implements org.apache.commons.math3.FieldElement.divide.

Divide the value of this fraction by the passed BigInteger, ie this * 1 / bg, returning the result in reduced form.

Parameters
bg:BigInteger

the BigInteger to divide by, must not be null

Returns:BigFraction

a BigFraction instance with the resulting values

Exceptions
NullArgumentException:
if the BigInteger is null
MathArithmeticException:
if the fraction to divide by is zero
divideback to summary
public BigFraction divide(final int i)

Implements org.apache.commons.math3.FieldElement.divide.

Divide the value of this fraction by the passed int, ie this * 1 / i, returning the result in reduced form.

Parameters
i:int

the int to divide by

Returns:BigFraction

a BigFraction instance with the resulting values

Exceptions
MathArithmeticException:
if the fraction to divide by is zero
divideback to summary
public BigFraction divide(final long l)

Implements org.apache.commons.math3.FieldElement.divide.

Divide the value of this fraction by the passed long, ie this * 1 / l, returning the result in reduced form.

Parameters
l:long

the long to divide by

Returns:BigFraction

a BigFraction instance with the resulting values

Exceptions
MathArithmeticException:
if the fraction to divide by is zero
divideback to summary
public BigFraction divide(final BigFraction fraction)

Implements org.apache.commons.math3.FieldElement.divide.

Divide the value of this fraction by another, returning the result in reduced form.

Parameters
fraction:BigFraction

Fraction to divide by, must not be null.

Returns:BigFraction

a BigFraction instance with the resulting values.

Exceptions
NullArgumentException:
if the fraction is null.
MathArithmeticException:
if the fraction to divide by is zero
doubleValueback to summary
public double doubleValue()

Implements abstract java.lang.Number.doubleValue.

Gets the fraction as a double. This calculates the fraction as the numerator divided by denominator.

Returns:double

the fraction as a double

Annotations
@Override
See Also
java.lang.Number#doubleValue()
equalsback to summary
public boolean equals(final Object other)

Overrides java.lang.Object.equals.

Test for the equality of two fractions. If the lowest term numerator and denominators are the same for both fractions, the two fractions are considered to be equal.

Parameters
other:Object

fraction to test for equality to this fraction, can be null.

Returns:boolean

true if two fractions are equal, false if object is null, not an instance of BigFraction, or not equal to this fraction instance.

Annotations
@Override
See Also
java.lang.Object#equals(java.lang.Object)
floatValueback to summary
public float floatValue()

Implements abstract java.lang.Number.floatValue.

Gets the fraction as a float. This calculates the fraction as the numerator divided by denominator.

Returns:float

the fraction as a float.

Annotations
@Override
See Also
java.lang.Number#floatValue()
getDenominatorback to summary
public BigInteger getDenominator()

Access the denominator as a BigInteger.

Returns:BigInteger

the denominator as a BigInteger.

getDenominatorAsIntback to summary
public int getDenominatorAsInt()

Access the denominator as a int.

Returns:int

the denominator as a int.

getDenominatorAsLongback to summary
public long getDenominatorAsLong()

Access the denominator as a long.

Returns:long

the denominator as a long.

getFieldback to summary
public BigFractionField getField()

Implements org.apache.commons.math3.FieldElement.getField.

Doc from org.apache.commons.math3.FieldElement.getField.

Get the Field to which the instance belongs.

Returns:BigFractionField

Field to which the instance belongs

getNumeratorback to summary
public BigInteger getNumerator()

Access the numerator as a BigInteger.

Returns:BigInteger

the numerator as a BigInteger.

getNumeratorAsIntback to summary
public int getNumeratorAsInt()

Access the numerator as a int.

Returns:int

the numerator as a int.

getNumeratorAsLongback to summary
public long getNumeratorAsLong()

Access the numerator as a long.

Returns:long

the numerator as a long.

getReducedFractionback to summary
public static BigFraction getReducedFraction(final int numerator, final int denominator)

Creates a BigFraction instance with the 2 parts of a fraction Y/Z.

Any negative signs are resolved to be on the numerator.

Parameters
numerator:int

the numerator, for example the three in 'three sevenths'.

denominator:int

the denominator, for example the seven in 'three sevenths'.

Returns:BigFraction

a new fraction instance, with the numerator and denominator reduced.

Exceptions
ArithmeticException:
if the denominator is zero.
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Gets a hashCode for the fraction.

Returns:int

a hash code value for this object.

Annotations
@Override
See Also
java.lang.Object#hashCode()
intValueback to summary
public int intValue()

Implements abstract java.lang.Number.intValue.

Gets the fraction as an int. This returns the whole number part of the fraction.

Returns:int

the whole number fraction part.

Annotations
@Override
See Also
java.lang.Number#intValue()
longValueback to summary
public long longValue()

Implements abstract java.lang.Number.longValue.

Gets the fraction as a long. This returns the whole number part of the fraction.

Returns:long

the whole number fraction part.

Annotations
@Override
See Also
java.lang.Number#longValue()
multiplyback to summary
public BigFraction multiply(final BigInteger bg)

Implements org.apache.commons.math3.FieldElement.multiply.

Multiplies the value of this fraction by the passed BigInteger, returning the result in reduced form.

Parameters
bg:BigInteger

the BigInteger to multiply by.

Returns:BigFraction

a BigFraction instance with the resulting values.

Exceptions
NullArgumentException:
if bg is null.
multiplyback to summary
public BigFraction multiply(final int i)

Implements org.apache.commons.math3.FieldElement.multiply, org.apache.commons.math3.FieldElement.multiply.

Multiply the value of this fraction by the passed int, returning the result in reduced form.

Parameters
i:int

the int to multiply by.

Returns:BigFraction

a BigFraction instance with the resulting values.

multiplyback to summary
public BigFraction multiply(final long l)

Implements org.apache.commons.math3.FieldElement.multiply.

Multiply the value of this fraction by the passed long, returning the result in reduced form.

Parameters
l:long

the long to multiply by.

Returns:BigFraction

a BigFraction instance with the resulting values.

multiplyback to summary
public BigFraction multiply(final BigFraction fraction)

Implements org.apache.commons.math3.FieldElement.multiply.

Multiplies the value of this fraction by another, returning the result in reduced form.

Parameters
fraction:BigFraction

Fraction to multiply by, must not be null.

Returns:BigFraction

a BigFraction instance with the resulting values.

Exceptions
NullArgumentException:
if fraction is null.
negateback to summary
public BigFraction negate()

Implements org.apache.commons.math3.FieldElement.negate.

Return the additive inverse of this fraction, returning the result in reduced form.

Returns:BigFraction

the negation of this fraction.

percentageValueback to summary
public double percentageValue()

Gets the fraction percentage as a double. This calculates the fraction as the numerator divided by denominator multiplied by 100.

Returns:double

the fraction percentage as a double.

powback to summary
public BigFraction pow(final int exponent)

Returns a BigFraction whose value is (this<sup>exponent</sup>), returning the result in reduced form.

Parameters
exponent:int

exponent to which this BigFraction is to be raised.

Returns:BigFraction

thisexponent.

powback to summary
public BigFraction pow(final long exponent)

Returns a BigFraction whose value is (thisexponent), returning the result in reduced form.

Parameters
exponent:long

exponent to which this BigFraction is to be raised.

Returns:BigFraction

thisexponent as a BigFraction.

powback to summary
public BigFraction pow(final BigInteger exponent)

Returns a BigFraction whose value is (thisexponent), returning the result in reduced form.

Parameters
exponent:BigInteger

exponent to which this BigFraction is to be raised.

Returns:BigFraction

thisexponent as a BigFraction.

powback to summary
public double pow(final double exponent)

Returns a double whose value is (thisexponent), returning the result in reduced form.

Parameters
exponent:double

exponent to which this BigFraction is to be raised.

Returns:double

thisexponent.

reciprocalback to summary
public BigFraction reciprocal()

Implements org.apache.commons.math3.FieldElement.reciprocal.

Return the multiplicative inverse of this fraction.

Returns:BigFraction

the reciprocal fraction.

reduceback to summary
public BigFraction reduce()

Reduce this BigFraction to its lowest terms.

Returns:BigFraction

the reduced BigFraction. It doesn't change anything if the fraction can be reduced.

subtractback to summary
public BigFraction subtract(final BigInteger bg)

Implements org.apache.commons.math3.FieldElement.subtract.

Subtracts the value of an BigInteger from the value of this BigFraction, returning the result in reduced form.

Parameters
bg:BigInteger

the BigInteger to subtract, cannot be null.

Returns:BigFraction

a BigFraction instance with the resulting values.

Exceptions
NullArgumentException:
if the BigInteger is null.
subtractback to summary
public BigFraction subtract(final int i)

Implements org.apache.commons.math3.FieldElement.subtract.

Subtracts the value of an integer from the value of this BigFraction, returning the result in reduced form.

Parameters
i:int

the integer to subtract.

Returns:BigFraction

a BigFraction instance with the resulting values.

subtractback to summary
public BigFraction subtract(final long l)

Implements org.apache.commons.math3.FieldElement.subtract.

Subtracts the value of a long from the value of this BigFraction, returning the result in reduced form.

Parameters
l:long

the long to subtract.

Returns:BigFraction

a BigFraction instance with the resulting values.

subtractback to summary
public BigFraction subtract(final BigFraction fraction)

Implements org.apache.commons.math3.FieldElement.subtract.

Subtracts the value of another fraction from the value of this one, returning the result in reduced form.

Parameters
fraction:BigFraction

BigFraction to subtract, must not be null.

Returns:BigFraction

a BigFraction instance with the resulting values

Exceptions
NullArgumentException:
if the fraction is null.
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the String representing this fraction, ie "num / dem" or just "num" if the denominator is one.

Returns:String

a string representation of the fraction.

Annotations
@Override
See Also
java.lang.Object#toString()