AVPKit
IRational.cpp
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 #include "IRational.h"
21 #include "Global.h"
22 #include "Rational.h"
23 
24 namespace com { namespace avpkit { namespace core
25  {
26 
27  IRational::IRational()
28  {
29  }
30 
31  IRational::~IRational()
32  {
33  }
34 
35  int32_t
37  {
38  return a->compareTo(b);
39  }
40 
41  int32_t
42  IRational :: sReduce(IRational *dst, int64_t num,
43  int64_t den, int64_t max)
44  {
45  return (dst ? dst->reduce(num, den, max) : 0);
46  }
47 
48  IRational*
50  {
51  return a->divide(b);
52  }
53 
54  IRational*
56  {
57  return a->subtract(b);
58  }
59 
60  IRational*
62  {
63  return a->add(b);
64  }
65 
66  IRational*
68  {
69  return a->multiply(b);
70  }
71 
72  int64_t
73  IRational :: sRescale(int64_t origValue, IRational* origBase, IRational* newBase)
74  {
75  return newBase->rescale(origValue, origBase);
76  }
77 
78  IRational*
80  {
81  Global::init();
82  return Rational::make();
83  }
84 
85  IRational*
87  {
88  Global::init();
89  return Rational::make(d);
90  }
91 
92  IRational*
94  {
95  Global::init();
96  Rational* src = dynamic_cast<Rational*>(aSrc);
97  IRational* retval = 0;
98  if (src)
99  {
100  retval = Rational::make(src);
101  }
102  return retval;
103  }
104 
105  IRational*
106  IRational :: make(int32_t num, int32_t den)
107  {
108  Global::init();
109  return Rational::make(num, den);
110  }
111 
112  int64_t
113  IRational :: sRescale(int64_t origValue,
114  IRational* origBase, IRational* newBase,
115  Rounding rounding)
116  {
117  if (!origBase || !newBase)
118  return origValue;
119  return newBase->rescale(origValue, origBase, rounding);
120  }
121 
122  int64_t
123  IRational :: rescale(int64_t srcValue,
124  int32_t dstNumerator,
125  int32_t dstDenominator,
126  int32_t srcNumerator,
127  int32_t srcDenominator,
128  Rounding rounding)
129  {
130  return Rational::rescale(srcValue,
131  dstNumerator, dstDenominator,
132  srcNumerator, srcDenominator,
133  rounding);
134  }
135 
136  }}}
static void init()
Internal Only.
Definition: Global.cpp:157
This class wraps represents a Rational number for the AVPKit.
Definition: IRational.h:43
static int32_t sReduce(IRational *dst, int64_t num, int64_t den, int64_t max)
Reduce a fraction to it's lowest common denominators.
Definition: IRational.cpp:42
virtual int32_t compareTo(IRational *other)=0
Compare a rational to this rational.
virtual IRational * divide(IRational *arg)=0
Divides this rational by arg.
static IRational * make()
Get a new rational that will be set to 0/0.
Definition: IRational.cpp:79
static IRational * sMultiply(IRational *a, IRational *b)
Multiples a by b.
Definition: IRational.cpp:67
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.
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...
Definition: IRational.cpp:73
static IRational * sSubtract(IRational *a, IRational *b)
Subtracts a from b.
Definition: IRational.cpp:55
virtual IRational * add(IRational *arg)=0
Adds arg to this rational.
static int32_t sCompareTo(IRational *a, IRational *b)
Compare two rationals.
Definition: IRational.cpp:36
virtual IRational * multiply(IRational *arg)=0
Multiplies this number by arg.
virtual IRational * subtract(IRational *arg)=0
Subtracts arg from this rational.
static IRational * sAdd(IRational *a, IRational *b)
Adds a to b.
Definition: IRational.cpp:61
static IRational * sDivide(IRational *a, IRational *b)
Divides a by b.
Definition: IRational.cpp:49
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...
Definition: Rational.cpp:244
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...