summaryrefslogtreecommitdiffstats
path: root/kcalc/knumber/knumber_priv.h
blob: 7dd58a6d77ccbfc94c579a08951ea369b6dbc99f (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* 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_PRIV_H
#define _KNUMBER_PRIV_H

class QString;

#include <cstdio>
#include <gmp.h>

// work-around for pre-C99-libs
#ifndef INFINITY
#define INFINITY  HUGE_VAL
#endif
// this is really ugly
#ifndef NAN
#define NAN (atof("nan"))
#endif

class _knumber
{
 public:
  enum NumType {SpecialType, IntegerType, FractionType, FloatType};
  enum ErrorType {UndefinedNumber, Infinity, MinusInfinity};

  _knumber() {}

  virtual ~_knumber() {}

  virtual void copy(_knumber const & num) = 0;

  virtual NumType type(void) const = 0;

  virtual QString const ascii(int prec = -1) const = 0;

  virtual _knumber * abs(void) const = 0;
  virtual _knumber * intPart(void) const = 0;
  virtual int sign(void) const = 0;
  virtual _knumber * sqrt(void) const = 0;
  virtual _knumber * cbrt(void) const = 0;
  virtual _knumber * change_sign(void) const = 0;
  virtual _knumber * reciprocal(void) const = 0;
  virtual _knumber * add(_knumber const & arg2) const = 0;
  virtual _knumber * multiply(_knumber const & arg2) const = 0;
  _knumber * divide(_knumber const & arg2) const;

  virtual _knumber * power(_knumber const & exponent) const = 0;

  virtual int compare(_knumber const &arg2) const = 0;

  virtual operator signed long int (void) const = 0;
  virtual operator unsigned long int (void) const = 0;
  virtual operator double (void) const = 0;
};



class _knumerror : public _knumber
{
 public:
  _knumerror(ErrorType error = UndefinedNumber)
    : _error(error) { }
  
  _knumerror(_knumber const & num);

  _knumerror(const QString & num);

  //virtual ~_knumerror() { }

  _knumerror const & operator = (_knumerror const & num);

  virtual void copy(_knumber const & num)
  {
    _error = dynamic_cast<_knumerror const &>(num)._error;
  }

  virtual NumType type(void) const {return SpecialType;}

  virtual QString const ascii(int prec = -1) const;

  virtual _knumber * abs(void) const;
  virtual _knumber * intPart(void) const;
  virtual int sign(void) const;
  virtual _knumber * cbrt(void) const;
  virtual _knumber * sqrt(void) const;
  virtual _knumber * change_sign(void) const;
  virtual _knumber * reciprocal(void) const;
  virtual _knumber * add(_knumber const & arg2) const;
  virtual _knumber * multiply(_knumber const & arg2) const;

  virtual _knumber * power(_knumber const & exponent) const;

  virtual int compare(_knumber const &arg2) const;

  virtual operator signed long int (void) const;
  virtual operator unsigned long int (void) const;
  virtual operator double (void) const;

 private:

  ErrorType _error;

  friend class _knuminteger;
  friend class _knumfraction;
  friend class _knumfloat;
};



class _knuminteger : public _knumber
{
 public:
  _knuminteger(signed int num = 0)
  {
    mpz_init_set_si(_mpz, num);
  }

  _knuminteger(unsigned int num)
  {
    mpz_init_set_ui(_mpz, num);
  }

  _knuminteger(signed long int num)
  {
    mpz_init_set_si(_mpz, num);
  }

  _knuminteger(unsigned long int num)
  {
    mpz_init_set_ui(_mpz, num);
  }
  
  _knuminteger(unsigned long long int num);

  _knuminteger(_knumber const & num);

  _knuminteger(const QString & num);

  virtual ~_knuminteger()
  {
    mpz_clear(_mpz);
  }

  _knuminteger const & operator = (_knuminteger const & num);

  virtual void copy(_knumber const & num)
  {
    mpz_set(_mpz, dynamic_cast<_knuminteger const &>(num)._mpz);
  }

  virtual NumType type(void) const {return IntegerType;}

  virtual QString const ascii(int prec = -1) const;

  virtual _knumber * abs(void) const;
  virtual _knumber * intPart(void) const;
  virtual int sign(void) const;
  virtual _knumber * cbrt(void) const;
  virtual _knumber * sqrt(void) const;
  virtual _knumber * change_sign(void) const;
  virtual _knumber * reciprocal(void) const;
  virtual _knumber * add(_knumber const & arg2) const;
  virtual _knumber * multiply(_knumber const & arg2) const;

  virtual int compare(_knumber const &arg2) const;

  virtual _knumber * power(_knumber const & exponent) const;

  virtual operator signed long int (void) const;
  virtual operator unsigned long int (void) const;
  virtual operator double (void) const;

  _knuminteger * intAnd(_knuminteger const &arg2) const;
  _knuminteger * intOr(_knuminteger const &arg2) const;
  _knumber * mod(_knuminteger const &arg2) const;
  _knumber * shift(_knuminteger const &arg2) const;
  
 private:
  mpz_t _mpz;

  friend class _knumfraction;
  friend class _knumfloat;
};



class _knumfraction : public _knumber
{
 public:
  
  _knumfraction(signed long int nom = 0, signed long int denom = 1)
  {
    mpq_init(_mpq);
    mpq_set_si(_mpq, nom, denom);
    mpq_canonicalize(_mpq);
  }

  _knumfraction(_knumber const & num);
  
  _knumfraction(QString const & num);

  virtual ~_knumfraction()
  {
    mpq_clear(_mpq);
  }
  
  virtual void copy(_knumber const & num)
  {
    mpq_set(_mpq, dynamic_cast<_knumfraction const &>(num)._mpq);
  }

  virtual NumType type(void) const {return FractionType;}

  virtual QString const ascii(int prec = -1) const;
  
  bool isInteger(void) const;

  virtual _knumber * abs(void) const;
  virtual _knumber * intPart(void) const;
  virtual int sign(void) const;
  virtual _knumber * cbrt(void) const;
  virtual _knumber * sqrt(void) const;
  virtual _knumber * change_sign(void) const;
  virtual _knumber * reciprocal(void) const;
  virtual _knumber * add(_knumber const & arg2) const;
  virtual _knumber * multiply(_knumber const & arg2) const;

  virtual _knumber * power(_knumber const & exponent) const;
  
  virtual int compare(_knumber const &arg2) const;

  virtual operator signed long int (void) const;
  virtual operator unsigned long int (void) const;
  virtual operator double (void) const;

 private:
  mpq_t _mpq;

  friend class _knuminteger;
  friend class _knumfloat;
};

class _knumfloat : public _knumber
{
 public:
  _knumfloat(double num = 1.0)
  {
    mpf_init(_mpf);
    mpf_set_d(_mpf, num);
  }
  
  _knumfloat(_knumber const & num);

  _knumfloat(QString const & num);
  
  virtual ~_knumfloat()
  {
    mpf_clear(_mpf);
  }

  virtual void copy(_knumber const & num)
  {
    mpf_set(_mpf, dynamic_cast<_knumfloat const &>(num)._mpf);
  }

  virtual NumType type(void) const {return FloatType;}

  virtual QString const ascii(int prec = -1) const;

  virtual _knumber * abs(void) const;
  virtual _knumber * intPart(void) const;
  virtual int sign(void) const;
  virtual _knumber * cbrt(void) const;
  virtual _knumber * sqrt(void) const;
  virtual _knumber * change_sign(void) const;
  virtual _knumber * reciprocal(void) const;
  virtual _knumber * add(_knumber const & arg2) const;
  virtual _knumber * multiply(_knumber const & arg2) const;
  virtual _knumber * divide(_knumber const & arg2) const;

  virtual _knumber * power(_knumber const & exponent) const;

  virtual int compare(_knumber const &arg2) const;

  virtual operator signed long int (void) const;
  virtual operator unsigned long int (void) const;
  virtual operator double (void) const;

 private:
    mpf_t _mpf;

  friend class _knuminteger;
  friend class _knumfraction;
};


#endif //  _KNUMBER_PRIV_H