libqalculate-ruby 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,387 @@
1
+ /*
2
+ Qalculate
3
+
4
+ Copyright (C) 2004-2007 Niklas Knutsson (nq@altern.org)
5
+
6
+ This program is free software; you can redistribute it and/or modify
7
+ it under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation; either version 2 of the License, or
9
+ (at your option) any later version.
10
+ */
11
+
12
+ #ifndef NUMBER_H
13
+ #define NUMBER_H
14
+
15
+ #include <libqalculate/includes.h>
16
+
17
+ #include <cln/cln.h>
18
+
19
+ /** @file */
20
+
21
+ #define EQUALS_PRECISION_DEFAULT -1
22
+ #define EQUALS_PRECISION_LOWEST -2
23
+ #define EQUALS_PRECISION_HIGHEST -3
24
+
25
+
26
+ /// A number.
27
+ /**
28
+ * Can be rational, floating point, complex or infinite.
29
+ * Has arbitrary precision (uses Calculator::precision()) and infinitely large rational numbers.
30
+ * Implimented using CLN numbers.
31
+ */
32
+ class Number {
33
+
34
+ private:
35
+
36
+ protected:
37
+
38
+ void removeFloatZeroPart();
39
+ void testApproximate();
40
+ void testInteger();
41
+ void setPrecisionAndApproximateFrom(const Number &o);
42
+
43
+ cln::cl_N value;
44
+ bool b_inf, b_pinf, b_minf;
45
+ bool b_approx;
46
+ int i_precision;
47
+
48
+ public:
49
+
50
+ /**
51
+ * Constructs a number initialized as zero.
52
+ */
53
+ Number();
54
+ /**
55
+ * Constructs a number parsing a text string.
56
+ *
57
+ * @param number Text string to read number from.
58
+ * @param po Options for parsing the text string.
59
+ */
60
+ Number(string number, const ParseOptions &po = default_parse_options);
61
+ /**
62
+ * Constructs a rational number.
63
+ *
64
+ * @param numerator
65
+ * @param denominator
66
+ * @param exp_10
67
+ */
68
+ Number(int numerator, int denominator = 1, int exp_10 = 0);
69
+ /**
70
+ * Constructs a copy of a number.
71
+ */
72
+ Number(const Number &o);
73
+ virtual ~Number();
74
+
75
+ void set(string number, const ParseOptions &po = default_parse_options);
76
+ void set(int numerator, int denominator = 1, int exp_10 = 0);
77
+ void setInfinity();
78
+ void setPlusInfinity();
79
+ void setMinusInfinity();
80
+ void setFloat(double d_value);
81
+
82
+ void setInternal(const cln::cl_N &cln_value);
83
+
84
+ void setImaginaryPart(const Number &o);
85
+ void setImaginaryPart(int numerator, int denominator = 1, int exp_10 = 0);
86
+ void set(const Number &o);
87
+ void clear();
88
+
89
+ const cln::cl_N &internalNumber() const;
90
+
91
+ double floatValue() const;
92
+ /**
93
+ * Converts a number to an integer. If the number does not represent an integer it will rounded using round().
94
+ *
95
+ * @param[out] overflow If overflow is non-null it will be set to true if the number was to large to fit in an int.
96
+ * @return Resulting integer.
97
+ */
98
+ int intValue(bool *overflow = NULL) const;
99
+
100
+ /** Returns true if the number is approximate.
101
+ *
102
+ * @return true if the number is approximate.
103
+ */
104
+ bool isApproximate() const;
105
+ /** Returns true if the number has an approximate representation/is of approximate type -- if it is a floating point number. Numbers of approximate type are always approximate, but the reversed relation is not always true.
106
+ *
107
+ * @return true if the number has an approximate representation.
108
+ */
109
+ bool isApproximateType() const;
110
+ /** Defines the number as approximate or exact. If a number of approximate type is set as exact, it will be converted to a rational number.
111
+ *
112
+ * @param is_approximate If the number shall be regarded as approximate.
113
+ */
114
+ void setApproximate(bool is_approximate = true);
115
+
116
+ /** Returns the.precision of the number.
117
+ *
118
+ * @return Precision of the number or -1 if the number is exact or the precision has not been set.
119
+ */
120
+ int precision() const;
121
+ void setPrecision(int prec);
122
+
123
+ bool isUndefined() const;
124
+ /** Returns true if the number is infinity, plus infinity or minus infinity.
125
+ *
126
+ * @return true if the number is infinite.
127
+ */
128
+ bool isInfinite() const;
129
+ /** Returns true if the number is infinity, if the number is plus or minus infinity (which is not known).
130
+ *
131
+ * @return true if the number is infinity.
132
+ */
133
+ bool isInfinity() const;
134
+ /** Returns true if the number is plus infinity.
135
+ *
136
+ * @return true if the number is plus infinity.
137
+ */
138
+ bool isPlusInfinity() const;
139
+ /** Returns true if the number is minus infinity.
140
+ *
141
+ * @return true if the number is minus infinity.
142
+ */
143
+ bool isMinusInfinity() const;
144
+
145
+ /** Returns the real part of the number if it is complex, or a copy if it is real.
146
+ *
147
+ * @return true if the real part of a complex number.
148
+ */
149
+ Number realPart() const;
150
+ /** Returns the imaginary part as real number of the number if it is complex, or zero if it is real.
151
+ *
152
+ * @return true if the imaginary part of a complex number.
153
+ */
154
+ Number imaginaryPart() const;
155
+ Number numerator() const;
156
+ Number denominator() const;
157
+ Number complexNumerator() const;
158
+ Number complexDenominator() const;
159
+
160
+ void operator = (const Number &o);
161
+ void operator -- (int);
162
+ void operator ++ (int);
163
+ Number operator - () const;
164
+ Number operator * (const Number &o) const;
165
+ Number operator / (const Number &o) const;
166
+ Number operator + (const Number &o) const;
167
+ Number operator - (const Number &o) const;
168
+ Number operator ^ (const Number &o) const;
169
+ Number operator && (const Number &o) const;
170
+ Number operator || (const Number &o) const;
171
+ Number operator ! () const;
172
+
173
+ void operator *= (const Number &o);
174
+ void operator /= (const Number &o);
175
+ void operator += (const Number &o);
176
+ void operator -= (const Number &o);
177
+ void operator ^= (const Number &o);
178
+
179
+ bool operator == (const Number &o) const;
180
+ bool operator != (const Number &o) const;
181
+
182
+ bool bitAnd(const Number &o);
183
+ bool bitOr(const Number &o);
184
+ bool bitXor(const Number &o);
185
+ bool bitNot();
186
+ bool bitEqv(const Number &o);
187
+ bool shiftLeft(const Number &o);
188
+ bool shiftRight(const Number &o);
189
+ bool shift(const Number &o);
190
+
191
+ bool hasRealPart() const;
192
+ bool hasImaginaryPart() const;
193
+ bool isComplex() const;
194
+ bool isInteger() const;
195
+ Number integer() const;
196
+ bool isRational() const;
197
+ bool isReal() const;
198
+ bool isFraction() const;
199
+ bool isZero() const;
200
+ bool isOne() const;
201
+ bool isTwo() const;
202
+ bool isI() const;
203
+ bool isMinusI() const;
204
+ bool isMinusOne() const;
205
+ bool isNegative() const;
206
+ bool isNonNegative() const;
207
+ bool isPositive() const;
208
+ bool isNonPositive() const;
209
+ bool realPartIsNegative() const;
210
+ bool realPartIsPositive() const;
211
+ bool imaginaryPartIsNegative() const;
212
+ bool imaginaryPartIsPositive() const;
213
+ bool hasNegativeSign() const;
214
+ bool hasPositiveSign() const;
215
+ bool equalsZero() const;
216
+ bool equals(const Number &o) const;
217
+ bool equalsApproximately(const Number &o, int prec) const;
218
+ ComparisonResult compare(const Number &o) const;
219
+ ComparisonResult compareApproximately(const Number &o, int prec = EQUALS_PRECISION_LOWEST) const;
220
+ ComparisonResult compareImaginaryParts(const Number &o) const;
221
+ ComparisonResult compareRealParts(const Number &o) const;
222
+ bool isGreaterThan(const Number &o) const;
223
+ bool isLessThan(const Number &o) const;
224
+ bool isGreaterThanOrEqualTo(const Number &o) const;
225
+ bool isLessThanOrEqualTo(const Number &o) const;
226
+ bool isEven() const;
227
+ bool denominatorIsEven() const;
228
+ bool denominatorIsTwo() const;
229
+ bool numeratorIsEven() const;
230
+ bool numeratorIsOne() const;
231
+ bool numeratorIsMinusOne() const;
232
+ bool isOdd() const;
233
+
234
+ int integerLength() const;
235
+
236
+ /** Add to the number (x+o).
237
+ *
238
+ * @param o Number to add.
239
+ * @return true if the operation was successful.
240
+ */
241
+ bool add(const Number &o);
242
+ /** Subtracts from to the number (x-o).
243
+ *
244
+ * @param o Number to subtract.
245
+ * @return true if the operation was successful.
246
+ */
247
+ bool subtract(const Number &o);
248
+ /** Multiply the number (x*o).
249
+ *
250
+ * @param o Number to multiply with.
251
+ * @return true if the operation was successful.
252
+ */
253
+ bool multiply(const Number &o);
254
+ /** Divide the number (x/o).
255
+ *
256
+ * @param o Number to divide by.
257
+ * @return true if the operation was successful.
258
+ */
259
+ bool divide(const Number &o);
260
+ /** Invert the number (1/x).
261
+ *
262
+ * @return true if the operation was successful.
263
+ */
264
+ bool recip();
265
+ /** Raise the number (x^o).
266
+ *
267
+ * @param o Number to raise to.
268
+ * @param try_exact If an exact solution should be tried first (might be slow).
269
+ * @return true if the operation was successful.
270
+ */
271
+ bool raise(const Number &o, bool try_exact = true);
272
+ /** Multiply the number with a power of ten (x*10^o).
273
+ *
274
+ * @param o Number to raise 10 by.
275
+ * @return true if the operation was successful.
276
+ */
277
+ bool exp10(const Number &o);
278
+ /** Multiply the number with a power of two (x*2^o).
279
+ *
280
+ * @param o Number to raise 2 by.
281
+ * @return true if the operation was successful.
282
+ */
283
+ bool exp2(const Number &o);
284
+ /** Set the number to ten raised by the number (10^x).
285
+ *
286
+ * @return true if the operation was successful.
287
+ */
288
+ bool exp10();
289
+ /** Set the number to two raised by the number (2^x).
290
+ *
291
+ * @return true if the operation was successful.
292
+ */
293
+ bool exp2();
294
+ /** Raise the number by two (x^2).
295
+ *
296
+ * @return true if the operation was successful.
297
+ */
298
+ bool square();
299
+
300
+ /** Negate the number (-x).
301
+ *
302
+ * @return true if the operation was successful.
303
+ */
304
+ bool negate();
305
+ void setNegative(bool is_negative);
306
+ bool abs();
307
+ bool signum();
308
+ bool round(const Number &o);
309
+ bool floor(const Number &o);
310
+ bool ceil(const Number &o);
311
+ bool trunc(const Number &o);
312
+ bool mod(const Number &o);
313
+ bool isqrt();
314
+ bool round();
315
+ bool floor();
316
+ bool ceil();
317
+ bool trunc();
318
+ bool frac();
319
+ bool rem(const Number &o);
320
+
321
+ bool smod(const Number &o);
322
+ bool irem(const Number &o);
323
+ bool irem(const Number &o, Number &q);
324
+ bool iquo(const Number &o);
325
+ bool iquo(const Number &o, Number &r);
326
+
327
+ int getBoolean() const;
328
+ void toBoolean();
329
+ void setTrue(bool is_true = true);
330
+ void setFalse();
331
+ void setLogicalNot();
332
+
333
+ /** Set the number to e, the base of natural logarithm, calculated with the current default precision.
334
+ */
335
+ void e();
336
+ /** Set the number to pi, Archimede's constant, calculated with the current default precision.
337
+ */
338
+ void pi();
339
+ /** Set the number to Catalan's constant, calculated with the current default precision.
340
+ */
341
+ void catalan();
342
+ /** Set the number to Euler's constant, calculated with the current default precision.
343
+ */
344
+ void euler();
345
+ /** Set the number to Riemann's zeta with the number as integral point. The number must be an integer greater than one.
346
+ *
347
+ * @return true if the calculation was successful.
348
+ */
349
+ bool zeta();
350
+
351
+ bool sin();
352
+ bool asin();
353
+ bool sinh();
354
+ bool asinh();
355
+ bool cos();
356
+ bool acos();
357
+ bool cosh();
358
+ bool acosh();
359
+ bool tan();
360
+ bool atan();
361
+ bool tanh();
362
+ bool atanh();
363
+ bool ln();
364
+ bool log(const Number &o);
365
+ bool exp();
366
+ bool lambertW();
367
+ bool gcd(const Number &o);
368
+ bool lcm(const Number &o);
369
+
370
+ bool factorial();
371
+ bool multiFactorial(const Number &o);
372
+ bool doubleFactorial();
373
+ bool binomial(const Number &m, const Number &k);
374
+ bool factorize(vector<Number> &factors);
375
+
376
+ bool add(const Number &o, MathOperation op);
377
+
378
+ string printNumerator(int base = 10, bool display_sign = true, BaseDisplay base_display = BASE_DISPLAY_NORMAL, bool lower_case = false) const;
379
+ string printDenominator(int base = 10, bool display_sign = true, BaseDisplay base_display = BASE_DISPLAY_NORMAL, bool lower_case = false) const;
380
+ string printImaginaryNumerator(int base = 10, bool display_sign = true, BaseDisplay base_display = BASE_DISPLAY_NORMAL, bool lower_case = false) const;
381
+ string printImaginaryDenominator(int base = 10, bool display_sign = true, BaseDisplay base_display = BASE_DISPLAY_NORMAL, bool lower_case = false) const;
382
+
383
+ string print(const PrintOptions &po = default_print_options, const InternalPrintStruct &ips = top_ips) const;
384
+
385
+ };
386
+
387
+ #endif
@@ -0,0 +1,219 @@
1
+ /*
2
+ Qalculate
3
+
4
+ Copyright (C) 2003-2006 Niklas Knutsson (nq@altern.org)
5
+
6
+ This program is free software; you can redistribute it and/or modify
7
+ it under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation; either version 2 of the License, or
9
+ (at your option) any later version.
10
+ */
11
+
12
+ #ifndef PREFIX_H
13
+ #define PREFIX_H
14
+
15
+
16
+ #include <libqalculate/includes.h>
17
+ #include <libqalculate/Number.h>
18
+
19
+ /** @file */
20
+
21
+ ///Types for prefix classes.
22
+ typedef enum {
23
+ PREFIX_DECIMAL,
24
+ PREFIX_BINARY,
25
+ PREFIX_NUMBER
26
+ } PrefixType;
27
+
28
+ ///Abstract class for prefixes.
29
+ /** A prefix is prepended to a unit to specificy a quantity multiplicator. A prefix has a numerical value which raised to the units power defines the quantity. In for example the expression "3 kilometers", meter is the unit, 3 is regular quantity, and kilo is a prefix with a value 1000, thus the example equals "3000 meters". If the unit instead had been squared, the value of the prefix would have been raised by two and the total quantity would have been 3.000.000.
30
+
31
+ Prefixes can have up to free different three names -- a long name, a short name and a short unicode name. The unicode name is an alternative to the short name that is preferred if unicode characters can be displayed. The names or used to reference the prefix in mathematical expressions and to display a prefix in a result.
32
+ */
33
+ class Prefix {
34
+ protected:
35
+ string l_name, s_name, u_name;
36
+ public:
37
+ /** Create a prefix.
38
+ *
39
+ * @param long_name Long name.
40
+ * @param short_name Short name.
41
+ * @param unicode_name Unicode name.
42
+ */
43
+ Prefix(string long_name, string short_name = "", string unicode_name = "");
44
+ virtual ~Prefix();
45
+ /** Returns the short name of the prefix.
46
+ *
47
+ * @param return_long_if_no_short If the long name shall be returned if the prefix has not got a short name (if it is empty).
48
+ * @param use_unicode If a unicode version of the name is allowed and preferred.
49
+ * @returns The short name of the prefix.
50
+ */
51
+ const string &shortName(bool return_long_if_no_short = true, bool use_unicode = false) const;
52
+ /** Returns the long name of the prefix.
53
+ *
54
+ * @param return_short_if_no_long If the short name shall be returned if the prefix has not got a long name (if it is empty).
55
+ * @param use_unicode If a unicode version of the name is allowed and preferred.
56
+ * @returns The long name of the prefix.
57
+ */
58
+ const string &longName(bool return_short_if_no_long = true, bool use_unicode = false) const;
59
+ /** Returns the unicode name of the prefix.
60
+ *
61
+ * @param return_short_if_no_uni If the short name shall be returned if the prefix has not got a unicode name (if it is empty).
62
+ * @returns The unicode name of the prefix.
63
+ */
64
+ const string &unicodeName(bool return_short_if_no_uni = true) const;
65
+ /** Sets the short name of the prefix.
66
+ *
67
+ * @param short_name The new short name for the prefix.
68
+ */
69
+ void setShortName(string short_name);
70
+ /** Sets the long name of the prefix.
71
+ *
72
+ * @param long_name The new long name for the prefix.
73
+ */
74
+ void setLongName(string long_name);
75
+ /** Sets the unicode name of the prefix. The unicode name is an alternative to the short name that is preferred if unicode characters can be displayed.
76
+ *
77
+ * @param unicode_name The new unicode name for the prefix.
78
+ */
79
+ void setUnicodeName(string unicode_name);
80
+ /** Returns a preferred name of the prefix.
81
+ *
82
+ * @param short_default If a short name is preferred.
83
+ * @param use_unicode If a unicode name is preferred.
84
+ * @param can_display_unicode_string_function Function that tests if the unicode characters in a name can be displayed. If the function returns false, the name will be rejected.
85
+ * @param can_display_unicode_string_arg Argument to pass to the above test function.
86
+ * @returns A preferred name.
87
+ */
88
+ const string &name(bool short_default = true, bool use_unicode = false, bool (*can_display_unicode_string_function) (const char*, void*) = NULL, void *can_display_unicode_string_arg = NULL) const;
89
+ /** Returns the value of the prefix.
90
+ *
91
+ * @param nexp The power of the prefixed unit.
92
+ * @returns The value of the prefix.
93
+ */
94
+ virtual Number value(const Number &nexp) const = 0;
95
+ /** Returns the value of the prefix.
96
+ *
97
+ * @param iexp The power of the prefixed unit.
98
+ * @returns The value of the prefix.
99
+ */
100
+ virtual Number value(int iexp) const = 0;
101
+ /** Returns the value of the prefix.
102
+ *
103
+ * @returns The value of the prefix.
104
+ */
105
+ virtual Number value() const = 0;
106
+ /** Returns type, subclass, of the prefix. This can be PREFIX_DECIMAL for prefixes of the class DecimalPrefix, PREFIX_BINARY for BinaryPrefix, or PREFIX_NUMBER for NumberPrefix.
107
+ *
108
+ * @returns The type of the prefix.
109
+ */
110
+ virtual int type() const = 0;
111
+
112
+ };
113
+
114
+ ///A decimal (metric) prefix.
115
+ /** A metric or decimal prefix has an integer exponent which with a base of ten constitutes the value of the prefix (value=10^exponent).
116
+ */
117
+ class DecimalPrefix : public Prefix {
118
+ protected:
119
+ int exp;
120
+ public:
121
+ /** Create a decimal prefix.
122
+ *
123
+ * @param exp10 Exponent for the value.
124
+ * @param long_name Long name.
125
+ * @param short_name Short name.
126
+ * @param unicode_name Unicode name.
127
+ */
128
+ DecimalPrefix(int exp10, string long_name, string short_name = "", string unicode_name = "");
129
+ ~DecimalPrefix();
130
+ /** Returns the exponent.
131
+ *
132
+ * @param iexp Exponent of the unit.
133
+ * @returns The exponent of the prefix.
134
+ */
135
+ int exponent(int iexp = 1) const;
136
+ /** Returns the exponent.
137
+ *
138
+ * @param nexp Exponent of the unit.
139
+ * @returns The exponent of the prefix.
140
+ */
141
+ Number exponent(const Number &nexp) const;
142
+ /** Sets the exponent of the prefix.
143
+ *
144
+ * @param iexp New exponent for the prefix.
145
+ */
146
+ void setExponent(int iexp);
147
+ Number value(const Number &nexp) const;
148
+ Number value(int iexp) const;
149
+ Number value() const;
150
+ int type() const;
151
+ };
152
+
153
+ ///A binary prefix.
154
+ /** A Binary prefix has an integer exponent which with a base of two constitutes the value of the prefix (value=2^exponent).
155
+ */
156
+ class BinaryPrefix : public Prefix {
157
+ protected:
158
+ int exp;
159
+ public:
160
+ /** Create a binary prefix.
161
+ *
162
+ * @param exp2 Exponent for the value.
163
+ * @param long_name Long name.
164
+ * @param short_name Short name.
165
+ * @param unicode_name Unicode name.
166
+ */
167
+ BinaryPrefix(int exp2, string long_name, string short_name = "", string unicode_name = "");
168
+ ~BinaryPrefix();
169
+ /** Returns the exponent.
170
+ *
171
+ * @param iexp Exponent of the unit.
172
+ * @returns The exponent of the prefix.
173
+ */
174
+ int exponent(int iexp = 1) const;
175
+ /** Returns the exponent.
176
+ *
177
+ * @param nexp Exponent of the unit.
178
+ * @returns The exponent of the prefix.
179
+ */
180
+ Number exponent(const Number &nexp) const;
181
+ /** Sets the exponent of the prefix.
182
+ *
183
+ * @param iexp New exponent for the prefix.
184
+ */
185
+ void setExponent(int iexp);
186
+ Number value(const Number &nexp) const;
187
+ Number value(int iexp) const;
188
+ Number value() const;
189
+ int type() const;
190
+ };
191
+
192
+ ///A prefix with a free numerical value.
193
+ /** A prefix without any predefined base, which can use any number.
194
+ */
195
+ class NumberPrefix : public Prefix {
196
+ protected:
197
+ Number o_number;
198
+ public:
199
+ /** Create a number prefix.
200
+ *
201
+ * @param nr Value of the prefix.
202
+ * @param long_name Long name.
203
+ * @param short_name Short name.
204
+ * @param unicode_name Unicode name.
205
+ */
206
+ NumberPrefix(const Number &nr, string long_name, string short_name = "", string unicode_name = "");
207
+ ~NumberPrefix();
208
+ /** Sets the value of the prefix.
209
+ *
210
+ * @param nr New value for the prefix.
211
+ */
212
+ void setValue(const Number &nr);
213
+ Number value(const Number &nexp) const;
214
+ Number value(int iexp) const;
215
+ Number value() const;
216
+ int type() const;
217
+ };
218
+
219
+ #endif