Copyright 2012-04-26 Werner Randelshofer

org.monte.media.math
Class Rational

java.lang.Object
  extended by java.lang.Number
      extended by org.monte.media.math.Rational
All Implemented Interfaces:
java.io.Serializable

public class Rational
extends java.lang.Number

Represents a TIFF RATIONAL number.

Two LONGs 32-bit (4-byte) unsigned integer: the first represents the numerator of a fraction; the second, the denominator.

Invariants:

Version:
1.3 2011-08-27 Adds add method.
1.2 2010-09-07 Extends Number instead of Object.
1.1 2010-07-06 Shows decimal value next to the fraction.
1.0 2009-12-27 Created.
Author:
Werner Randelshofer
See Also:
Serialized Form

Field Summary
static long serialVersionUID
           
 
Constructor Summary
Rational(long numerator, long denominator)
           
Rational(Rational r)
           
 
Method Summary
 Rational add(Rational that)
           
 Rational ceil(long d)
          Returns the closest rational with the specified denominator which is greater or equal than this number.
 int compareTo(Rational that)
          return { -1, 0, +1 } if a < b, a = b, or a > b.
 Rational divide(Rational that)
           
 double doubleValue()
           
 boolean equals(java.lang.Object obj)
           
 float floatValue()
           
 Rational floor(long d)
          Returns the closest rational with the specified denominator which is smaller or equal than this number.
 long getDenominator()
           
 long getNumerator()
           
 int hashCode()
           
 int intValue()
           
 Rational inverse()
           
 boolean isLessOrEqualZero()
           
 boolean isZero()
           
 long longValue()
           
static void main(java.lang.String[] args)
           
static Rational max(Rational a, Rational b)
           
static Rational min(Rational a, Rational b)
           
 Rational multiply(long integer)
           
 Rational multiply(Rational that)
          Warning.
 Rational negate()
           
 Rational round(long d)
           
 Rational subtract(Rational that)
          Warning.
 java.lang.String toDescriptiveString()
           
 java.lang.String toString()
           
static Rational valueOf(double d)
           
 
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

serialVersionUID

public static final long serialVersionUID
See Also:
Constant Field Values
Constructor Detail

Rational

public Rational(long numerator,
                long denominator)

Rational

public Rational(Rational r)
Method Detail

getNumerator

public long getNumerator()

getDenominator

public long getDenominator()

add

public Rational add(Rational that)

subtract

public Rational subtract(Rational that)
Warning. Rational is supposed to be immutable. * private Rational addAssign(Rational that) { if (this.den == that.den) { // => same denominator: add numerators this.num += that.num; return this; } // FIXME - handle overflow long s = scm(this.den, that.den); this.num = this.num * (s / this.den) + that.num * (s / that.den); this.den = s; return reduceAssign(); }


negate

public Rational negate()

inverse

public Rational inverse()

floor

public Rational floor(long d)
Returns the closest rational with the specified denominator which is smaller or equal than this number.


ceil

public Rational ceil(long d)
Returns the closest rational with the specified denominator which is greater or equal than this number.


multiply

public Rational multiply(Rational that)
Warning. Rational is supposed to be immutable. * / private Rational reduceAssign() { long g = gcd(num, den); num /= g; den /= g; return this; }


multiply

public Rational multiply(long integer)

divide

public Rational divide(Rational that)

toString

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

toDescriptiveString

public java.lang.String toDescriptiveString()

intValue

public int intValue()
Specified by:
intValue in class java.lang.Number

longValue

public long longValue()
Specified by:
longValue in class java.lang.Number

floatValue

public float floatValue()
Specified by:
floatValue in class java.lang.Number

doubleValue

public double doubleValue()
Specified by:
doubleValue in class java.lang.Number

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

compareTo

public int compareTo(Rational that)
return { -1, 0, +1 } if a < b, a = b, or a > b.


hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

max

public static Rational max(Rational a,
                           Rational b)

min

public static Rational min(Rational a,
                           Rational b)

isZero

public boolean isZero()

isLessOrEqualZero

public boolean isLessOrEqualZero()

valueOf

public static Rational valueOf(double d)

main

public static void main(java.lang.String[] args)

round

public Rational round(long d)

Copyright 2012-04-26 Werner Randelshofer