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 |
388 | |