Copyright 2013-01-06 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:
$Id: Rational.java 299 2013-01-03 07:40:18Z werner $
Author:
Werner Randelshofer
See Also:
Serialized Form

Field Summary
static Rational ONE
           
static long serialVersionUID
           
static Rational ZERO
           
 
Constructor Summary
Rational(long numerator)
           
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 Rational max(Rational a, Rational b)
           
static Rational min(Rational a, Rational b)
           
 Rational multiply(long integer)
           
 Rational multiply(Rational that)
           
 Rational negate()
           
 Rational round(long d)
           
 Rational subtract(Rational that)
          Warning.
 java.lang.String toDescriptiveString()
           
 java.lang.String toString()
           
static Rational valueOf(java.math.BigInteger num, java.math.BigInteger den)
           
static Rational valueOf(double d)
           
static Rational valueOf(long num, long den)
           
static Rational valueOf(java.lang.String str)
          Parses a string.
 
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

ONE

public static final Rational ONE

ZERO

public static final Rational ZERO

serialVersionUID

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

Rational

public Rational(long numerator)

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)

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)

valueOf

public static Rational valueOf(long num,
                               long den)

valueOf

public static Rational valueOf(java.math.BigInteger num,
                               java.math.BigInteger den)

round

public Rational round(long d)

valueOf

public static Rational valueOf(java.lang.String str)
Parses a string. A rational can be represented in the following ways:
  • As a long number
  • As a double number
  • As an integer/integer rational number
  • Throws:
    java.lang.NumberFormatException - if str can not be parsed.

    Copyright 2013-01-06 Werner Randelshofer