## Class Fraction

• All Implemented Interfaces:
Serializable, Cloneable, Comparable<Fraction>

public final class Fraction
extends Number
implements Comparable<Fraction>, 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
Author:
Martin Desruisseaux (MPO)
Serialized Form
• ### Constructor Summary

Constructors
Constructor and Description
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

All Methods
Modifier and Type Method and Description
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(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 hashCode()
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.
String toString()
Returns a string representation of this fraction.
• ### 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.

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 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 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 Number
Returns:
This fraction rounded to nearest integer.
• #### intValue

public int intValue()
Returns this fraction rounded to nearest integer.
Specified by:
intValue in class 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 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(Object other)
Compares this fraction with the given object for equality.
Overrides:
equals in class 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.
• #### hashCode

public int hashCode()
Overrides:
hashCode in class Object
• #### clone

public Fraction clone()
Returns a clone of this fraction.
Overrides:
clone in class Object
Returns:
A clone of this fraction.
• #### toString

public String toString()
Returns a string representation of this fraction.
Overrides:
toString in class Object
Returns:
A string representation of this fraction.