|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
ObjectNumber
Fraction
public final class Fraction
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.
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(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. |
String |
toString()
Returns a string representation of this fraction. |
Methods inherited from class Number |
---|
byteValue, shortValue |
Methods inherited from class Object |
---|
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Fraction()
public Fraction(Fraction other)
other
- The fraction to copy in this fraction.public Fraction(int numerator)
numerator
- The numerator.public Fraction(int numerator, int denominator)
numerator
- The numerator.denominator
- The denominator.Method Detail |
---|
public void set(int numerator, int denominator)
numerator
- The numerator.denominator
- The denominator.public void add(Fraction other)
other
- The fraction to add to this fraction.public void subtract(Fraction other)
other
- The fraction to subtract to this fraction.public void multiply(Fraction other)
other
- The fraction to multiply to this fraction.public void divide(Fraction other)
other
- The fraction to divide to this fraction.public int numerator()
public int denominator()
public double doubleValue()
doubleValue
in class Number
public float floatValue()
floatValue
in class Number
public long longValue()
longValue
in class Number
public int intValue()
intValue
in class Number
public static long round(long numerator, long denominator)
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.
numerator
- The numerator in the division.denominator
- The denominator in the division.
numerator / denominator
rounded toward nearest integer.public static int round(int numerator, int denominator)
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.
numerator
- The numerator in the division.denominator
- The denominator in the division.
numerator / denominator
rounded toward nearest integer.public static int floor(int numerator, int denominator)
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
.
numerator
- The numerator in the division.denominator
- The denominator in the division.
numerator / denominator
rounded toward negative infinity.public static int ceil(int numerator, int denominator)
numerator / denominator
and rounds the result toward positive infinity.
This is different from the default operation on primitive types, which rounds toward zero.
numerator
- The numerator in the division.denominator
- The denominator in the division.
numerator / denominator
rounded toward positive infinity.public int compareTo(Fraction other)
compareTo
in interface Comparable<Fraction>
other
- The fraction to compare to this fraction for ordering.
public boolean equals(Object other)
equals
in class Object
other
- The object to compare with this fraction for equality.
true
if the given object is an other fraction numerically
equals to this fraction.public Fraction clone()
clone
in class Object
public String toString()
toString
in class Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |