
public class Rational extends RefCounted
Video formats often use rational numbers, and converting between
them willy nilly can lead to rounding errors, and eventually, out
of sync problems. Therefore we use IRational objects to pass
around Rational Numbers and avoid conversion until the very last moment.
Note: There are some static convenience methods
in this class that start with s*. They start with s
(as opposed to overloading methods (e.g. sAdd(...) vs. add(...)).
| Modifier and Type | Class and Description |
|---|---|
static class |
Rational.Rounding
A type to specify how rounding should be done.
|
| Modifier and Type | Method and Description |
|---|---|
Rational |
add(Rational arg)
Adds arg to this rational
|
int |
compareTo(Rational other)
Compare a rational to this rational
|
Rational |
copy()
Creates a new IRational object by copying (by value) this object.
|
Rational |
copyReference()
Create a new Rational object that is actually referring to the
exact same underlying native object.
|
Rational |
divide(Rational arg)
Divides this rational by arg.
|
boolean |
equals(Object obj)
Compares two values, returning true if the underlying objects in native code are the same object.
|
int |
getDenominator()
Get the denominator for this rational.
|
double |
getDouble()
Rational to double conversion.
|
int |
getNumerator()
Get the numerator for this rational.
|
double |
getValue()
An alias for #getDouble() but matching JavaBean
conventions. |
int |
hashCode()
Get a hashable value for this object.
|
void |
init()
Marks this object as finalized and immutable.
|
boolean |
isFinalized()
Returns true if #init() has been called and
this object is now considered finalized and immutable. |
boolean |
isNegative()
Is this number negative?
|
boolean |
isPositive()
Is this number positive?
|
static Rational |
make()
Get a new rational that will be set to 0/1.
The rational will not have #init() called and hence will be modifiable by #setValue(double) until #init() is called. |
static Rational |
make(double d)
Converts a double precision floating point number to a rational.
|
static Rational |
make(int num,
int den)
Create a rational from a numerator and denominator.
We will always reduce this to the lowest num/den pair we can, but never having den exceed what was passed in. |
static Rational |
make(Rational src)
Creates deep copy of a Rational from another Rational.
|
Rational |
multiply(Rational arg)
Multiplies this number by arg
|
static boolean |
negative(Rational num)
Is this number not null and negative.
|
static boolean |
positive(Rational num)
Is this number not null and positive.
|
int |
reduce(long num,
long den,
long max)
Reduce a fraction to it's lowest common denominators.
This is useful for framerate calculations. |
static long |
rescale(long srcValue,
int dstNumerator,
int dstDenominator,
int srcNumerator,
int srcDenominator,
Rational.Rounding rounding)
Rescales a long value to another long value.
|
long |
rescale(long origValue,
Rational origBase)
Takes a value scaled in increments of origBase and gives the
equivalent value scaled in terms of this Rational. |
long |
rescale(long origValue,
Rational origBase,
Rational.Rounding rounding)
Takes a value scaled in increments of origBase and gives the
equivalent value scaled in terms of this Rational. |
static Rational |
sAdd(Rational a,
Rational b)
Adds a to b.
|
static int |
sCompareTo(Rational a,
Rational b)
Compare two rationals
|
static Rational |
sDivide(Rational a,
Rational b)
Divides a by b.
|
void |
setDenominator(int value)
Sets the denominator on this object.
|
void |
setNumerator(int value)
Sets the numerator on this object.
|
void |
setValue(double value)
Sets the numerator and denominator on this object by
reducing the double to the closest integer numerator and denominator. |
static Rational |
sMultiply(Rational a,
Rational b)
Multiples a by b.
|
static int |
sReduce(Rational dst,
long num,
long den,
long max)
Reduce a fraction to it's lowest common denominators.
This is useful for framerate calculations. |
static long |
sRescale(long origValue,
Rational origBase,
Rational newBase)
Takes a value scaled in increments of origBase and gives the
equivalent value scaled in terms of this Rational. |
static long |
sRescale(long origValue,
Rational origBase,
Rational newBase,
Rational.Rounding rounding)
Takes a value scaled in increments of origBase and gives the
equivalent value scaled in terms of this Rational. |
static Rational |
sSubtract(Rational a,
Rational b)
Subtracts a from b.
|
Rational |
subtract(Rational arg)
Subtracts arg from this rational
|
String |
toString()
Prints the contents of this object as a fraction.
|
delete, getCurrentRefCountpublic Rational copyReference()
copyReference in class RefCountedpublic boolean equals(Object obj)
public int hashCode()
public String toString()
toString in class ObjectgetNumerator()/getDenominator()"public boolean isPositive()
public static boolean positive(Rational num)
num - the number; may be nullpublic boolean isNegative()
public static boolean negative(Rational num)
num - the number; may be nullpublic int getNumerator()
public int getDenominator()
public Rational copy()
public int compareTo(Rational other)
other - second rationalpublic static int sCompareTo(Rational a, Rational b)
a - the first rationalb - the second rationalpublic double getDouble()
public int reduce(long num,
long den,
long max)
num - the src numerator.den - the src denominator.max - the maximum allowed for nom & den in the reduced fraction.public static int sReduce(Rational dst, long num, long den, long max)
dst - The destination rational num - the src numerator.den - the src denominator.max - the maximum allowed for nom & den in the reduced fraction.public Rational multiply(Rational arg)
arg - number to mulitply by.public static Rational sMultiply(Rational a, Rational b)
a - the first numberb - the second number.public Rational divide(Rational arg)
arg - The divisor to use.public static Rational sDivide(Rational a, Rational b)
a - The first number.public Rational subtract(Rational arg)
arg - The amount to subtract from this.public static Rational sSubtract(Rational a, Rational b)
a - The first number.public Rational add(Rational arg)
arg - The amount to add to this.public static Rational sAdd(Rational a, Rational b)
a - The first number.public long rescale(long origValue,
Rational origBase)
origValue - The original int64_t value you care about.origBase - The original base Rational that origValue is scaled with.public static long sRescale(long origValue,
Rational origBase,
Rational newBase)
origValue - The original int64_t value you care about.origBase - The original base Rational that origValue is scaled with.newBase - The rational you want to rescale origValue into.public static Rational make()
public static Rational make(double d)
d - double to convertpublic static Rational make(Rational src)
src - The source Rational to copy.public static Rational make(int num, int den)
num - The numerator of the resulting Rationalden - The denominator of the resulting Rationalpublic long rescale(long origValue,
Rational origBase,
Rational.Rounding rounding)
origValue - The original int64_t value you care about.origBase - The original base Rational that origValue is scaled with.rounding - How you want rounding to occurpublic static long sRescale(long origValue,
Rational origBase,
Rational newBase,
Rational.Rounding rounding)
origValue - The original int64_t value you care about.origBase - The original base Rational that origValue is scaled with.newBase - The rational you want to rescale origValue into.rounding - How you want rounding to occurpublic static long rescale(long srcValue,
int dstNumerator,
int dstDenominator,
int srcNumerator,
int srcDenominator,
Rational.Rounding rounding)
This method doesn't use IRational values, but
instead uses numerators and denominators
passed in by the caller. It will not result
in any memory allocations.
srcValue - The value to rescale.dstNumerator - The numerator of the unitsdstDenominator - The denominator of the unitssrcNumerator - The numerator of the unitssrcValue is expressed in.srcDenominator - The denominator of the unitssrcValue is expressed in.rounding - How you want rounding to occurpublic void setNumerator(int value)
If #isFinalized is true, then this method is ignored.
public void setDenominator(int value)
If #isFinalized is true, then this method is ignored.
public void setValue(double value)
If #isFinalized is true, then this method is ignored.
public double getValue()
public boolean isFinalized()
public void init()
Most make methods will call this method
automatically, with the exception of the blank factory
method #make().
Copyright © 2018 Humble Software. All rights reserved.