AVPKit
IRational.h
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 
20 #ifndef IRATIONAL_H_
21 #define IRATIONAL_H_
22 
23 #include <com/avpkit/ferry/RefCounted.h>
24 #include <com/avpkit/core/AVPKit.h>
25 
26 namespace com { namespace avpkit { namespace core
27 {
28 
42  class VS_API_AVPKIT IRational : public com::avpkit::ferry::RefCounted
43  {
44  public:
45 
51  virtual int32_t getNumerator()=0;
52 
57  virtual int32_t getDenominator()=0;
58 
64  virtual IRational * copy()=0;
65 
71  virtual int32_t compareTo(IRational*other)=0;
72 
80  static int32_t sCompareTo(IRational *a, IRational *b);
81 
87  virtual double getDouble()=0;
88 
97  virtual int32_t reduce(int64_t num, int64_t den, int64_t max)=0;
98 
108  static int32_t sReduce(IRational *dst, int64_t num,
109  int64_t den, int64_t max);
110 
116  virtual IRational* multiply(IRational *arg)=0;
117 
124  static IRational* sMultiply(IRational* a, IRational*b);
125 
131  virtual IRational* divide(IRational *arg)=0;
132 
139  static IRational* sDivide(IRational *a, IRational* b);
140 
146  virtual IRational* subtract(IRational *arg)=0;
153  static IRational* sSubtract(IRational *a, IRational* b);
154 
160  virtual IRational* add(IRational *arg)=0;
167  static IRational* sAdd(IRational *a, IRational* b);
168 
178  virtual int64_t rescale(int64_t origValue, IRational* origBase)=0;
179 
190  static int64_t sRescale(int64_t origValue, IRational* origBase, IRational* newBase);
191 
199  static IRational *make();
200 
206  static IRational *make(double d);
214  static IRational* make(IRational *src);
215 
227  static IRational *make(int32_t num, int32_t den);
228 
229  /*
230  * Added for 3.1
231  */
232 
233  typedef enum Rounding {
234  ROUND_ZERO = 0,
235  ROUND_INF = 1,
236  ROUND_DOWN = 2,
237  ROUND_UP = 3,
238  ROUND_NEAR_INF = 5,
239  } Rounding;
249  virtual int64_t rescale(int64_t origValue,
250  IRational* origBase,
251  Rounding rounding)=0;
252 
264  static int64_t sRescale(int64_t origValue,
265  IRational* origBase, IRational* newBase,
266  Rounding rounding);
267 
268  /*
269  * Added for 3.2
270  */
271 
298  static int64_t rescale(int64_t srcValue,
299  int32_t dstNumerator,
300  int32_t dstDenominator,
301  int32_t srcNumerator,
302  int32_t srcDenominator,
303  Rounding rounding);
304 
305  protected:
306  IRational();
307  virtual ~IRational();
308 
309  public:
310  /*
311  * Added for 3.2
312  */
313 
322  virtual void setNumerator(int32_t value)=0;
323 
324 
333  virtual void setDenominator(int32_t value)=0;
334 
345  virtual void setValue(double value)=0;
346 
352  virtual double getValue()=0;
353 
360  virtual bool isFinalized()=0;
361 
373  virtual void init()=0;
374  };
375 
376 }}}
377 
378 #endif /*IRATIONAL_H_*/
This class wraps represents a Rational number for the AVPKit.
Definition: IRational.h:43
virtual double getValue()=0
An alias for getDouble() but matching JavaBean conventions.
virtual int32_t compareTo(IRational *other)=0
Compare a rational to this rational.
virtual IRational * divide(IRational *arg)=0
Divides this rational by arg.
virtual int64_t rescale(int64_t origValue, IRational *origBase)=0
Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this...
virtual int32_t reduce(int64_t num, int64_t den, int64_t max)=0
Reduce a fraction to it's lowest common denominators.
virtual int64_t rescale(int64_t origValue, IRational *origBase, Rounding rounding)=0
Takes a value scaled in increments of origBase and gives the equivalent value scaled in terms of this...
virtual IRational * add(IRational *arg)=0
Adds arg to this rational.
virtual bool isFinalized()=0
Returns true if init() has been called and this object is now considered finalized and immutable.
virtual IRational * copy()=0
Creates a new IRational object by copying (by value) this object.
virtual int32_t getDenominator()=0
Get the denominator for this rational.
virtual IRational * multiply(IRational *arg)=0
Multiplies this number by arg.
virtual void setDenominator(int32_t value)=0
Sets the denominator on this object.
virtual IRational * subtract(IRational *arg)=0
Subtracts arg from this rational.
virtual void setValue(double value)=0
Sets the numerator and denominator on this object by reducing the double to the closest integer numer...
virtual void init()=0
Marks this object as finalized and immutable.
virtual void setNumerator(int32_t value)=0
Sets the numerator on this object.
virtual int32_t getNumerator()=0
Get the numerator for this rational.
virtual double getDouble()=0
Rational to double conversion.
Parent of all Ferry objects – it mains reference counts in native code.
Definition: RefCounted.h:85
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...