Package maths
Class BigFraction
- java.lang.Object
-
- maths.BigFraction
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable,java.lang.Comparable
public class BigFraction extends java.lang.Object implements java.lang.Cloneable, java.lang.Comparable, java.io.SerializableRepresents a fraction with arbitrary precision using BigInteger.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected java.math.BigIntegerdenominator_protected java.math.BigIntegernumerator_static BigFractionONEstatic BigFractionZERO
-
Constructor Summary
Constructors Constructor Description BigFraction(long num, long den)Constructs a BigFraction with the specified long numerator and denominator.BigFraction(java.lang.String s)Constructs a BigFraction from a string representation.BigFraction(java.math.BigInteger b)Constructs a BigFraction with the specified BigInteger numerator.BigFraction(java.math.BigInteger num, java.math.BigInteger den)Constructs a BigFraction with the specified numerator and denominator.BigFraction(BigFraction f)Copy constructor for BigFraction.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BigFractionadd(long n)Adds the specified long value to this fraction.BigFractionadd(java.math.BigInteger n)Adds the specified BigInteger to this fraction.BigFractionadd(BigFraction b)Adds the specified fraction to this fraction.java.lang.Objectclone()Creates and returns a copy of this BigFraction.intcompareTo(java.lang.Object other)Compares this fraction with another object.intcompareTo(java.math.BigInteger n)Compares this fraction with a BigInteger.java.math.BigIntegerdenominator()Returns the denominator as a BigInteger.BigFractiondivide(java.math.BigInteger n)Divides this fraction by the specified BigInteger.BigFractiondivide(BigFraction b)Divides this fraction by the specified fraction.booleanequals(long n)Checks if this fraction is equal to a long value.booleanequals(java.lang.Object other)Checks if this fraction is equal to another object.booleanequals(java.math.BigInteger n)Checks if this fraction is equal to a BigInteger.inthashCode()Returns a hash code for this fraction.booleanisZero()Checks if the numerator is zero.BigFractionmax(BigFraction val)Returns the maximum of this fraction and the specified fraction.BigFractionmin(BigFraction val)Returns the minimum of this fraction and the specified fraction.BigFractionmultiply(java.math.BigInteger n)Multiplies this fraction by the specified BigInteger.BigFractionmultiply(BigFraction b)Multiplies this fraction by the specified fraction.java.math.BigIntegernumerator()Returns the numerator as a BigInteger.BigFractionpow(int d)Raises this fraction to the power of the specified integer.BigFractionsqrt()Calculates the square root of this fraction if it is non-negative.java.lang.StringtoString()Returns a string representation of the fraction.
-
-
-
Field Detail
-
numerator_
protected final java.math.BigInteger numerator_
-
denominator_
protected final java.math.BigInteger denominator_
-
ZERO
public static final BigFraction ZERO
-
ONE
public static final BigFraction ONE
-
-
Constructor Detail
-
BigFraction
public BigFraction(java.math.BigInteger num, java.math.BigInteger den)Constructs a BigFraction with the specified numerator and denominator.- Parameters:
num- the numeratorden- the denominator
-
BigFraction
public BigFraction(java.math.BigInteger b)
Constructs a BigFraction with the specified BigInteger numerator.- Parameters:
b- the numerator
-
BigFraction
public BigFraction(BigFraction f)
Copy constructor for BigFraction.- Parameters:
f- the BigFraction to copy
-
BigFraction
public BigFraction(java.lang.String s)
Constructs a BigFraction from a string representation.- Parameters:
s- the string representation of the fraction
-
BigFraction
public BigFraction(long num, long den)Constructs a BigFraction with the specified long numerator and denominator.- Parameters:
num- the numeratorden- the denominator
-
-
Method Detail
-
numerator
public java.math.BigInteger numerator()
Returns the numerator as a BigInteger.- Returns:
- the numerator as a BigInteger
-
denominator
public java.math.BigInteger denominator()
Returns the denominator as a BigInteger.- Returns:
- the denominator as a BigInteger
-
toString
public java.lang.String toString()
Returns a string representation of the fraction.- Overrides:
toStringin classjava.lang.Object- Returns:
- the string representation of the fraction
-
clone
public java.lang.Object clone()
Creates and returns a copy of this BigFraction.- Overrides:
clonein classjava.lang.Object- Returns:
- a clone of this BigFraction
-
min
public BigFraction min(BigFraction val)
Returns the minimum of this fraction and the specified fraction.- Parameters:
val- the fraction to compare with- Returns:
- the minimum fraction
-
max
public BigFraction max(BigFraction val)
Returns the maximum of this fraction and the specified fraction.- Parameters:
val- the fraction to compare with- Returns:
- the maximum fraction
-
pow
public BigFraction pow(int d)
Raises this fraction to the power of the specified integer.- Parameters:
d- the exponent- Returns:
- the resulting fraction
-
add
public BigFraction add(BigFraction b)
Adds the specified fraction to this fraction.- Parameters:
b- the fraction to add- Returns:
- the resulting fraction
-
add
public BigFraction add(java.math.BigInteger n)
Adds the specified BigInteger to this fraction.- Parameters:
n- the BigInteger to add- Returns:
- the resulting fraction
-
add
public BigFraction add(long n)
Adds the specified long value to this fraction.- Parameters:
n- the long value to add- Returns:
- the resulting fraction
-
multiply
public BigFraction multiply(BigFraction b)
Multiplies this fraction by the specified fraction.- Parameters:
b- the fraction to multiply by- Returns:
- the resulting fraction
-
multiply
public BigFraction multiply(java.math.BigInteger n)
Multiplies this fraction by the specified BigInteger.- Parameters:
n- the BigInteger to multiply by- Returns:
- the resulting fraction
-
divide
public BigFraction divide(BigFraction b)
Divides this fraction by the specified fraction.- Parameters:
b- the fraction to divide by- Returns:
- the resulting fraction
-
divide
public BigFraction divide(java.math.BigInteger n)
Divides this fraction by the specified BigInteger.- Parameters:
n- the BigInteger to divide by- Returns:
- the resulting fraction
-
sqrt
public BigFraction sqrt()
Calculates the square root of this fraction if it is non-negative.- Returns:
- the square root as a BigFraction, or null if not a perfect square
-
isZero
public boolean isZero()
Checks if the numerator is zero.- Returns:
- true if the numerator is zero; false otherwise
-
compareTo
public int compareTo(java.lang.Object other)
Compares this fraction with another object.- Specified by:
compareToin interfacejava.lang.Comparable- Parameters:
other- the object to compare with- Returns:
- -1, 0, or 1 as this fraction is less than, equal to, or greater than the given object
-
compareTo
public int compareTo(java.math.BigInteger n)
Compares this fraction with a BigInteger.- Parameters:
n- the BigInteger to compare with- Returns:
- -1, 0, or 1 as this fraction is less than, equal to, or greater than the given BigInteger
-
equals
public boolean equals(java.lang.Object other)
Checks if this fraction is equal to another object.- Overrides:
equalsin classjava.lang.Object- Parameters:
other- the object to compare with- Returns:
- true if the fractions are equal; false otherwise
-
equals
public boolean equals(java.math.BigInteger n)
Checks if this fraction is equal to a BigInteger.- Parameters:
n- the BigInteger to compare with- Returns:
- true if equal; false otherwise
-
equals
public boolean equals(long n)
Checks if this fraction is equal to a long value.- Parameters:
n- the long value to compare with- Returns:
- true if equal; false otherwise
-
hashCode
public int hashCode()
Returns a hash code for this fraction.- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hash code computed from numerator and denominator
-
-