Class BigFraction
java.lang.Object
BigFraction
- All Implemented Interfaces:
java.lang.Comparable<BigFraction>
public class BigFraction extends java.lang.Object implements java.lang.Comparable<BigFraction>
This is a class for the performance of extended-precision rational
arithmetic. The objecs we create are immutable.
-
Field Summary
Fields Modifier and Type Field Description static BigFraction
HALF
This is the BigFraction 1/2.static BigFraction
ONE
This is the BigFraction 1/1.static BigFraction
ZERO
This is the BigFraction 0/1. -
Constructor Summary
Constructors Constructor Description BigFraction(java.lang.String num, java.lang.String denom)
This constructor creates the BigFraction num/denom.BigFraction(java.math.BigInteger num, java.math.BigInteger denom)
This constructor creates the BigFraction num/denom. -
Method Summary
Modifier and Type Method Description BigFraction
abs()
This computes the absolute value of a BigFraction as a BigFraction.BigFraction
add(BigFraction that)
This computes this + that.java.math.BigInteger
bigIntegerValue()
This computes the integer part of this BigFraction, truncating toward zero.int
compareTo(BigFraction that)
This method retursn a negative integer if this < that, a positive integer if this > that, and zero if this equals that.BigFraction
divide(BigFraction that)
This computes this / that.double
doubleValue()
boolean
equals(java.lang.Object o)
This tests fractions for numerical equality.static BigFraction
harmonic(int n)
static void
main(java.lang.String[] args)
BigFraction
max(BigFraction that)
BigFraction
min(BigFraction that)
BigFraction
multiply(BigFraction that)
This computes this * that.BigFraction
negate()
This returns a new BigFraction represeting -this.BigFraction
pow(int n)
This computes thisn.BigFraction
subtract(BigFraction that)
This computes this - that.java.lang.String
toString()
This makes a string representation of the form num/denom.static BigFraction
valueOf(long num, long denom)
This static factory method creates a BigFraction from two longs.
-
Field Details
-
Constructor Details
-
BigFraction
public BigFraction(java.math.BigInteger num, java.math.BigInteger denom)This constructor creates the BigFraction num/denom.- Parameters:
num
- the numerator of this BigFractiondenom
- the denominator of this BigFraction- Throws:
java.lang.IllegalArgumentException
- if a zero is passed in as a denominator
-
BigFraction
public BigFraction(java.lang.String num, java.lang.String denom)This constructor creates the BigFraction num/denom. The fraction will be represented in its fully reduced form with any negative in the numerator.- Parameters:
num
- a numerical String representing the numerator of this BigFractiondenom
- a numerical String representing the denominator of this BigFraction
-
-
Method Details
-
valueOf
This static factory method creates a BigFraction from two longs.- Parameters:
num
- the numerator of this BigFractiondenom
- the denominator of this BigFraction- Returns:
- a new BigFraction representing num/denom.
-
toString
public java.lang.String toString()This makes a string representation of the form num/denom.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string of the form num/denom.
-
equals
public boolean equals(java.lang.Object o)This tests fractions for numerical equality.- Overrides:
equals
in classjava.lang.Object
- Parameters:
o
- the object we are checking for equality with this BigFraction- Returns:
- true if o is a BigFraction and if it is numerically equal to this BigFraction.
-
add
This computes this + that.- Parameters:
that
- is a BigFraction we are adding to this BigFraction- Returns:
- this + that
-
subtract
This computes this - that.- Parameters:
that
- is a BigFraction we are adding to this BigFraction- Returns:
- this - that
-
multiply
This computes this * that.- Parameters:
that
- is a BigFraction we are adding to this BigFraction- Returns:
- this * that
-
divide
This computes this / that.- Parameters:
that
- is a BigFraction we are adding to this BigFraction- Returns:
- this / that
-
pow
This computes thisn.- Parameters:
n
- the power we are raising this BigInteger to.- Returns:
- thisn
-
bigIntegerValue
public java.math.BigInteger bigIntegerValue()This computes the integer part of this BigFraction, truncating toward zero.- Returns:
- a BigInteger representing the integer part of this BigFraction.
-
negate
This returns a new BigFraction represeting -this.- Returns:
- -this
-
abs
This computes the absolute value of a BigFraction as a BigFraction.- Returns:
- |this|
-
compareTo
This method retursn a negative integer if this < that, a positive integer if this > that, and zero if this equals that.- Specified by:
compareTo
in interfacejava.lang.Comparable<BigFraction>
- Parameters:
that
- the BigFraction we are comparing this BigFraction to- Returns:
- a negative integer if this < that, a positive integer if this > that and zero if this equals that.
-
max
-
min
-
harmonic
-
doubleValue
public double doubleValue() -
main
public static void main(java.lang.String[] args)
-