org.geotools.math
Class Fraction

java.lang.Object
  extended by java.lang.Number
      extended by org.geotools.math.Fraction
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.lang.Comparable<Fraction>

public final class Fraction
extends java.lang.Number
implements java.lang.Comparable<Fraction>, java.lang.Cloneable

A fraction made of a numerator and a denominator. This is not the purpose of this class to provides a full-fledged library for fractional number handling. This class exists mostly for the limited needs of some operations on tiled images.

For performance reasons, the methods in this class never create new objects. They always operate on an object specified in argument, and store the result in the object on which the method was invoked.

This class is final for performance reason.

Since:
2.5
Version:
$Id: Fraction.java 30725 2008-06-13 19:29:06Z desruisseaux $
Author:
Martin Desruisseaux (MPO)
See Also:
Serialized Form

Constructor Summary
Fraction()
          Creates a new fraction initialized to 0/0, which is an indetermined value.
Fraction(Fraction other)
          Creates a new fraction initialized to the same value than the given fraction.
Fraction(int numerator)
          Creates a new fraction initialized to the given numerator.
Fraction(int numerator, int denominator)
          Creates a new fraction.
 
Method Summary
 void add(Fraction other)
          Adds to this fraction the values given by the given fraction.
static int ceil(int numerator, int denominator)
          Computes numerator / denominator and rounds the result toward positive infinity.
 Fraction clone()
          Returns a clone of this fraction.
 int compareTo(Fraction other)
          Compares this fraction with the given one for order.
 int denominator()
          Returns the denominator.
 void divide(Fraction other)
          Divides this fraction by the given fraction.
 double doubleValue()
          Returns the fraction as a floating point number.
 boolean equals(java.lang.Object other)
          Compares this fraction with the given object for equality.
 float floatValue()
          Returns the fraction as a floating point number.
static int floor(int numerator, int denominator)
          Computes numerator / denominator and rounds the result toward negative infinity.
 int intValue()
          Returns this fraction rounded to nearest integer.
 long longValue()
          Returns this fraction rounded to nearest integer.
 void multiply(Fraction other)
          Multiplies this fraction by the given fraction.
 int numerator()
          Returns the numerator.
static int round(int numerator, int denominator)
          Computes numerator / denominator and rounds the result toward nearest integer.
static long round(long numerator, long denominator)
          Computes numerator / denominator and rounds the result toward nearest integer.
 void set(int numerator, int denominator)
          Sets this fraction to the given value.
 void subtract(Fraction other)
          Subtracts to this fraction the values given by the given fraction.
 java.lang.String toString()
          Returns a string representation of this fraction.
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Fraction

public Fraction()
Creates a new fraction initialized to 0/0, which is an indetermined value. Note that this is not the same than initializing a fraction to 0.


Fraction

public Fraction(Fraction other)
Creates a new fraction initialized to the same value than the given fraction.

Parameters:
other - The fraction to copy in this fraction.

Fraction

public Fraction(int numerator)
Creates a new fraction initialized to the given numerator.

Parameters:
numerator - The numerator.

Fraction

public Fraction(int numerator,
                int denominator)
Creates a new fraction.

Parameters:
numerator - The numerator.
denominator - The denominator.
Method Detail

set

public void set(int numerator,
                int denominator)
Sets this fraction to the given value.

Parameters:
numerator - The numerator.
denominator - The denominator.

add

public void add(Fraction other)
Adds to this fraction the values given by the given fraction. The results is stored in this fraction.

Parameters:
other - The fraction to add to this fraction.

subtract

public void subtract(Fraction other)
Subtracts to this fraction the values given by the given fraction. The results is stored in this fraction.

Parameters:
other - The fraction to subtract to this fraction.

multiply

public void multiply(Fraction other)
Multiplies this fraction by the given fraction. The results is stored in this fraction.

Parameters:
other - The fraction to multiply to this fraction.

divide

public void divide(Fraction other)
Divides this fraction by the given fraction. The results is stored in this fraction.

Parameters:
other - The fraction to divide to this fraction.

numerator

public int numerator()
Returns the numerator.

Returns:
The numerator.

denominator

public int denominator()
Returns the denominator.

Returns:
The denominator.

doubleValue

public double doubleValue()
Returns the fraction as a floating point number.

Specified by:
doubleValue in class java.lang.Number
Returns:
This fraction as a floating point number.

floatValue

public float floatValue()
Returns the fraction as a floating point number.

Specified by:
floatValue in class java.lang.Number
Returns:
This fraction as a floating point number.

longValue

public long longValue()
Returns this fraction rounded to nearest integer.

Specified by:
longValue in class java.lang.Number
Returns:
This fraction rounded to nearest integer.

intValue

public int intValue()
Returns this fraction rounded to nearest integer.

Specified by:
intValue in class java.lang.Number
Returns:
This fraction rounded to nearest integer.

round

public static long round(long numerator,
                         long denominator)
Computes numerator / denominator and rounds the result toward nearest integer. If the result is located at equal distance from the two nearest integers, then rounds to the even one.

Parameters:
numerator - The numerator in the division.
denominator - The denominator in the division.
Returns:
numerator / denominator rounded toward nearest integer.

round

public static int round(int numerator,
                        int denominator)
Computes numerator / denominator and rounds the result toward nearest integer. If the result is located at equal distance from the two nearest integers, then rounds to the even one.

Parameters:
numerator - The numerator in the division.
denominator - The denominator in the division.
Returns:
numerator / denominator rounded toward nearest integer.

floor

public static int floor(int numerator,
                        int denominator)
Computes numerator / denominator and rounds the result toward negative infinity. This is different from the default operation on primitive types, which rounds toward zero.

Tip: if the numerator and the denominator are both positive or both negative, then the result is positive and identical to numerator / denominator.

Parameters:
numerator - The numerator in the division.
denominator - The denominator in the division.
Returns:
numerator / denominator rounded toward negative infinity.

ceil

public static int ceil(int numerator,
                       int denominator)
Computes numerator / denominator and rounds the result toward positive infinity. This is different from the default operation on primitive types, which rounds toward zero.

Parameters:
numerator - The numerator in the division.
denominator - The denominator in the division.
Returns:
numerator / denominator rounded toward positive infinity.

compareTo

public int compareTo(Fraction other)
Compares this fraction with the given one for order.

Specified by:
compareTo in interface java.lang.Comparable<Fraction>
Parameters:
other - The fraction to compare to this fraction for ordering.
Returns:
A negative number if this fraction is smaller than the given fraction, a positive number if greater, or 0 if equals.

equals

public boolean equals(java.lang.Object other)
Compares this fraction with the given object for equality.

Overrides:
equals in class java.lang.Object
Parameters:
other - The object to compare with this fraction for equality.
Returns:
true if the given object is an other fraction numerically equals to this fraction.

clone

public Fraction clone()
Returns a clone of this fraction.

Overrides:
clone in class java.lang.Object
Returns:
A clone of this fraction.

toString

public java.lang.String toString()
Returns a string representation of this fraction.

Overrides:
toString in class java.lang.Object
Returns:
A string representation of this fraction.


Copyright © 1996-2010 Geotools. All Rights Reserved.