|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.lang.Number
ch.javasoft.math.BigFraction
public class BigFraction
The BigFraction is a fractional with numerator and
denominator both being BigInteger numbers.
Big integer fractionals are constant, they don't change after instantiation.
| Field Summary | |
|---|---|
static BigFraction |
ONE
The BigFraction constant one. |
static BigFraction |
TEN
The BigFraction constant ten. |
static BigFraction |
TWO
The BigFraction constant two. |
static BigFraction |
ZERO
The BigFraction constant zero. |
| Constructor Summary | |
|---|---|
BigFraction(BigInteger numerator,
BigInteger denominator)
Constructor with numerator and denominator. |
|
BigFraction(long numerator,
long denominator)
Constructor with numerator and denominator. |
|
BigFraction(String s)
It is recommended to use valueOf(String) instead. |
|
| Method Summary | |
|---|---|
BigFraction |
abs()
Returns a BigFraction whose value is the absolute value of this BigFraction. |
BigFraction |
add(BigFraction val)
Returns a BigFraction whose value is (this + val). |
int |
compareTo(BigFraction other)
Compares this BigFraction with the specified BigInteger. |
BigFraction |
divide(BigFraction by)
Returns a BigFraction whose value is (this / val) = (this.numerator * by.denominator) / (this.denominator * by.numerator) . |
double |
doubleValue()
|
boolean |
equals(Object obj)
Returns true if and only if the numerator and denominator of the two objects are equal. |
boolean |
equalsNumerically(BigFraction other)
Returns true iff this big integer fraction number is numerically equal to other. |
float |
floatValue()
|
static BigFraction |
gcd(BigFraction... values)
Calculates the greatest common divisor of the specified fraction numbers. |
BigFraction |
gcd(BigFraction val)
Calculates the greatest common divisor of this fraction number and val. |
BigInteger |
getDenominator()
Returns the denominator |
BigInteger |
getNumerator()
Returns the enumerator |
int |
hashCode()
Returns the hash code for this BigFraction. |
int |
intValue()
Converts this BigFraction to an int. |
BigFraction |
invert()
Returns a BigFraction whose value is (1/this). |
boolean |
isInteger()
Returns true if this number represents an integer, that is, if remainder of numerator/denominator equals zero. |
boolean |
isNegative()
Convenience method to check whether this big integer fraction number is negative, say this < 0 |
boolean |
isNonNegative()
Convenience method to check whether this big integer fraction number is non-negative, say this >= 0 |
boolean |
isNonPositive()
Convenience method to check whether this big integer fraction number is non-positive, say this <= 0 |
boolean |
isNonZero()
Convenience method to check whether this big integer fraction number is numerically not equal to zero, say this != 0 |
boolean |
isOne()
Convenience method to check whether this big integer fraction number is numerically equal to one. |
boolean |
isPositive()
Convenience method to check whether this big integer fraction number is positive, say this > 0 |
boolean |
isReduced()
Returns true if this fraction is already reduced, i.e. |
boolean |
isZero()
Convenience method to check whether this big integer fraction number is numerically equal to zero, say this == 0 |
long |
longValue()
Converts this BigFraction to a long. |
BigFraction |
max(BigFraction with)
Returns the maximum of this BigFraction and val. |
BigFraction |
min(BigFraction with)
Returns the minimum of this BigFraction and val. |
BigFraction |
multiply(BigFraction val)
Returns a BigFraction whose value is (this * val). |
BigFraction |
negate()
Returns a BigFraction whose value is (-this). |
BigFraction |
pow(int exponent)
Returns a BigFraction whose value is (thisexponent) == (this.nominatorexponent / this.denominatorexponent). |
BigFraction |
reduce()
Divides denominator and numerator by their greatest common divisor (gcd). |
BigFraction |
shiftLeft(int n)
Returns a BigInteger whose value is (this << n). |
BigFraction |
shiftRight(int n)
Returns a BigFraction whose value is (this >> n). |
int |
signum()
Returns the signum function of this BigFraction. |
BigFraction |
subtract(BigFraction val)
Returns a BigFraction whose value is (this - val). |
BigDecimal |
toBigDecimal()
Returns a BigDecimal whose value is (numerator / denominator), and whose preferred scale is 0; if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown. |
BigDecimal |
toBigDecimal(int scale,
RoundingMode roundingMode)
Returns a BigDecimal whose value is (numerator / divisor), and whose scale is as specified. |
BigDecimal |
toBigDecimal(MathContext mc)
Returns a BigDecimal whose value is (numerator / divisor), with rounding according to the context settings. |
BigInteger |
toBigInteger()
Returns a BigInteger whose value is (numerator / denominator), using integer division (no rounding). |
BigInteger |
toBigInteger(RoundingMode roundingMode)
Returns a BigInteger whose value is (numerator / denominator), using decimal division. |
static double |
toDouble(BigInteger numerator,
BigInteger denominator)
Converts big integer numerator/denominator arguments to a double value |
static float |
toFloat(BigInteger numerator,
BigInteger denominator)
Converts big integer numerator/denominator arguments to a float value |
String |
toString()
Returns a string representation of this BigFraction. |
static BigFraction |
valueOf(BigDecimal val)
Translates a BigDecimal to a BigFraction |
static BigFraction |
valueOf(BigInteger val)
Translates a BigInteger to a BigFraction |
static BigFraction |
valueOf(BigInteger numerator,
BigInteger denominator)
Returns a BigFraction from big integer numerator and denominator. |
static BigFraction |
valueOf(double val)
Translates a double to a BigFraction |
static BigFraction |
valueOf(long val)
Translates a long to a BigFraction |
static BigFraction |
valueOf(long numerator,
long denominator)
Returns a BigFraction from long numerator and denominator. |
static BigFraction |
valueOf(Number val)
Translates any number into a big integer fractional number. |
static BigFraction |
valueOf(String s)
Translates string representation of a fractional number into a BigFraction instance. |
static BigFraction |
valueOfAdjusted(double val)
Translates a double to a BigFraction, but tries to adjust the value to a nice fractional number, with numerator/denominator as small as possible. |
| Methods inherited from class java.lang.Number |
|---|
byteValue, shortValue |
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final BigFraction ZERO
public static final BigFraction ONE
public static final BigFraction TWO
public static final BigFraction TEN
| Constructor Detail |
|---|
public BigFraction(long numerator,
long denominator)
numerator - numerator of the fraction numberdenominator - denominator of the fraction number
public BigFraction(BigInteger numerator,
BigInteger denominator)
numerator - numerator of the fraction numberdenominator - denominator of the fraction numberpublic BigFraction(String s)
valueOf(String) instead. This method
is a convenient method mainly for reflection use by frameworks.
s - the string to parse| Method Detail |
|---|
public BigInteger getNumerator()
public BigInteger getDenominator()
public double doubleValue()
doubleValue in class Numberpublic float floatValue()
floatValue in class Numberpublic int intValue()
int. This
conversion is analogous to a narrowing
primitive conversion from long to
int as defined in the Java Language
Specification: if this BigFraction is too big to fit in an
int, only the low-order 32 bits are returned.
Note that this conversion can lose information about the
overall magnitude of the BigFraction value as well as return a
result with the opposite sign.
intValue in class Numberint.public long longValue()
long. This
conversion is analogous to a narrowing
primitive conversion from long to
int as defined in the Java Language
Specification: if this BigFraction is too big to fit in a
long, only the low-order 64 bits are returned.
Note that this conversion can lose information about the
overall magnitude of the BigFraction value as well as return a
result with the opposite sign.
longValue in class Numberlong.public BigInteger toBigInteger()
public BigInteger toBigInteger(RoundingMode roundingMode)
roundingMode - Mode rounding mode to apply
public BigDecimal toBigDecimal()
ArithmeticException - if the exact quotient does not have a
terminating decimal expansionpublic BigDecimal toBigDecimal(MathContext mc)
mc - the context to use.
ArithmeticException - if the result is inexact but the
rounding mode is UNNECESSARY or
mc.precision == 0 and the quotient has a
non-terminating decimal expansion.
public BigDecimal toBigDecimal(int scale,
RoundingMode roundingMode)
scale - scale of the BigDecimal quotient to be returned.roundingMode - rounding mode to apply.
ArithmeticException - if divisor is zero,
roundingMode==RoundingMode.UNNECESSARY and
the specified scale is insufficient to represent the result
of the division exactly.public int hashCode()
hashCode in class Objectpublic boolean equals(Object obj)
equalsNumerically(BigFraction) or
compareTo(BigFraction)
equals in class Objectpublic int compareTo(BigFraction other)
compareTo in interface Comparable<BigFraction>other - fractional number to which this fraction is to be compared.
public int signum()
public BigFraction abs()
reduced, that is,
gcd(nominator, denominator) might be != 1.
public BigFraction negate()
reduced, that is,
gcd(nominator, denominator) might be != 1.
public BigFraction invert()
reduced, that is,
gcd(nominator, denominator) might be != 1.
The resulting denominator is guaranteed to be non-negative.
public BigFraction add(BigFraction val)
reduced, that is,
gcd(nominator, denominator) might be != 1.
val - value to be added to this BigFraction.
public BigFraction subtract(BigFraction val)
reduced, that is,
gcd(nominator, denominator) might be != 1.
val - value to be subtracted from this BigFraction.
public BigFraction multiply(BigFraction val)
reduced, that is,
gcd(nominator, denominator) might be != 1.
val - value to be multiplied by this BigFraction.
public BigFraction divide(BigFraction by)
reduced, that is,
gcd(nominator, denominator) might be != 1.
by - value by which this BigFraction is to be divided.
ArithmeticException - val==0public BigFraction pow(int exponent)
reduced, that is,
gcd(nominator, denominator) might be != 1.
exponent - exponent to which this BigFraction is to be
raised.
ArithmeticException - exponent is negative and
this is zero. (This would cause a division by zero.)public BigFraction shiftLeft(int n)
To achieve that the resulting fraction number is smallest possible, the denominator is shifted to the right as far as possible. The remaining positions are left shifts of the numerator.
n - shift distance, in bits.
shiftRight(int)public BigFraction shiftRight(int n)
To achieve that the resulting fraction number is smallest possible, the numerator is shifted to the right as far as possible. The remaining positions are left shifts of the denominator.
n - shift distance, in bits.
shiftLeft(int)public BigFraction max(BigFraction with)
with - value with which the maximum is to be computed.
public BigFraction min(BigFraction with)
with - value with which the minimum is to be computed.
public BigFraction reduce()
isReduced()public BigFraction gcd(BigFraction val)
val. The gcd of two fraction numbers is gcd of numerators
divided by gcd of deniminators.
If both numbers are negative, the resulting gcd is also negative. If both numbers are zero, the result is zero. Otherwise, the result is positive.
val - value with which the gcd is to be computed
public static BigFraction gcd(BigFraction... values)
This method might be useful to scale down a vector of fraction numbers.
If all numbers are negative, the resulting gcd is also negative. If all numbers are zero, the result is zero. Otherwise, the result is positive.
values - values for which the gcd is to be computed
public boolean isZero()
signum(),
compareTo(BigFraction),
equalsNumerically(BigFraction)public boolean isReduced()
reduce()public boolean isNonZero()
signum(),
compareTo(BigFraction),
equalsNumerically(BigFraction)public boolean isNegative()
signum(),
compareTo(BigFraction)public boolean isNonNegative()
signum(),
compareTo(BigFraction)public boolean isPositive()
signum(),
compareTo(BigFraction)public boolean isNonPositive()
signum(),
compareTo(BigFraction)public boolean isOne()
equalsNumerically(BigFraction),
compareTo(BigFraction)public boolean isInteger()
public boolean equalsNumerically(BigFraction other)
equals(Object) only returns true
if both nominator and denominator are equal, and false otherwise, even if
both fractional numbers represent the same numeric value.
equals(Object)public String toString()
Strings returned here are parsable by valueOf(String)
toString in class Object
public static BigFraction valueOf(String s)
throws NumberFormatException
BigInteger and BigDecimal, or strings of the form
numerator/denominator with two big integers.
s - the string representation of a big integer fraction to parse
NumberFormatException - val is not a valid representation
of a BigFraction.BigInteger.BigInteger(String),
BigDecimal.BigDecimal(String)public static BigFraction valueOf(BigInteger val)
BigInteger to a BigFraction
val - the big integer to translate
public static BigFraction valueOf(BigDecimal val)
BigDecimal to a BigFraction
val - the big decimal to translate
public static BigFraction valueOf(long val)
val - the long to translate
BigDecimal.valueOf(long)public static BigFraction valueOf(double val)
val - the double to translate
BigDecimal.valueOf(double)
public static BigFraction valueOf(long numerator,
long denominator)
BigFraction(long, long), but possibly,
some caching is used for commonly used values.
numerator - the numerator for the fraction number to be createddenominator - the denominator for the fraction number to be created
BigFraction(long, long)
public static BigFraction valueOf(BigInteger numerator,
BigInteger denominator)
BigFraction(BigInteger, BigInteger),
but possibly, some caching is used for commonly used values.
numerator - the numerator for the fraction number to be createddenominator - the denominator for the fraction number to be created
BigFraction(BigInteger, BigInteger)public static BigFraction valueOf(Number val)
valueOf(String) is invoked after converting
the given number to a string value.
This method is guaranteed to work for java number types, including
BigInteger and BigDecimal.
public static BigFraction valueOfAdjusted(double val)
val - the double to translate
ArithmeticException - if the value is NaN or infinitevalueOf(double)
public static double toDouble(BigInteger numerator,
BigInteger denominator)
public static float toFloat(BigInteger numerator,
BigInteger denominator)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||