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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
// -*- c-basic-offset: 2 -*-
/* This file is part of the KDE libraries
Copyright (C) 2005 Klaus Niederkrueger <kniederk@math.uni-koeln.de>
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 _KNUMBER_H
#define _KNUMBER_H
#include <kdelibs_export.h>
#include "knumber_priv.h"
class QString;
/**
*
* @short Class that provides arbitrary precision numbers
*
* KNumber provides access to arbitrary precision numbers from within
* KDE.
*
* KNumber is based on the GMP (GNU Multiprecision) library and
* provides transparent support to integer, fractional and floating
* point number. It contains rudimentary error handling, and also
* includes methods for converting the numbers to QStrings for
* output, and to read QStrings to obtain a KNumber.
*
* The different types of numbers that can be represented by objects
* of this class will be described below:
*
* @li @p NumType::SpecialType - This type represents an error that
* has occurred, e.g. trying to divide 1 by 0 gives an object that
* represents infinity.
*
* @li @p NumType::IntegerType - The number is an integer. It can be
* arbitrarily large (restricted by the memory of the system).
*
* @li @p NumType::FractionType - A fraction is a number of the form
* denominator divided by nominator, where both denominator and
* nominator are integers of arbitrary size.
*
* @li @p NumType::FloatType - The number is of floating point
* type. These numbers are usually rounded, so that they do not
* represent precise values.
*
*
* @author Klaus Niederkrueger <kniederk@math.uni-koeln.de>
*/
class KDE_EXPORT KNumber
{
public:
static KNumber const Zero;
static KNumber const One;
static KNumber const MinusOne;
static KNumber const Pi;
static KNumber const Euler;
static KNumber const NotDefined;
/**
* KNumber tries to provide transparent access to the following type
* of numbers:
*
* @li @p NumType::SpecialType - Some type of error has occurred,
* further inspection with @p KNumber::ErrorType
*
* @li @p NumType::IntegerType - the number is an integer
*
* @li @p NumType::FractionType - the number is a fraction
*
* @li @p NumType::FloatType - the number is of floating point type
*
*/
enum NumType {SpecialType, IntegerType, FractionType, FloatType};
/**
* A KNumber that represents an error, i.e. that is of type @p
* NumType::SpecialType can further distinguished:
*
* @li @p ErrorType::UndefinedNumber - This is e.g. the result of
* taking the square root of a negative number or computing
* \f$ \infty - \infty \f$.
*
* @li @p ErrorType::Infinity - Such a number can be e.g. obtained
* by dividing 1 by 0. Some further calculations are still allowed,
* e.g. \f$ \infty + 5 \f$ still gives \f$\infty\f$.
*
* @li @p ErrorType::MinusInfinity - MinusInfinity behaves similarly
* to infinity above. It can be obtained by changing the sign of
* infinity.
*
*/
enum ErrorType {UndefinedNumber, Infinity, MinusInfinity};
KNumber(signed int num = 0);
KNumber(unsigned int num);
KNumber(signed long int num);
KNumber(unsigned long int num);
KNumber(unsigned long long int num);
KNumber(double num);
KNumber(KNumber const & num);
KNumber(TQString const & num);
~KNumber()
{
delete _num;
}
KNumber const & operator=(KNumber const & num);
/**
* Returns the type of the number, as explained in @p KNumber::NumType.
*/
NumType type(void) const;
/**
* Set whether the output of numbers (with KNumber::toQString)
* should happen as floating point numbers or not. This method has
* in fact only an effect on numbers of type @p
* NumType::FractionType, which can be either displayed as fractions
* or in decimal notation.
*
* The default behavior is not to display fractions in floating
* point notation.
*/
static void setDefaultFloatOutput(bool flag);
/**
* Set whether a number constructed from a TQString should be
* initialized as a fraction or as a float, e.g. "1.01" would be
* treated as 101/100, if this flag is set to true.
*
* The default setting is false.
*/
static void setDefaultFractionalInput(bool flag);
/**
* Set the default precision to be *at least* @p prec (decimal)
* digits. All subsequent initialized floats will use at least this
* precision, but previously initialized variables are unaffected.
*/
static void setDefaultFloatPrecision(unsigned int prec);
/**
* What a terrible method name!! When displaying a fraction, the
* default mode gives @p "nomin/denom". With this method one can
* choose to display a fraction as @p "integer nomin/denom".
*
* Examples: Default representation mode is 47/17, but if @p flag is
* @p true, then the result is 2 13/17.
*/
static void setSplitoffIntegerForFractionOutput(bool flag);
/**
* Return a TQString representing the KNumber.
*
* @param width This number specifies the maximal length of the
* output, before the method switches to exponential notation and
* does rounding. For negative numbers, this option is ignored.
*
* @param prec This parameter controls the number of digits
* following the decimal point. For negative numbers, this option
* is ignored.
*
*/
TQString const toQString(int width = -1, int prec = -1) const;
/**
* Compute the absolute value, i.e. @p x.abs() returns the value
*
* \f[ \left\{\begin{array}{cl} x, & x \ge 0 \\ -x, & x <
* 0\end{array}\right.\f]
* This method works for \f$ x = \infty \f$ and \f$ x = -\infty \f$.
*/
KNumber const abs(void) const;
/**
* Compute the square root. If \f$ x < 0 \f$ (including \f$
* x=-\infty \f$), then @p x.sqrt() returns @p
* ErrorType::UndefinedNumber.
*
* If @p x is an integer or a fraction, then @p x.sqrt() tries to
* compute the exact square root. If the square root is not a
* fraction, then a float with the default precision is returned.
*
* This method works for \f$ x = \infty \f$ giving \f$ \infty \f$.
*/
KNumber const sqrt(void) const;
/**
* Compute the cube root.
*
* If @p x is an integer or a fraction, then @p x.cbrt() tries to
* compute the exact cube root. If the cube root is not a fraction,
* then a float is returned, but
*
* WARNING: A float cube root is computed as a standard @p double
* that is later transformed back into a @p KNumber.
*
* This method works for \f$ x = \infty \f$ giving \f$ \infty \f$,
* and for \f$ x = -\infty \f$ giving \f$ -\infty \f$.
*/
KNumber const cbrt(void) const;
/**
* Truncates a @p KNumber to its integer type returning a number of
* type @p NumType::IntegerType.
*
* If \f$ x = \pm\infty \f$, integerPart leaves the value unchanged,
* i.e. it returns \f$ \pm\infty \f$.
*/
KNumber const integerPart(void) const;
KNumber const power(KNumber const &exp) const;
KNumber const operator+(KNumber const & arg2) const;
KNumber const operator -(void) const;
KNumber const operator-(KNumber const & arg2) const;
KNumber const operator*(KNumber const & arg2) const;
KNumber const operator/(KNumber const & arg2) const;
KNumber const operator%(KNumber const & arg2) const;
KNumber const operator&(KNumber const & arg2) const;
KNumber const operator|(KNumber const & arg2) const;
KNumber const operator<<(KNumber const & arg2) const;
KNumber const operator>>(KNumber const & arg2) const;
operator bool(void) const;
operator signed long int(void) const;
operator unsigned long int(void) const;
operator unsigned long long int(void) const;
operator double(void) const;
bool const operator==(KNumber const & arg2) const
{ return (compare(arg2) == 0); }
bool const operator!=(KNumber const & arg2) const
{ return (compare(arg2) != 0); }
bool const operator>(KNumber const & arg2) const
{ return (compare(arg2) > 0); }
bool const operator<(KNumber const & arg2) const
{ return (compare(arg2) < 0); }
bool const operator>=(KNumber const & arg2) const
{ return (compare(arg2) >= 0); }
bool const operator<=(KNumber const & arg2) const
{ return (compare(arg2) <= 0); }
KNumber & operator +=(KNumber const &arg);
KNumber & operator -=(KNumber const &arg);
//KNumber const toFloat(void) const;
private:
void simplifyRational(void);
int const compare(KNumber const & arg2) const;
_knumber *_num;
static bool _float_output;
static bool _fraction_input;
static bool _splitoffinteger_output;
};
#endif // _KNUMBER_H
|