edu.princeton.repeatedgames.rgsolve.utilities
Class Fraction

java.lang.Object
  extended by edu.princeton.repeatedgames.rgsolve.utilities.Fraction

public class Fraction
extends java.lang.Object

A class for converting doubles to rationals, and the reverse. See http://shreevatsa.wordpress.com/2011/01/10/not-all-best- rational-approximations-are-the-convergents-of-the-continued-fraction/


Field Summary
private  long denominator
          the denominator of this fraction
static int MAX_CONVERGENTS
          default Maximum number of convergents in series to calculate
private  long numerator
          the numerator of this fraction
static Fraction ONE
          1/1
static double TOLERANCE
          default approximation tolerance of fraction to inputed double
static Fraction ZERO
          0/1
 
Constructor Summary
Fraction(double value)
          Constructs a rational approximation to the double value
Fraction(long numerator, long denominator)
          Constructor.
 
Method Summary
 Fraction add(Fraction f)
           
static long gcd(long A, long B)
          The greatest common divisor of integers A and B
static Fraction getFraction(double x)
          Finds rational approximation to reals (doubles) x, using default parameters.
static Fraction getFraction(double x, double tolerance, int maxConvergents)
          Finds rational approximation to reals (doubles) x.
 java.lang.String getLatexString()
           
 Fraction getReciprocal()
          Gets the reciprocal.
 double getValue()
          Returns this fraction as a double value
 Fraction multiply(Fraction f)
           
 Fraction over(Fraction f)
          Returns this / f
static boolean setMaxConvergents()
          GUI display to change max convergents
static void setMaxConvergents(int max)
           
 Fraction subtract(Fraction f)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ONE

public static final Fraction ONE
1/1


ZERO

public static final Fraction ZERO
0/1


TOLERANCE

public static final double TOLERANCE
default approximation tolerance of fraction to inputed double

See Also:
Constant Field Values

MAX_CONVERGENTS

public static int MAX_CONVERGENTS
default Maximum number of convergents in series to calculate


numerator

private long numerator
the numerator of this fraction


denominator

private long denominator
the denominator of this fraction

Constructor Detail

Fraction

public Fraction(long numerator,
                long denominator)
Constructor. Throws exception for denominator = 0

Parameters:
numerator -
denominator -

Fraction

public Fraction(double value)
Constructs a rational approximation to the double value

Parameters:
value -
Method Detail

setMaxConvergents

public static boolean setMaxConvergents()
GUI display to change max convergents


add

public Fraction add(Fraction f)

subtract

public Fraction subtract(Fraction f)

multiply

public Fraction multiply(Fraction f)

over

public Fraction over(Fraction f)
Returns this / f

Parameters:
f -
Returns:
this/ / f

getReciprocal

public Fraction getReciprocal()
Gets the reciprocal. Throws exception if numerator is zero

Returns:
1 / this

getValue

public double getValue()
Returns this fraction as a double value

Returns:
the double value of this fraction

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

gcd

public static long gcd(long A,
                       long B)
The greatest common divisor of integers A and B

Parameters:
A -
B -
Returns:
The greatest common divisor of integers A and B

getFraction

public static Fraction getFraction(double x)
Finds rational approximation to reals (doubles) x, using default parameters.

Parameters:
x - the real to be approximated
Returns:
a rational approximation to x

getFraction

public static Fraction getFraction(double x,
                                   double tolerance,
                                   int maxConvergents)
Finds rational approximation to reals (doubles) x.

Parameters:
x - the real to be approximated
tolerance - distance between approximation and x prompting termination
maxConvergents - maximum number of convergents in series to calculate
Returns:
a rational approximation to x

getLatexString

public java.lang.String getLatexString()

setMaxConvergents

public static void setMaxConvergents(int max)