1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/* This file is part of the KDE project
Copyright (C) 2005 Tomas Mecir <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KSPREAD_VALUECALC
#define KSPREAD_VALUECALC
#include <map>
#include <tqvaluelist.h>
#include <tqvaluevector.h>
#include "kspread_value.h"
namespace KSpread
{
class Doc;
class ValueCalc;
class ValueConverter;
// Condition structures
enum Comp { isEqual, isLess, isGreater, lessEqual, greaterEqual, notEqual };
enum Type { numeric, string };
struct Condition
{
Comp comp;
int index;
double value;
TQString stringValue;
Type type;
};
typedef TQValueList<Condition> ConditionList;
typedef void (*arrayWalkFunc) (ValueCalc *, Value &result,
Value val, Value param);
/**
The ValueCalc class is used to perform all sorts of calculations.
No other means of calculation should be performed, to achieve
transparency, and to ease addition of new datatypes.
Currently, most functions simply convert data to double and work with that.
The idea is such that after we add support for bigger precision, we only need
to adjust this class and the parsing/formatting/converting classes. All
function implementations will remain exactly the same.
Of course, for some functions, it might be impossible to apply them
on all datatypes, but since all of them can be applied on both
doubles and GnuMP-based numbers, that is not of much concern ;)
*/
class ValueCalc {
public:
ValueCalc (ValueConverter* c);
ValueConverter *conv () { return converter; };
// some functions need access to the document
void setDoc (Doc *d) { _doc = d; };
Doc *doc () { return _doc; };
/** basic arithmetic operations */
Value add (const Value &a, const Value &b);
Value sub (const Value &a, const Value &b);
Value mul (const Value &a, const Value &b);
Value div (const Value &a, const Value &b);
Value mod (const Value &a, const Value &b);
Value pow (const Value &a, const Value &b);
Value sqr (const Value &a);
Value sqrt (const Value &a);
Value add (const Value &a, double b);
Value sub (const Value &a, double b);
Value mul (const Value &a, double b);
Value div (const Value &a, double b);
Value pow (const Value &a, double b);
Value abs (const Value &a);
/** comparison and related */
bool isZero (const Value &a);
bool isEven (const Value &a);
/** numerical comparison */
bool equal (const Value &a, const Value &b);
/** numerical comparison with a little epsilon tolerance */
bool approxEqual (const Value &a, const Value &b);
/** numerical comparison */
bool greater (const Value &a, const Value &b);
/** numerical comparison - greater or equal */
bool gequal (const Value &a, const Value &b);
/** numerical comparison */
bool lower (const Value &a, const Value &b);
/** string comparison */
bool strEqual (const Value &a, const Value &b);
int sign (const Value &a);
/** rounding */
Value roundDown (const Value &a, const Value &digits);
Value roundUp (const Value &a, const Value &digits);
Value round (const Value &a, const Value &digits);
Value roundDown (const Value &a, int digits = 0);
Value roundUp (const Value &a, int digits = 0);
Value round (const Value &a, int digits = 0);
/** logarithms and exponentials */
Value log (const Value &number, const Value &base);
Value log (const Value &number, double base = 10);
Value ln (const Value &number);
Value exp (const Value &number);
/** constants */
Value pi ();
Value eps ();
/** random number from <0.0, range) */
Value random (double range = 1.0);
Value random (Value range);
/** some computational functions */
Value fact (const Value &which);
Value fact (const Value &which, const Value &end);
Value fact (int which, int end = 0);
/** double factorial (every other number multiplied) */
Value factDouble (int which);
Value factDouble (Value which);
/** combinations */
Value combin (int n, int k);
Value combin (Value n, Value k);
/** greatest common divisor */
Value gcd (const Value &a, const Value &b);
/** lowest common multiplicator */
Value lcm (const Value &a, const Value &b);
/** base conversion 10 -> base */
Value base (const Value &val, int base = 16, int prec = 0);
/** base conversion base -> 10 */
Value fromBase (const Value &val, int base = 16);
/** goniometric functions */
Value sin (const Value &number);
Value cos (const Value &number);
Value tg (const Value &number);
Value cotg (const Value &number);
Value asin (const Value &number);
Value acos (const Value &number);
Value atg (const Value &number);
Value atan2 (const Value &y, const Value &x);
/** hyperbolic functions */
Value sinh (const Value &number);
Value cosh (const Value &number);
Value tgh (const Value &number);
Value asinh (const Value &number);
Value acosh (const Value &number);
Value atgh (const Value &number);
/** some statistical stuff
TODO: we may want to move these over to a separate class or something,
as the functions are mostly big */
Value phi (Value x);
Value gauss (Value xx);
Value gaussinv (Value xx);
Value GetGamma (Value _x);
Value GetLogGamma (Value _x);
Value GetGammaDist (Value _x, Value _alpha,
Value _beta);
Value GetBeta (Value _x, Value _alpha,
Value _beta);
/** bessel functions - may also end up being separated from here */
Value besseli (Value v, Value x);
Value besselj (Value v, Value x);
Value besselk (Value v, Value x);
Value besseln (Value v, Value x);
/** error functions (see: man erf) */
Value erf (Value x);
Value erfc (Value x);
/** array/range walking */
void arrayWalk (const Value &range, Value &res,
arrayWalkFunc func, Value param);
/** Walk the array in function-like style.
This method is here to avoid duplication in function handlers. */
void arrayWalk (TQValueVector<Value> &range, Value &res,
arrayWalkFunc func, Value param);
void twoArrayWalk (const Value &a1, const Value &a2,
Value &res, arrayWalkFunc func);
void twoArrayWalk (TQValueVector<Value> &a1,
TQValueVector<Value> &a2, Value &res, arrayWalkFunc func);
arrayWalkFunc awFunc (const TQString &name);
void registerAwFunc (const TQString &name, arrayWalkFunc func);
/** basic range functions */
// if full is true, A-version is used (means string/bool values included)
Value sum (const Value &range, bool full = true);
Value sumsq (const Value &range, bool full = true);
Value sumIf (const Value &range,
const Value &checkRange, const Condition &cond);
int count (const Value &range, bool full = true);
int countIf (const Value &range, const Condition &cond);
Value avg (const Value &range, bool full = true);
Value max (const Value &range, bool full = true);
Value min (const Value &range, bool full = true);
Value product (const Value &range, Value init,
bool full = true);
Value stddev (const Value &range, bool full = true);
Value stddev (const Value &range, Value avg,
bool full = true);
Value stddevP (const Value &range, bool full = true);
Value stddevP (const Value &range, Value avg,
bool full = true);
/** range functions using value lists */
Value sum (TQValueVector<Value> range, bool full = true);
int count (TQValueVector<Value> range, bool full = true);
Value avg (TQValueVector<Value> range, bool full = true);
Value max (TQValueVector<Value> range, bool full = true);
Value min (TQValueVector<Value> range, bool full = true);
Value product (TQValueVector<Value> range, Value init,
bool full = true);
Value stddev (TQValueVector<Value> range, bool full = true);
Value stddev (TQValueVector<Value> range, Value avg,
bool full = true);
Value stddevP (TQValueVector<Value> range, bool full = true);
Value stddevP (TQValueVector<Value> range, Value avg,
bool full = true);
/**
This method parses the condition in string text to the condition cond.
It sets the condition's type and value.
*/
void getCond (Condition &cond, Value val);
/**
Returns true if value d matches the condition cond, built with getCond().
Otherwise, it returns false.
*/
bool matches (const Condition &cond, Value d);
protected:
ValueConverter* converter;
/** return result formatting, based on these two values */
Value::Format format (Value::Format a, Value::Format b);
Doc *_doc;
/** registered array-walk functions */
std::map<TQString, arrayWalkFunc> awFuncs;
};
} //namespace KSpread
#endif // KSPREAD_VALUECALC
|