public final class Fraction extends Number implements Comparable<Fraction>, Cloneable
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 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.
|
Modifier and Type | Method and Description |
---|---|
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 |
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.
|
byteValue, shortValue
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.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()
public int intValue()
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)
public Fraction clone()
Copyright © 1996–2023 Geotools. All rights reserved.