public final class Fraction extends Number implements Comparable<Fraction>, NativeOperators<Fraction>, Serializable
Modifier and Type | Field and Description |
---|---|
static Fraction |
ONE
A fraction representing "1".
|
static Fraction |
ZERO
A fraction representing "0".
|
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)
Adds an integer to the fraction.
|
int |
compareTo(Fraction other)
Compares this object to another based on size.
|
Fraction |
divide(Fraction fraction)
Divides the value of this fraction by another.
|
Fraction |
divide(int i)
Divides the fraction by an integer.
|
double |
doubleValue()
Retrieves the
double value closest to this fraction. |
boolean |
equals(Object other)
Test for the equality of two fractions.
|
float |
floatValue()
Retrieves the
float value closest to this fraction. |
static Fraction |
from(double value)
Creates an instance.
|
static Fraction |
from(double value,
double epsilon,
int maxIterations)
Create a fraction given the double value and maximum error allowed.
|
static Fraction |
from(double value,
int maxDenominator)
Creates an instance.
|
int |
getDenominator() |
int |
getNumerator() |
int |
hashCode() |
int |
intValue()
Retrieves the whole number part of the fraction.
|
long |
longValue()
Retrieves the whole number part of the fraction.
|
Fraction |
multiply(Fraction fraction)
Multiplies the value of this fraction by another, returning the
result in reduced form.
|
Fraction |
multiply(int i)
Multiplies the fraction by an integer.
|
Fraction |
negate()
Computes the additive inverse of this fraction.
|
static Fraction |
of(int num)
Creates an instance.
|
static Fraction |
of(int num,
int den)
Return a fraction given the numerator and denominator.
|
Fraction |
one() |
static Fraction |
parse(String s)
Parses a string that would be produced by
toString()
and instantiates the corresponding object. |
Fraction |
pow(int n) |
Fraction |
reciprocal()
Computes the multiplicative inverse of this fraction.
|
int |
signum()
Retrieves the sign 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)
Subtracts an integer from this fraction.
|
String |
toString() |
Fraction |
zero() |
byteValue, shortValue
public static final Fraction ONE
public static final Fraction ZERO
public static Fraction from(double value)
value
- Value to convert to a fraction.ArithmeticException
- if the continued fraction failed to
converge.public static Fraction from(double value, double epsilon, int maxIterations)
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 convergentsArithmeticException
- if the continued fraction failed to
converge.public static Fraction from(double value, int maxDenominator)
References:
value
- the double value to convert to a fraction.maxDenominator
- The maximum allowed value for denominatorArithmeticException
- if the continued fraction failed to
converge.public static Fraction of(int num)
num / 1
.num
- Numerator.public static Fraction of(int num, int den)
num
- Numerator.den
- Denominator.ArithmeticException
- if the denominator is zero
or if integer overflow occurs.public Fraction abs()
public int compareTo(Fraction other)
compareTo
in interface Comparable<Fraction>
other
- Object to compare to.object
, +1 if this is greater
than object
, 0 if they are equal.public double doubleValue()
double
value closest to this fraction.
This calculates the fraction as numerator divided by denominator.doubleValue
in class Number
double
.public boolean equals(Object other)
public float floatValue()
float
value closest to this fraction.
This calculates the fraction as numerator divided by denominator.floatValue
in class Number
float
.public int getDenominator()
public int getNumerator()
public int intValue()
public long longValue()
public int signum()
public Fraction negate()
public Fraction reciprocal()
reciprocal
in interface Multiplication<Fraction>
public Fraction add(Fraction fraction)
add
in interface Addition<Fraction>
fraction
- Fraction to add.ArithmeticException
- if the resulting numerator or denominator
cannot be represented in an int
.public Fraction add(int i)
i
- Value to add.this + i
.public Fraction subtract(Fraction fraction)
subtract
in interface NativeOperators<Fraction>
fraction
- Fraction to subtract.ArithmeticException
- if the resulting numerator or denominator
cannot be represented in an int
.public Fraction subtract(int i)
i
- Value to subtract.this - i
.public Fraction multiply(Fraction fraction)
multiply
in interface Multiplication<Fraction>
fraction
- Fraction to multiply by.ArithmeticException
- if the resulting numerator or denominator
cannot be represented in an int
.public Fraction multiply(int i)
multiply
in interface NativeOperators<Fraction>
i
- Value to multiply by.this * i
.public Fraction divide(Fraction fraction)
divide
in interface NativeOperators<Fraction>
fraction
- Fraction to divide by.ArithmeticException
- if the fraction to divide by is zero
or if the resulting numerator or denominator cannot be represented
by an int
.public Fraction divide(int i)
i
- Value to divide by.this * i
.public Fraction pow(int n)
pow
in interface NativeOperators<Fraction>
n
- Power.thisn
.public Fraction one()
one
in interface Multiplication<Fraction>
public static Fraction parse(String s)
toString()
and instantiates the corresponding object.s
- String representation.NumberFormatException
- if the string does not conform to the
specification.Copyright © 2017–2020 The Apache Software Foundation. All rights reserved.