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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
|
/* 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.
*/
#include <math.h>
#include <config.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <qregexp.h>
#include <qstring.h>
#include "knumber_priv.h"
_knumerror::_knumerror(_knumber const & num)
{
switch(num.type()) {
case SpecialType:
_error = dynamic_cast<_knumerror const &>(num)._error;
break;
case IntegerType:
case FractionType:
case FloatType:
// What should I do here?
break;
}
}
_knuminteger::_knuminteger(unsigned long long int num)
{
mpz_init(_mpz);
#if SIZEOF_UNSIGNED_LONG == 8
mpz_init_set_ui(_mpz, static_cast<unsigned long int>(num));
#elif SIZEOF_UNSIGNED_LONG == 4
mpz_set_ui(_mpz, static_cast<unsigned long int>(num >> 32));
mpz_mul_2exp(_mpz, _mpz, 32);
mpz_add_ui(_mpz, _mpz, static_cast<unsigned long int>(num));
#else
#error "SIZEOF_UNSIGNED_LONG is a unhandled case"
#endif
}
_knuminteger::_knuminteger(_knumber const & num)
{
mpz_init(_mpz);
switch(num.type()) {
case IntegerType:
mpz_set(_mpz, dynamic_cast<_knuminteger const &>(num)._mpz);
break;
case FractionType:
case FloatType:
case SpecialType:
// What should I do here?
break;
}
}
_knumfraction::_knumfraction(_knumber const & num)
{
mpq_init(_mpq);
switch(num.type()) {
case IntegerType:
mpq_set_z(_mpq, dynamic_cast<_knuminteger const &>(num)._mpz);
break;
case FractionType:
mpq_set(_mpq, dynamic_cast<_knumfraction const &>(num)._mpq);
break;
case FloatType:
case SpecialType:
// What should I do here?
break;
}
}
_knumfloat::_knumfloat(_knumber const & num)
{
mpf_init(_mpf);
switch(num.type()) {
case IntegerType:
mpf_set_z(_mpf, dynamic_cast<_knuminteger const &>(num)._mpz);
break;
case FractionType:
mpf_set_q(_mpf, dynamic_cast<_knumfraction const &>(num)._mpq);
break;
case FloatType:
mpf_set(_mpf, dynamic_cast<_knumfloat const &>(num)._mpf);
break;
case SpecialType:
// What should I do here?
break;
}
}
_knumerror::_knumerror(QString const & num)
{
if (num == "nan")
_error = UndefinedNumber;
else if (num == "inf")
_error = Infinity;
else if (num == "-inf")
_error = MinusInfinity;
}
_knuminteger::_knuminteger(QString const & num)
{
mpz_init(_mpz);
mpz_set_str(_mpz, num.ascii(), 10);
}
_knumfraction::_knumfraction(QString const & num)
{
mpq_init(_mpq);
if (QRegExp("^[+-]?\\d+(\\.\\d*)?(e[+-]?\\d+)?$").exactMatch(num)) {
// my hand-made conversion is terrible
// first me convert the mantissa
unsigned long int digits_after_dot = ((num.section( '.', 1, 1)).section('e', 0, 0)).length();
QString tmp_num = num.section('e', 0, 0).remove('.');
mpq_set_str(_mpq, tmp_num.ascii(), 10);
mpz_t tmp_int;
mpz_init(tmp_int);
mpz_ui_pow_ui (tmp_int, 10, digits_after_dot);
mpz_mul(mpq_denref(_mpq), mpq_denref(_mpq), tmp_int);
// now we take care of the exponent
if (! (tmp_num = num.section('e', 1, 1)).isEmpty()) {
long int tmp_exp = tmp_num.toLong();
if (tmp_exp > 0) {
mpz_ui_pow_ui (tmp_int, 10,
static_cast<unsigned long int>(tmp_exp));
mpz_mul(mpq_numref(_mpq), mpq_numref(_mpq), tmp_int);
} else {
mpz_ui_pow_ui (tmp_int, 10,
static_cast<unsigned long int>(-tmp_exp));
mpz_mul(mpq_denref(_mpq), mpq_denref(_mpq), tmp_int);
}
}
mpz_clear(tmp_int);
} else
mpq_set_str(_mpq, num.ascii(), 10);
mpq_canonicalize(_mpq);
}
_knumfloat::_knumfloat(QString const & num)
{
mpf_init(_mpf);
mpf_set_str(_mpf, num.ascii(), 10);
}
_knuminteger const & _knuminteger::operator = (_knuminteger const & num)
{
if (this == &num)
return *this;
mpz_set(_mpz, num._mpz);
return *this;
}
QString const _knumerror::ascii(int prec) const
{
static_cast<void>(prec);
switch(_error) {
case UndefinedNumber:
return QString("nan");
case Infinity:
return QString("inf");
case MinusInfinity:
return QString("-inf");
default:
return QString::null;
}
}
QString const _knuminteger::ascii(int prec) const
{
static_cast<void>(prec);
char *tmp_ptr;
gmp_asprintf(&tmp_ptr, "%Zd", _mpz);
QString ret_str = tmp_ptr;
free(tmp_ptr);
return ret_str;
}
QString const _knumfraction::ascii(int prec) const
{
static_cast<void>(prec);
char *tmp_ptr = mpq_get_str(0, 10, _mpq);
QString ret_str = tmp_ptr;
free(tmp_ptr);
return ret_str;
}
QString const _knumfloat::ascii(int prec) const
{
QString ret_str;
char *tmp_ptr;
if (prec > 0)
gmp_asprintf(&tmp_ptr, ("%." + QString().setNum(prec) + "Fg").ascii(), _mpf);
else
gmp_asprintf(&tmp_ptr, "%Fg", _mpf);
ret_str = tmp_ptr;
free(tmp_ptr);
return ret_str;
}
bool _knumfraction::isInteger(void) const
{
if (mpz_cmp_ui(mpq_denref(_mpq), 1) == 0)
return true;
else
return false;
}
_knumber * _knumerror::abs(void) const
{
_knumerror * tmp_num = new _knumerror(*this);
if(_error == MinusInfinity) tmp_num->_error = Infinity;
return tmp_num;
}
_knumber * _knuminteger::abs(void) const
{
_knuminteger * tmp_num = new _knuminteger();
mpz_abs(tmp_num->_mpz, _mpz);
return tmp_num;
}
_knumber * _knumfraction::abs(void) const
{
_knumfraction * tmp_num = new _knumfraction();
mpq_abs(tmp_num->_mpq, _mpq);
return tmp_num;
}
_knumber * _knumfloat::abs(void) const
{
_knumfloat * tmp_num = new _knumfloat();
mpf_abs(tmp_num->_mpf, _mpf);
return tmp_num;
}
_knumber * _knumerror::intPart(void) const
{
return new _knumerror(*this);
}
_knumber * _knuminteger::intPart(void) const
{
_knuminteger *tmp_num = new _knuminteger();
mpz_set(tmp_num->_mpz, _mpz);
return tmp_num;
}
_knumber * _knumfraction::intPart(void) const
{
_knuminteger *tmp_num = new _knuminteger();
mpz_set_q(tmp_num->_mpz, _mpq);
return tmp_num;
}
_knumber * _knumfloat::intPart(void) const
{
_knuminteger *tmp_num = new _knuminteger();
mpz_set_f(tmp_num->_mpz, _mpf);
return tmp_num;
}
int _knumerror::sign(void) const
{
switch(_error) {
case Infinity:
return 1;
case MinusInfinity:
return -1;
default:
return 0;
}
}
int _knuminteger::sign(void) const
{
return mpz_sgn(_mpz);
}
int _knumfraction::sign(void) const
{
return mpq_sgn(_mpq);
}
int _knumfloat::sign(void) const
{
return mpf_sgn(_mpf);
}
#warning _cbrt for now this is a stupid work around
static void _cbrt(mpf_t &num)
{
double tmp_num = cbrt(mpf_get_d(num));
mpf_init_set_d(num, tmp_num);
}
_knumber * _knumerror::cbrt(void) const
{
// infty ^3 = infty; -infty^3 = -infty
_knumerror *tmp_num = new _knumerror(*this);
return tmp_num;
}
_knumber * _knuminteger::cbrt(void) const
{
_knuminteger * tmp_num = new _knuminteger();
if(mpz_root(tmp_num->_mpz, _mpz, 3))
return tmp_num; // root is perfect
delete tmp_num; // root was not perfect, result will be float
_knumfloat * tmp_num2 = new _knumfloat();
mpf_set_z(tmp_num2->_mpf, _mpz);
_cbrt(tmp_num2->_mpf);
return tmp_num2;
}
_knumber * _knumfraction::cbrt(void) const
{
_knumfraction * tmp_num = new _knumfraction();
if (mpz_root(mpq_numref(tmp_num->_mpq), mpq_numref(_mpq), 3)
&& mpz_root(mpq_denref(tmp_num->_mpq), mpq_denref(_mpq), 3))
return tmp_num; // root is perfect
delete tmp_num; // root was not perfect, result will be float
_knumfloat * tmp_num2 = new _knumfloat();
mpf_set_q(tmp_num2->_mpf, _mpq);
_cbrt(tmp_num2->_mpf);
return tmp_num2;
}
_knumber * _knumfloat::cbrt(void) const
{
_knumfloat * tmp_num = new _knumfloat(*this);
_cbrt(tmp_num->_mpf);
return tmp_num;
}
_knumber * _knumerror::sqrt(void) const
{
_knumerror *tmp_num = new _knumerror(*this);
if(_error == MinusInfinity) tmp_num->_error = UndefinedNumber;
return tmp_num;
}
_knumber * _knuminteger::sqrt(void) const
{
if (mpz_sgn(_mpz) < 0) {
_knumerror *tmp_num = new _knumerror(UndefinedNumber);
return tmp_num;
}
if (mpz_perfect_square_p(_mpz)) {
_knuminteger * tmp_num = new _knuminteger();
mpz_sqrt(tmp_num->_mpz, _mpz);
return tmp_num;
} else {
_knumfloat * tmp_num = new _knumfloat();
mpf_set_z(tmp_num->_mpf, _mpz);
mpf_sqrt(tmp_num->_mpf, tmp_num->_mpf);
return tmp_num;
}
}
_knumber * _knumfraction::sqrt(void) const
{
if (mpq_sgn(_mpq) < 0) {
_knumerror *tmp_num = new _knumerror(UndefinedNumber);
return tmp_num;
}
if (mpz_perfect_square_p(mpq_numref(_mpq))
&& mpz_perfect_square_p(mpq_denref(_mpq))) {
_knumfraction * tmp_num = new _knumfraction();
mpq_set(tmp_num->_mpq, _mpq);
mpz_sqrt(mpq_numref(tmp_num->_mpq), mpq_numref(tmp_num->_mpq));
mpz_sqrt(mpq_denref(tmp_num->_mpq), mpq_denref(tmp_num->_mpq));
return tmp_num;
} else {
_knumfloat * tmp_num = new _knumfloat();
mpf_set_q(tmp_num->_mpf, _mpq);
mpf_sqrt(tmp_num->_mpf, tmp_num->_mpf);
return tmp_num;
}
_knumfraction * tmp_num = new _knumfraction();
return tmp_num;
}
_knumber * _knumfloat::sqrt(void) const
{
if (mpf_sgn(_mpf) < 0) {
_knumerror *tmp_num = new _knumerror(UndefinedNumber);
return tmp_num;
}
_knumfloat * tmp_num = new _knumfloat();
mpf_sqrt(tmp_num->_mpf, _mpf);
return tmp_num;
}
_knumber * _knumerror::change_sign(void) const
{
_knumerror * tmp_num = new _knumerror();
if(_error == Infinity) tmp_num->_error = MinusInfinity;
if(_error == MinusInfinity) tmp_num->_error = Infinity;
return tmp_num;
}
_knumber * _knuminteger::change_sign(void) const
{
_knuminteger * tmp_num = new _knuminteger();
mpz_neg(tmp_num->_mpz, _mpz);
return tmp_num;
}
_knumber * _knumfraction::change_sign(void) const
{
_knumfraction * tmp_num = new _knumfraction();
mpq_neg(tmp_num->_mpq, _mpq);
return tmp_num;
}
_knumber *_knumfloat::change_sign(void) const
{
_knumfloat * tmp_num = new _knumfloat();
mpf_neg(tmp_num->_mpf, _mpf);
return tmp_num;
}
_knumber * _knumerror::reciprocal(void) const
{
switch(_error) {
case Infinity:
case MinusInfinity:
return new _knuminteger(0);
case UndefinedNumber:
default:
return new _knumerror(UndefinedNumber);
}
}
_knumber * _knuminteger::reciprocal(void) const
{
if(mpz_cmp_si(_mpz, 0) == 0) return new _knumerror(Infinity);
_knumfraction * tmp_num = new _knumfraction(*this);
mpq_inv(tmp_num->_mpq, tmp_num->_mpq);
return tmp_num;
}
_knumber * _knumfraction::reciprocal() const
{
if(mpq_cmp_si(_mpq, 0, 1) == 0) return new _knumerror(Infinity);
_knumfraction * tmp_num = new _knumfraction();
mpq_inv(tmp_num->_mpq, _mpq);
return tmp_num;
}
_knumber *_knumfloat::reciprocal(void) const
{
if(mpf_cmp_si(_mpf, 0) == 0) return new _knumerror(Infinity);
_knumfloat * tmp_num = new _knumfloat();
mpf_div(tmp_num->_mpf, _knumfloat("1.0")._mpf, _mpf);
return tmp_num;
}
_knumber * _knumerror::add(_knumber const & arg2) const
{
if (arg2.type() != SpecialType)
return new _knumerror(_error);
_knumerror const & tmp_arg2 = dynamic_cast<_knumerror const &>(arg2);
if (_error == UndefinedNumber
|| tmp_arg2._error == UndefinedNumber
|| (_error == Infinity && tmp_arg2._error == MinusInfinity)
|| (_error == MinusInfinity && tmp_arg2._error == Infinity)
)
return new _knumerror(UndefinedNumber);
return new _knumerror(_error);
}
_knumber * _knuminteger::add(_knumber const & arg2) const
{
if (arg2.type() != IntegerType)
return arg2.add(*this);
_knuminteger * tmp_num = new _knuminteger();
mpz_add(tmp_num->_mpz, _mpz,
dynamic_cast<_knuminteger const &>(arg2)._mpz);
return tmp_num;
}
_knumber * _knumfraction::add(_knumber const & arg2) const
{
if (arg2.type() == IntegerType) {
// need to cast arg2 to fraction
_knumfraction tmp_num(arg2);
return tmp_num.add(*this);
}
if (arg2.type() == FloatType || arg2.type() == SpecialType)
return arg2.add(*this);
_knumfraction * tmp_num = new _knumfraction();
mpq_add(tmp_num->_mpq, _mpq,
dynamic_cast<_knumfraction const &>(arg2)._mpq);
return tmp_num;
}
_knumber *_knumfloat::add(_knumber const & arg2) const
{
if (arg2.type() == SpecialType)
return arg2.add(*this);
if (arg2.type() != FloatType) {
// need to cast arg2 to float
_knumfloat tmp_num(arg2);
return tmp_num.add(*this);
}
_knumfloat * tmp_num = new _knumfloat();
mpf_add(tmp_num->_mpf, _mpf,
dynamic_cast<_knumfloat const &>(arg2)._mpf);
return tmp_num;
}
_knumber * _knumerror::multiply(_knumber const & arg2) const
{
//improve this
switch(arg2.type()) {
case SpecialType:
{
_knumerror const & tmp_arg2 = dynamic_cast<_knumerror const &>(arg2);
if (_error == UndefinedNumber || tmp_arg2._error == UndefinedNumber)
return new _knumerror(UndefinedNumber);
if ( this->sign() * arg2.sign() > 0)
return new _knumerror(Infinity);
else
return new _knumerror(MinusInfinity);
}
case IntegerType:
case FractionType:
case FloatType:
{
int sign_arg2 = arg2.sign();
if (_error == UndefinedNumber || sign_arg2 == 0)
return new _knumerror(UndefinedNumber);
if ( (_error == Infinity && sign_arg2 > 0) ||
(_error == MinusInfinity && sign_arg2 < 0) )
return new _knumerror(Infinity);
return new _knumerror(MinusInfinity);
}
}
return new _knumerror(_error);
}
_knumber * _knuminteger::multiply(_knumber const & arg2) const
{
if (arg2.type() != IntegerType)
return arg2.multiply(*this);
_knuminteger * tmp_num = new _knuminteger();
mpz_mul(tmp_num->_mpz, _mpz,
dynamic_cast<_knuminteger const &>(arg2)._mpz);
return tmp_num;
}
_knumber * _knumfraction::multiply(_knumber const & arg2) const
{
if (arg2.type() == IntegerType) {
// need to cast arg2 to fraction
_knumfraction tmp_num(arg2);
return tmp_num.multiply(*this);
}
if (arg2.type() == FloatType || arg2.type() == SpecialType)
return arg2.multiply(*this);
_knumfraction * tmp_num = new _knumfraction();
mpq_mul(tmp_num->_mpq, _mpq,
dynamic_cast<_knumfraction const &>(arg2)._mpq);
return tmp_num;
}
_knumber *_knumfloat::multiply(_knumber const & arg2) const
{
if (arg2.type() == SpecialType)
return arg2.multiply(*this);
if (arg2.type() == IntegerType &&
mpz_cmp_si(dynamic_cast<_knuminteger const &>(arg2)._mpz,0) == 0)
// if arg2 == 0 return integer 0!!
return new _knuminteger(0);
if (arg2.type() != FloatType) {
// need to cast arg2 to float
_knumfloat tmp_num(arg2);
return tmp_num.multiply(*this);
}
_knumfloat * tmp_num = new _knumfloat();
mpf_mul(tmp_num->_mpf, _mpf,
dynamic_cast<_knumfloat const &>(arg2)._mpf);
return tmp_num;
}
_knumber * _knumber::divide(_knumber const & arg2) const
{
_knumber * tmp_num = arg2.reciprocal();
_knumber * rslt_num = this->multiply(*tmp_num);
delete tmp_num;
return rslt_num;
}
_knumber *_knumfloat::divide(_knumber const & arg2) const
{
if(mpf_cmp_si(_mpf, 0) == 0) return new _knumerror(Infinity);
// automatically casts arg2 to float
_knumfloat * tmp_num = new _knumfloat(arg2);
mpf_div(tmp_num->_mpf, _mpf, tmp_num->_mpf);
return tmp_num;
}
_knumber * _knumerror::power(_knumber const & exponent) const
{
static_cast<void>(exponent);
return new _knumerror(UndefinedNumber);
}
_knumber * _knuminteger::power(_knumber const & exponent) const
{
if (exponent.type() == IntegerType) {
mpz_t tmp_mpz;
mpz_init_set(tmp_mpz,
dynamic_cast<_knuminteger const &>(exponent)._mpz);
if (! mpz_fits_ulong_p(tmp_mpz)) { // conversion wouldn't work, so
// use floats
mpz_clear(tmp_mpz);
// need to cast everything to float
_knumfloat tmp_num1(*this), tmp_num2(exponent);
return tmp_num1.power(tmp_num2);
}
unsigned long int tmp_int = mpz_get_ui(tmp_mpz);
mpz_clear(tmp_mpz);
_knuminteger * tmp_num = new _knuminteger();
mpz_pow_ui(tmp_num->_mpz, _mpz, tmp_int);
return tmp_num;
}
if (exponent.type() == FractionType) {
if (mpz_sgn(_mpz) < 0)
return new _knumerror(UndefinedNumber);
// GMP only supports few root functions, so we need to convert
// into signed long int
mpz_t tmp_mpz;
mpz_init_set(tmp_mpz,
mpq_denref(dynamic_cast<_knumfraction const &>(exponent)._mpq));
if (! mpz_fits_ulong_p(tmp_mpz)) { // conversion wouldn't work, so
// use floats
mpz_clear(tmp_mpz);
// need to cast everything to float
_knumfloat tmp_num1(*this), tmp_num2(exponent);
return tmp_num1.power(tmp_num2);
}
unsigned long int tmp_int = mpz_get_ui(tmp_mpz);
mpz_clear(tmp_mpz);
// first check if result will be an integer
_knuminteger * tmp_num = new _knuminteger();
int flag = mpz_root(tmp_num->_mpz, _mpz, tmp_int);
if (flag == 0) { // result is not exact
delete tmp_num;
// need to cast everything to float
_knumfloat tmp_num1(*this), tmp_num2(exponent);
return tmp_num1.power(tmp_num2);
}
// result is exact
mpz_init_set(tmp_mpz,
mpq_numref(dynamic_cast<_knumfraction const &>(exponent)._mpq));
if (! mpz_fits_ulong_p(tmp_mpz)) { // conversion wouldn't work, so
// use floats
mpz_clear(tmp_mpz);
// need to cast everything to float
_knumfloat tmp_num1(*this), tmp_num2(exponent);
return tmp_num1.power(tmp_num2);
}
tmp_int = mpz_get_ui(tmp_mpz);
mpz_clear(tmp_mpz);
mpz_pow_ui(tmp_num->_mpz, tmp_num->_mpz, tmp_int);
return tmp_num;
}
if (exponent.type() == FloatType) {
// need to cast everything to float
_knumfloat tmp_num(*this);
return tmp_num.power(exponent);
}
return new _knumerror(Infinity);
}
_knumber * _knumfraction::power(_knumber const & exponent) const
{
_knuminteger tmp_num = _knuminteger();
mpz_set(tmp_num._mpz, mpq_numref(_mpq));
_knumber *numer = tmp_num.power(exponent);
mpz_set(tmp_num._mpz, mpq_denref(_mpq));
_knumber *denom = tmp_num.power(exponent);
_knumber *result = numer->divide(*denom);
delete numer;
delete denom;
return result;
}
_knumber * _knumfloat::power(_knumber const & exponent) const
{
return new _knumfloat(pow(static_cast<double>(*this),
static_cast<double>(exponent)));
}
int _knumerror::compare(_knumber const &arg2) const
{
if (arg2.type() != SpecialType) {
switch(_error) {
case Infinity:
return 1;
case MinusInfinity:
return -1;
default:
return 1; // Not really o.k., but what should I return
}
}
switch(_error) {
case Infinity:
if (dynamic_cast<_knumerror const &>(arg2)._error == Infinity)
// Infinity is larger than anything else, but itself
return 0;
return 1;
case MinusInfinity:
if (dynamic_cast<_knumerror const &>(arg2)._error == MinusInfinity)
// MinusInfinity is smaller than anything else, but itself
return 0;
return -1;
default:
if (dynamic_cast<_knumerror const &>(arg2)._error == UndefinedNumber)
// Undefined only equal to itself
return 0;
return -arg2.compare(*this);
}
}
int _knuminteger::compare(_knumber const &arg2) const
{
if (arg2.type() != IntegerType)
return - arg2.compare(*this);
return mpz_cmp(_mpz, dynamic_cast<_knuminteger const &>(arg2)._mpz);
}
int _knumfraction::compare(_knumber const &arg2) const
{
if (arg2.type() != FractionType) {
if (arg2.type() == IntegerType) {
mpq_t tmp_frac;
mpq_init(tmp_frac);
mpq_set_z(tmp_frac,
dynamic_cast<_knuminteger const &>(arg2)._mpz);
int cmp_result = mpq_cmp(_mpq, tmp_frac);
mpq_clear(tmp_frac);
return cmp_result;
} else
return - arg2.compare(*this);
}
return mpq_cmp(_mpq, dynamic_cast<_knumfraction const &>(arg2)._mpq);
}
int _knumfloat::compare(_knumber const &arg2) const
{
if (arg2.type() != FloatType) {
mpf_t tmp_float;
if (arg2.type() == IntegerType) {
mpf_init(tmp_float);
mpf_set_z(tmp_float,
dynamic_cast<_knuminteger const &>(arg2)._mpz);
} else if (arg2.type() == FractionType) {
mpf_init(tmp_float);
mpf_set_q(tmp_float,
dynamic_cast<_knumfraction const &>(arg2)._mpq);
} else
return - arg2.compare(*this);
int cmp_result = mpf_cmp(_mpf, tmp_float);
mpf_clear(tmp_float);
return cmp_result;
}
return mpf_cmp(_mpf, dynamic_cast<_knumfloat const &>(arg2)._mpf);
}
_knumerror::operator signed long int (void) const
{
// what would be the correct return values here?
if (_error == Infinity)
return 0;
if (_error == MinusInfinity)
return 0;
else // if (_error == UndefinedNumber)
return 0;
}
_knumerror::operator unsigned long int (void) const
{
// what would be the correct return values here?
if (_error == Infinity)
return 0;
if (_error == MinusInfinity)
return 0;
else // if (_error == UndefinedNumber)
return 0;
}
_knuminteger::operator signed long int (void) const
{
return mpz_get_si(_mpz);
}
_knumfraction::operator signed long int (void) const
{
return static_cast<signed long int>(mpq_get_d(_mpq));
}
_knumfloat::operator signed long int (void) const
{
return mpf_get_si(_mpf);
}
_knuminteger::operator unsigned long int (void) const
{
return mpz_get_ui(_mpz);
}
_knumfraction::operator unsigned long int (void) const
{
return static_cast<unsigned long int>(mpq_get_d(_mpq));
}
_knumfloat::operator unsigned long int (void) const
{
return mpf_get_ui(_mpf);
}
_knumerror::operator double (void) const
{
if (_error == Infinity)
return INFINITY;
if (_error == MinusInfinity)
return -INFINITY;
else // if (_error == UndefinedNumber)
return NAN;
}
_knuminteger::operator double (void) const
{
return mpz_get_d(_mpz);
}
_knumfraction::operator double (void) const
{
return mpq_get_d(_mpq);
}
_knumfloat::operator double (void) const
{
return mpf_get_d(_mpf);
}
_knuminteger * _knuminteger::intAnd(_knuminteger const &arg2) const
{
_knuminteger * tmp_num = new _knuminteger();
mpz_and(tmp_num->_mpz, _mpz, arg2._mpz);
return tmp_num;
}
_knuminteger * _knuminteger::intOr(_knuminteger const &arg2) const
{
_knuminteger * tmp_num = new _knuminteger();
mpz_ior(tmp_num->_mpz, _mpz, arg2._mpz);
return tmp_num;
}
_knumber * _knuminteger::mod(_knuminteger const &arg2) const
{
if(mpz_cmp_si(arg2._mpz, 0) == 0) return new _knumerror(UndefinedNumber);
_knuminteger * tmp_num = new _knuminteger();
mpz_mod(tmp_num->_mpz, _mpz, arg2._mpz);
return tmp_num;
}
_knumber * _knuminteger::shift(_knuminteger const &arg2) const
{
mpz_t tmp_mpz;
mpz_init_set (tmp_mpz, arg2._mpz);
if (! mpz_fits_slong_p(tmp_mpz)) {
mpz_clear(tmp_mpz);
return new _knumerror(UndefinedNumber);
}
signed long int tmp_arg2 = mpz_get_si(tmp_mpz);
mpz_clear(tmp_mpz);
_knuminteger * tmp_num = new _knuminteger();
if (tmp_arg2 > 0) // left shift
mpz_mul_2exp(tmp_num->_mpz, _mpz, tmp_arg2);
else // right shift
mpz_tdiv_q_2exp(tmp_num->_mpz, _mpz, -tmp_arg2);
return tmp_num;
}
|