AVPKit
com::avpkit::core::Rational Class Reference
Inheritance diagram for com::avpkit::core::Rational:
Collaboration diagram for com::avpkit::core::Rational:

Public Member Functions

virtual IRationalcopy ()
 Creates a new IRational object by copying (by value) this object. More...
 
virtual int32_t getNumerator ()
 Get the numerator for this rational. More...
 
virtual int32_t getDenominator ()
 Get the denominator for this rational. More...
 
virtual int32_t compareTo (IRational *other)
 Compare a rational to this rational. More...
 
virtual double getDouble ()
 Rational to double conversion. More...
 
virtual int32_t reduce (int64_t num, int64_t den, int64_t max)
 Reduce a fraction to it's lowest common denominators. More...
 
virtual IRationalmultiply (IRational *arg)
 Multiplies this number by arg. More...
 
virtual IRationaldivide (IRational *arg)
 Divides this rational by arg. More...
 
virtual IRationalsubtract (IRational *arg)
 Subtracts arg from this rational. More...
 
virtual IRationaladd (IRational *arg)
 Adds arg to this rational. More...
 
virtual int64_t rescale (int64_t origValue, IRational *origBase)
 Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational. More...
 
virtual int64_t rescale (int64_t origValue, IRational *origBase, Rounding rounding)
 Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational. More...
 
virtual void setNumerator (int32_t value)
 Sets the numerator on this object. More...
 
virtual void setDenominator (int32_t value)
 Sets the denominator on this object. More...
 
virtual void setValue (double value)
 Sets the numerator and denominator on this object by reducing the double to the closest integer numerator and denominator. More...
 
virtual double getValue ()
 An alias for getDouble() but matching JavaBean conventions. More...
 
virtual bool isFinalized ()
 Returns true if init() has been called and this object is now considered finalized and immutable. More...
 
virtual void init ()
 Marks this object as finalized and immutable. More...
 
- Public Member Functions inherited from com::avpkit::ferry::RefCounted
virtual int32_t acquire ()
 Internal Only. More...
 
virtual int32_t release ()
 Internal Only. More...
 
virtual RefCountedcopyReference ()
 Create a new Java object that refers to the same native object. More...
 
virtual int32_t getCurrentRefCount ()
 Return the current reference count on this object. More...
 
void setJavaAllocator (void *allocator)
 This method is public but not part of the standard API. More...
 
void * getJavaAllocator ()
 This method is public but not part of the standard API. More...
 

Static Public Member Functions

static Rationalmake (double d)
 Converts a double precision floating point number to a rational. More...
 
static Rationalmake (AVRational *src)
 Create a Rational from an AVRational struct. More...
 
static Rationalmake (Rational *src)
 Creates copy of a Rational from another Rational. More...
 
static Rationalmake (int32_t num, int32_t den)
 Create a rational from a numerator and denominator. More...
 
static int64_t rescale (int64_t srcValue, int32_t dstNumerator, int32_t dstDenominator, int32_t srcNumerator, int32_t srcDenominator, Rounding rounding)
 
- Static Public Member Functions inherited from com::avpkit::core::IRational
static int32_t sCompareTo (IRational *a, IRational *b)
 Compare two rationals. More...
 
static int32_t sReduce (IRational *dst, int64_t num, int64_t den, int64_t max)
 Reduce a fraction to it's lowest common denominators. More...
 
static IRationalsMultiply (IRational *a, IRational *b)
 Multiples a by b. More...
 
static IRationalsDivide (IRational *a, IRational *b)
 Divides a by b. More...
 
static IRationalsSubtract (IRational *a, IRational *b)
 Subtracts a from b. More...
 
static IRationalsAdd (IRational *a, IRational *b)
 Adds a to b. More...
 
static int64_t sRescale (int64_t origValue, IRational *origBase, IRational *newBase)
 Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational. More...
 
static IRationalmake ()
 Get a new rational that will be set to 0/0. More...
 
static IRationalmake (double d)
 Converts a double precision floating point number to a rational. More...
 
static IRationalmake (IRational *src)
 Creates deep copy of a Rational from another Rational. More...
 
static IRationalmake (int32_t num, int32_t den)
 Create a rational from a numerator and denominator. More...
 
static int64_t sRescale (int64_t origValue, IRational *origBase, IRational *newBase, Rounding rounding)
 Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational. More...
 
static int64_t rescale (int64_t srcValue, int32_t dstNumerator, int32_t dstDenominator, int32_t srcNumerator, int32_t srcDenominator, Rounding rounding)
 Rescales a long value to another long value. More...
 

Additional Inherited Members

- Public Types inherited from com::avpkit::core::IRational
enum  Rounding {
  ROUND_ZERO = 0 , ROUND_INF = 1 , ROUND_DOWN = 2 , ROUND_UP = 3 ,
  ROUND_NEAR_INF = 5
}
 
typedef enum com::avpkit::core::IRational::Rounding Rounding
 
- Protected Member Functions inherited from com::avpkit::ferry::RefCounted
virtual void destroy ()
 This method is called by RefCounted objects when their Ref Count reaches zero and they are about to be destroyed.
 
- Protected Attributes inherited from com::avpkit::ferry::RefCounted
AtomicIntegermRefCount
 This is the internal reference count, represented as an AtomicInteger to make sure it is thread safe.
 
void * mAllocator
 Not part of public API.
 

Detailed Description

Definition at line 29 of file Rational.h.

Member Function Documentation

◆ add()

IRational * com::avpkit::core::Rational::add ( IRational arg)
virtual

Adds arg to this rational.

Parameters
argThe amount to add to this.
Returns
this+arg.

Implements com::avpkit::core::IRational.

Definition at line 227 of file Rational.cpp.

228  {
229  Rational *result = 0;
230  Rational *arg=dynamic_cast<Rational*>(other);
231  if (arg)
232  {
233  result = Rational::make();
234  if (result)
235  {
236  result->mRational = av_add_q(this->mRational,
237  arg->mRational);
238  }
239  }
240  return result;
241  }
static IRational * make()
Get a new rational that will be set to 0/0.
Definition: IRational.cpp:79

References com::avpkit::core::IRational::make().

◆ compareTo()

int32_t com::avpkit::core::Rational::compareTo ( IRational other)
virtual

Compare a rational to this rational.

Parameters
othersecond rational
Returns
0 if this==other, 1 if this>other and -1 if this<other.

Implements com::avpkit::core::IRational.

Definition at line 141 of file Rational.cpp.

142  {
143  int32_t result = 0;
144  Rational *arg=dynamic_cast<Rational*>(other);
145  if (arg)
146  result = av_cmp_q(mRational, arg->mRational);
147  return result;
148  }

◆ copy()

IRational * com::avpkit::core::Rational::copy ( )
virtual

Creates a new IRational object by copying (by value) this object.

Returns
the new object

Implements com::avpkit::core::IRational.

Definition at line 92 of file Rational.cpp.

93  {
94  return Rational::make(this);
95  }

References com::avpkit::core::IRational::make().

◆ divide()

IRational * com::avpkit::core::Rational::divide ( IRational arg)
virtual

Divides this rational by arg.

Parameters
argThe divisor to use.
Returns
this/arg.

Implements com::avpkit::core::IRational.

Definition at line 194 of file Rational.cpp.

195  {
196  Rational *result = 0;
197  Rational *arg=dynamic_cast<Rational*>(other);
198  if (arg)
199  {
200  result = Rational::make();
201  if (result)
202  {
203  result->mRational = av_div_q(this->mRational,
204  arg->mRational);
205  }
206  }
207  return result;
208  }

References com::avpkit::core::IRational::make().

◆ getDenominator()

virtual int32_t com::avpkit::core::Rational::getDenominator ( )
inlinevirtual

Get the denominator for this rational.

Returns
the denominator.

Implements com::avpkit::core::IRational.

Definition at line 37 of file Rational.h.

37 { return mRational.den; }

◆ getDouble()

double com::avpkit::core::Rational::getDouble ( )
virtual

Rational to double conversion.

Returns
(double) a

Implements com::avpkit::core::IRational.

Definition at line 151 of file Rational.cpp.

152  {
153  double result = 0;
154  // On some runs in Linux calling av_q2d will raise
155  // a FPE instead of returning back NaN or infinity,
156  // so we try to short-circuit that here.
157  if (mRational.den == 0)
158  if (mRational.num == 0)
159  result = std::numeric_limits<double>::quiet_NaN();
160  else
161  result = std::numeric_limits<double>::infinity();
162  else
163  result = av_q2d(mRational);
164  return result;
165  }

Referenced by getValue().

◆ getNumerator()

virtual int32_t com::avpkit::core::Rational::getNumerator ( )
inlinevirtual

Get the numerator for this rational.

Returns
the numerator.

Implements com::avpkit::core::IRational.

Definition at line 36 of file Rational.h.

36 { return mRational.num; }

◆ getValue()

double com::avpkit::core::Rational::getValue ( )
virtual

An alias for getDouble() but matching JavaBean conventions.

Since
3.2

Implements com::avpkit::core::IRational.

Definition at line 71 of file Rational.cpp.

72  {
73  return getDouble();
74  }
virtual double getDouble()
Rational to double conversion.
Definition: Rational.cpp:151

References getDouble().

◆ init()

void com::avpkit::core::Rational::init ( )
virtual

Marks this object as finalized and immutable.

Any setters called after the first init() call will be ignored.

Most make methods will call this method automatically, with the exception of the blank factory method make().

Since
3.2

Implements com::avpkit::core::IRational.

Definition at line 41 of file Rational.cpp.

42  {
43  if (!mInitialized) {
44  (void) reduce(mRational.num,
45  mRational.den,
46  FFMAX(mRational.den, mRational.num));
47  }
48  mInitialized = true;
49  }
virtual int32_t reduce(int64_t num, int64_t den, int64_t max)
Reduce a fraction to it's lowest common denominators.
Definition: Rational.cpp:168

References reduce().

Referenced by make().

◆ isFinalized()

bool com::avpkit::core::Rational::isFinalized ( )
virtual

Returns true if init() has been called and this object is now considered finalized and immutable.

Since
3.2

Implements com::avpkit::core::IRational.

Definition at line 124 of file Rational.cpp.

125  {
126  return mInitialized;
127  }

◆ make() [1/4]

Rational * com::avpkit::core::Rational::make ( AVRational *  src)
static

Create a Rational from an AVRational struct.

Parameters
srcThe source AVRational object.
Returns
A new Rational; caller must release() when done. Null if src is null.

Definition at line 77 of file Rational.cpp.

78  {
79  Rational *result=0;
80  if (src)
81  {
82  result = Rational::make();
83  if (result) {
84  result->mRational = *src;
85  result->init();
86  }
87  }
88  return result;
89  }

References init(), and com::avpkit::core::IRational::make().

◆ make() [2/4]

Rational * com::avpkit::core::Rational::make ( double  d)
static

Converts a double precision floating point number to a rational.

Parameters
ddouble to convert
Returns
A new Rational; caller must release() when done.

Definition at line 52 of file Rational.cpp.

53  {
54  Rational *result=0;
55  result = Rational::make();
56  if (result) {
57  result->setValue(d);
58  result->init();
59  }
60  return result;
61  }

References init(), com::avpkit::core::IRational::make(), and setValue().

◆ make() [3/4]

Rational * com::avpkit::core::Rational::make ( int32_t  num,
int32_t  den 
)
static

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.

Parameters
numThe numerator of the resulting Rational
denThe denominator of the resulting Rational
Returns
A new Rational; caller must call release.

Definition at line 129 of file Rational.cpp.

130  {
131  Rational *result=0;
132  result = Rational::make();
133  if (result) {
134  result->setNumerator(num);
135  result->setDenominator(den);
136  result->init();
137  }
138  return result;
139  }

References init(), com::avpkit::core::IRational::make(), setDenominator(), and setNumerator().

◆ make() [4/4]

Rational * com::avpkit::core::Rational::make ( Rational src)
static

Creates copy of a Rational from another Rational.

Note: This is a NEW object. To just keep tabs on the original, use acquire() to keep a reference.

Parameters
srcThe source Rational to copy.
Returns
A new Rational; caller must call release. Returns null if src is null.

Definition at line 98 of file Rational.cpp.

99  {
100  Rational *result=0;
101  if (src)
102  {
103  result = Rational::make();
104  if (result) {
105  result->mRational = src->mRational;
106  result->init();
107  }
108  }
109  return result;
110  }

References init(), and com::avpkit::core::IRational::make().

◆ multiply()

IRational * com::avpkit::core::Rational::multiply ( IRational arg)
virtual

Multiplies this number by arg.

Parameters
argnumber to mulitply by.
Returns
this*arg.

Implements com::avpkit::core::IRational.

Definition at line 177 of file Rational.cpp.

178  {
179  Rational *result = 0;
180  Rational *arg=dynamic_cast<Rational*>(other);
181  if (arg)
182  {
183  result = Rational::make();
184  if (result)
185  {
186  result->mRational = av_mul_q(this->mRational,
187  arg->mRational);
188  }
189  }
190  return result;
191  }

References com::avpkit::core::IRational::make().

◆ reduce()

int32_t com::avpkit::core::Rational::reduce ( int64_t  num,
int64_t  den,
int64_t  max 
)
virtual

Reduce a fraction to it's lowest common denominators.

This is useful for framerate calculations.

Parameters
numthe src numerator.
denthe src denominator.
maxthe maximum allowed for nom & den in the reduced fraction.
Returns
1 if exact, 0 otherwise

Implements com::avpkit::core::IRational.

Definition at line 168 of file Rational.cpp.

169  {
170  int32_t result = 0;
171  result = av_reduce(&mRational.num, &mRational.den,
172  num, den, max);
173  return result;
174  }

Referenced by init().

◆ rescale() [1/2]

int64_t com::avpkit::core::Rational::rescale ( int64_t  origValue,
IRational origBase 
)
virtual

Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational.

Parameters
origValueThe original int64_t value you care about.
origBaseThe original base Rational that origValue is scaled with.
Returns
The new integer value, scaled in units of this IRational.

Implements com::avpkit::core::IRational.

Definition at line 244 of file Rational.cpp.

245  {
246  int64_t retval=origValue;
247  Rational *arg=dynamic_cast<Rational*>(origBase);
248 
249  if (arg)
250  {
251  retval = av_rescale_q(origValue, arg->mRational, this->mRational);
252  }
253  return retval;
254  }

Referenced by com::avpkit::core::IRational::rescale().

◆ rescale() [2/2]

int64_t com::avpkit::core::Rational::rescale ( int64_t  origValue,
IRational origBase,
Rounding  rounding 
)
virtual

Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this Rational.

Parameters
origValueThe original int64_t value you care about.
origBaseThe original base Rational that origValue is scaled with.
roundingHow you want rounding to occur
Returns
The new integer value, scaled in units of this IRational.

Implements com::avpkit::core::IRational.

Definition at line 257 of file Rational.cpp.

259  {
260  int64_t retval=origValue;
261  Rational *arg=dynamic_cast<Rational*>(origBase);
262 
263  if (arg)
264  {
265  int64_t b = arg->mRational.num * (int64_t)this->mRational.den;
266  int64_t c = this->mRational.num * (int64_t)arg->mRational.den;
267 
268  retval = av_rescale_rnd(origValue, b,
269  c, (enum AVRounding)rounding);
270  }
271  return retval;
272  }

◆ setDenominator()

void com::avpkit::core::Rational::setDenominator ( int32_t  value)
virtual

Sets the denominator on this object.

If isFinalized is true, then this method is ignored.

Since
3.2

Implements com::avpkit::core::IRational.

Definition at line 118 of file Rational.cpp.

119  {
120  if (!mInitialized)
121  mRational.den = den;
122  }

Referenced by make().

◆ setNumerator()

void com::avpkit::core::Rational::setNumerator ( int32_t  value)
virtual

Sets the numerator on this object.

If isFinalized is true, then this method is ignored.

Since
3.2

Implements com::avpkit::core::IRational.

Definition at line 112 of file Rational.cpp.

113  {
114  if (!mInitialized)
115  mRational.num = num;
116  }

Referenced by make().

◆ setValue()

void com::avpkit::core::Rational::setValue ( double  value)
virtual

Sets the numerator and denominator on this object by reducing the double to the closest integer numerator and denominator.

If isFinalized is true, then this method is ignored.

Since
3.2

Implements com::avpkit::core::IRational.

Definition at line 64 of file Rational.cpp.

65  {
66  if (!mInitialized)
67  mRational = av_d2q(d, 0x7fffffff);
68  }

Referenced by make().

◆ subtract()

IRational * com::avpkit::core::Rational::subtract ( IRational arg)
virtual

Subtracts arg from this rational.

Parameters
argThe amount to subtract from this.
Returns
this-arg.

Implements com::avpkit::core::IRational.

Definition at line 211 of file Rational.cpp.

212  {
213  Rational *result = 0;
214  Rational *arg=dynamic_cast<Rational*>(other);
215  if (arg)
216  {
217  result = Rational::make();
218  if (result)
219  {
220  result->mRational = av_sub_q(this->mRational,
221  arg->mRational);
222  }
223  }
224  return result;
225  }

References com::avpkit::core::IRational::make().


The documentation for this class was generated from the following files: