-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqfunc.c
1845 lines (1699 loc) · 34.3 KB
/
qfunc.c
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
/*
* qfunc - extended precision rational arithmetic non-primitive functions
*
* Copyright (C) 1999-2007 David I. Bell and Ernest Bowen
*
* Primary author: David I. Bell
*
* Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* Calc 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 Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Under source code control: 1990/02/15 01:48:20
* File existed as early as: before 1990
*
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
#include "qmath.h"
#include "config.h"
#include "prime.h"
STATIC NUMBER **B_table;
STATIC long B_num;
STATIC long B_allocnum;
STATIC NUMBER **E_table;
STATIC long E_num;
#define QALLOCNUM 64
/*
* Set the default epsilon for approximate calculations.
* This must be greater than zero.
*
* given:
* q number to be set as the new epsilon
*/
void
setepsilon(NUMBER *q)
{
NUMBER *old;
if (qisneg(q) || qiszero(q)) {
math_error("Epsilon value must be greater than zero");
/*NOTREACHED*/
}
old = conf->epsilon;
conf->epsilonprec = qprecision(q);
conf->epsilon = qlink(q);
if (old)
qfree(old);
}
/*
* Return the inverse of one number modulo another.
* That is, find x such that:
* Ax = 1 (mod B)
* Returns zero if the numbers are not relatively prime (temporary hack).
*/
NUMBER *
qminv(NUMBER *q1, NUMBER *q2)
{
NUMBER *r;
ZVALUE z1, z2, tmp;
int s, t;
long rnd;
if (qisfrac(q1) || qisfrac(q2)) {
math_error("Non-integers for minv");
/*NOTREACHED*/
}
if (qiszero(q2)) {
if (qisunit(q1))
return qlink(q1);
return qlink(&_qzero_);
}
if (qisunit(q2))
return qlink(&_qzero_);
rnd = conf->mod;
s = (rnd & 4) ? 0 : q2->num.sign;
if (rnd & 1)
s^= 1;
tmp = q2->num;
tmp.sign = 0;
if (zmodinv(q1->num, tmp, &z1))
return qlink(&_qzero_);
zsub(tmp, z1, &z2);
if (rnd & 16) {
t = zrel(z1, z2);
if (t < 0)
s = 0;
else if (t > 0)
s = 1;
}
r = qalloc();
if (s) {
zfree(z1);
z2.sign = TRUE;
r->num = z2;
return r;
}
zfree(z2);
r->num = z1;
return r;
}
/*
* Return the residue modulo an integer (q3) of an integer (q1)
* raised to a positive integer (q2) power.
*/
NUMBER *
qpowermod(NUMBER *q1, NUMBER *q2, NUMBER *q3)
{
NUMBER *r;
ZVALUE z1, z2, tmp;
int s, t;
long rnd;
if (qisfrac(q1) || qisfrac(q2) || qisfrac(q3)) {
math_error("Non-integers for pmod");
/*NOTREACHED*/
}
if (qisneg(q2)) {
math_error("Negative power for pmod");
/*NOTREACHED*/
}
if (qiszero(q3))
return qpowi(q1, q2);
if (qisunit(q3))
return qlink(&_qzero_);
rnd = conf->mod;
s = (rnd & 4) ? 0 : q3->num.sign;
if (rnd & 1)
s^= 1;
tmp = q3->num;
tmp.sign = 0;
zpowermod(q1->num, q2->num, tmp, &z1);
if (ziszero(z1)) {
zfree(z1);
return qlink(&_qzero_);
}
zsub(tmp, z1, &z2);
if (rnd & 16) {
t = zrel(z1, z2);
if (t < 0)
s = 0;
else if (t > 0)
s = 1;
}
r = qalloc();
if (s) {
zfree(z1);
z2.sign = TRUE;
r->num = z2;
return r;
}
zfree(z2);
r->num = z1;
return r;
}
/*
* Return the power function of one number with another.
* The power must be integral.
* q3 = qpowi(q1, q2);
*/
NUMBER *
qpowi(NUMBER *q1, NUMBER *q2)
{
register NUMBER *r;
BOOL invert, sign;
ZVALUE num, zden, z2;
if (qisfrac(q2)) {
math_error("Raising number to fractional power");
/*NOTREACHED*/
}
num = q1->num;
zden = q1->den;
z2 = q2->num;
sign = (num.sign && zisodd(z2));
invert = z2.sign;
num.sign = 0;
z2.sign = 0;
/*
* Check for trivial cases first.
*/
if (ziszero(num) && !ziszero(z2)) { /* zero raised to a power */
if (invert) {
math_error("Zero raised to negative power");
/*NOTREACHED*/
}
return qlink(&_qzero_);
}
if (zisunit(num) && zisunit(zden)) { /* 1 or -1 raised to a power */
r = (sign ? q1 : &_qone_);
r->links++;
return r;
}
if (ziszero(z2)) /* raising to zeroth power */
return qlink(&_qone_);
if (zisunit(z2)) { /* raising to power 1 or -1 */
if (invert)
return qinv(q1);
return qlink(q1);
}
/*
* Not a trivial case. Do the real work.
*/
r = qalloc();
if (!zisunit(num))
zpowi(num, z2, &r->num);
if (!zisunit(zden))
zpowi(zden, z2, &r->den);
if (invert) {
z2 = r->num;
r->num = r->den;
r->den = z2;
}
r->num.sign = sign;
return r;
}
/*
* Given the legs of a right triangle, compute its hypotenuse within
* the specified error. This is sqrt(a^2 + b^2).
*/
NUMBER *
qhypot(NUMBER *q1, NUMBER *q2, NUMBER *epsilon)
{
NUMBER *tmp1, *tmp2, *tmp3;
if (qiszero(epsilon)) {
math_error("Zero epsilon value for hypot");
/*NOTREACHED*/
}
if (qiszero(q1))
return qqabs(q2);
if (qiszero(q2))
return qqabs(q1);
tmp1 = qsquare(q1);
tmp2 = qsquare(q2);
tmp3 = qqadd(tmp1, tmp2);
qfree(tmp1);
qfree(tmp2);
tmp1 = qsqrt(tmp3, epsilon, 24L);
qfree(tmp3);
return tmp1;
}
/*
* Given one leg of a right triangle with unit hypotenuse, calculate
* the other leg within the specified error. This is sqrt(1 - a^2).
* If the wantneg flag is nonzero, then negative square root is returned.
*/
NUMBER *
qlegtoleg(NUMBER *q, NUMBER *epsilon, BOOL wantneg)
{
NUMBER *res, *qtmp1, *qtmp2;
ZVALUE num;
if (qiszero(epsilon)) {
math_error("Zero epsilon value for legtoleg");
/*NOTREACHED*/
}
if (qisunit(q))
return qlink(&_qzero_);
if (qiszero(q)) {
if (wantneg)
return qlink(&_qnegone_);
return qlink(&_qone_);
}
num = q->num;
num.sign = 0;
if (zrel(num, q->den) >= 0) {
math_error("Leg too large in legtoleg");
/*NOTREACHED*/
}
qtmp1 = qsquare(q);
qtmp2 = qsub(&_qone_, qtmp1);
qfree(qtmp1);
res = qsqrt(qtmp2, epsilon, 24L);
qfree(qtmp2);
if (wantneg) {
qtmp1 = qneg(res);
qfree(res);
res = qtmp1;
}
return res;
}
/*
* Compute the square root of a real number.
* Type of rounding if any depends on rnd.
* If rnd & 32 is nonzero, result is exact for square numbers;
* If rnd & 64 is nonzero, the negative square root is returned;
* If rnd < 32, result is rounded to a multiple of epsilon
* up, down, etc. depending on bits 0, 2, 4 of v.
*/
NUMBER *
qsqrt(NUMBER *q1, NUMBER *epsilon, long rnd)
{
NUMBER *r, etemp;
ZVALUE tmp1, tmp2, quo, mul, divisor;
long s1, s2, up, RR, RS;
int sign;
if (qisneg(q1)) {
math_error("Square root of negative number");
/*NOTREACHED*/
}
if (qiszero(q1))
return qlink(&_qzero_);
sign = (rnd & 64) != 0;
if (qiszero(epsilon)) {
math_error("Zero epsilon for qsqrt");
/*NOTREACHED*/
}
etemp = *epsilon;
etemp.num.sign = 0;
RS = rnd & 25;
if (!(RS & 8))
RS ^= epsilon->num.sign;
if (rnd & 2)
RS ^= sign ^ epsilon->num.sign;
if (rnd & 4)
RS ^= epsilon->num.sign;
RR = zisunit(q1->den) && qisunit(epsilon);
if (rnd & 32 || RR) {
s1 = zsqrt(q1->num, &tmp1, RS);
if (RR) {
if (ziszero(tmp1)) {
zfree(tmp1);
return qlink(&_qzero_);
}
r = qalloc();
tmp1.sign = sign;
r->num = tmp1;
return r;
}
if (!s1) {
s2 = zsqrt(q1->den, &tmp2, 0);
if (!s2) {
r = qalloc();
tmp1.sign = sign;
r->num = tmp1;
r->den = tmp2;
return r;
}
zfree(tmp2);
}
zfree(tmp1);
}
up = 0;
zsquare(epsilon->den, &tmp1);
zmul(tmp1, q1->num, &tmp2);
zfree(tmp1);
zsquare(epsilon->num, &tmp1);
zmul(tmp1, q1->den, &divisor);
zfree(tmp1);
if (rnd & 16) {
zshift(tmp2, 2, &tmp1);
zfree(tmp2);
s1 = zquo(tmp1, divisor, &quo, 16);
zfree(tmp1);
s2 = zsqrt(quo, &tmp1, s1 ? s1 < 0 : 16);
zshift(tmp1, -1, &mul);
up = (*tmp1.v & 1) ? s1 + s2 : -1;
zfree(tmp1);
} else {
s1 = zquo(tmp2, divisor, &quo, 0);
zfree(tmp2);
s2 = zsqrt(quo, &mul, 0);
up = (s1 + s2) ? 0 : -1;
}
if (up == 0) {
if (rnd & 8)
up = (long)((RS ^ *mul.v) & 1);
else
up = RS ^ sign;
}
if (up > 0) {
zadd(mul, _one_, &tmp2);
zfree(mul);
mul = tmp2;
}
zfree(divisor);
zfree(quo);
if (ziszero(mul)) {
zfree(mul);
return qlink(&_qzero_);
}
r = qalloc();
zreduce(mul, etemp.den, &tmp1, &r->den);
zfree(mul);
tmp1.sign = sign;
zmul(tmp1, etemp.num, &r->num);
zfree(tmp1);
return r;
}
/*
* Calculate the integral part of the square root of a number.
* Example: qisqrt(13) = 3.
*/
NUMBER *
qisqrt(NUMBER *q)
{
NUMBER *r;
ZVALUE tmp;
if (qisneg(q)) {
math_error("Square root of negative number");
/*NOTREACHED*/
}
if (qiszero(q))
return qlink(&_qzero_);
r = qalloc();
if (qisint(q)) {
(void) zsqrt(q->num, &r->num,0);
return r;
}
zquo(q->num, q->den, &tmp, 0);
(void) zsqrt(tmp, &r->num,0);
freeh(tmp.v);
return r;
}
/*
* Return whether or not a number is an exact square.
*/
BOOL
qissquare(NUMBER *q)
{
BOOL flag;
flag = zissquare(q->num);
if (qisint(q) || !flag)
return flag;
return zissquare(q->den);
}
/*
* Compute the greatest integer of the Kth root of a number.
* Example: qiroot(85, 3) = 4.
*/
NUMBER *
qiroot(NUMBER *q1, NUMBER *q2)
{
NUMBER *r;
ZVALUE tmp;
if (qisneg(q2) || qiszero(q2) || qisfrac(q2)) {
math_error("Taking number to bad root value");
/*NOTREACHED*/
}
if (qiszero(q1))
return qlink(&_qzero_);
if (qisone(q1) || qisone(q2))
return qlink(q1);
if (qistwo(q2))
return qisqrt(q1);
r = qalloc();
if (qisint(q1)) {
zroot(q1->num, q2->num, &r->num);
return r;
}
zquo(q1->num, q1->den, &tmp, 0);
zroot(tmp, q2->num, &r->num);
zfree(tmp);
return r;
}
/*
* Return the greatest integer of the base 2 log of a number.
* This is the number such that 1 <= x / log2(x) < 2.
* Examples: qilog2(8) = 3, qilog2(1.3) = 1, qilog2(1/7) = -3.
*
* given:
* q number to take log of
*/
long
qilog2(NUMBER *q)
{
long n; /* power of two */
int c; /* result of comparison */
ZVALUE tmp1, tmp2; /* temporary values */
if (qiszero(q)) {
math_error("Zero argument for ilog2");
/*NOTREACHED*/
}
if (qisint(q))
return zhighbit(q->num);
tmp1 = q->num;
tmp1.sign = 0;
n = zhighbit(tmp1) - zhighbit(q->den);
if (n == 0)
c = zrel(tmp1, q->den);
else if (n > 0) {
zshift(q->den, n, &tmp2);
c = zrel(tmp1, tmp2);
zfree(tmp2);
} else {
zshift(tmp1, -n, &tmp2);
c = zrel(tmp2, q->den);
zfree(tmp2);
}
if (c < 0)
n--;
return n;
}
/*
* Return the greatest integer of the base 10 log of a number.
* This is the number such that 1 <= x / log10(x) < 10.
* Examples: qilog10(100) = 2, qilog10(12.3) = 1, qilog10(.023) = -2.
*
* given:
* q number to take log of
*/
long
qilog10(NUMBER *q)
{
long n; /* log value */
ZVALUE tmp1, tmp2; /* temporary values */
if (qiszero(q)) {
math_error("Zero argument for ilog10");
/*NOTREACHED*/
}
tmp1 = q->num;
tmp1.sign = 0;
if (qisint(q))
return zlog10(tmp1, NULL);
/*
* The number is not an integer.
* Compute the result if the number is greater than one.
*/
if (zrel(tmp1, q->den) > 0) {
zquo(tmp1, q->den, &tmp2, 0);
n = zlog10(tmp2, NULL);
zfree(tmp2);
return n;
}
/*
* Here if the number is less than one.
* If the number is the inverse of a power of ten, then the
* obvious answer will be off by one. Subtracting one if the
* number is the inverse of an integer will fix it.
*/
if (zisunit(tmp1))
zsub(q->den, _one_, &tmp2);
else
zquo(q->den, tmp1, &tmp2, 0);
n = -zlog10(tmp2, NULL) - 1;
zfree(tmp2);
return n;
}
/*
* Return the integer floor of the logarithm of a number relative to
* a specified integral base.
*/
NUMBER *
qilog(NUMBER *q, ZVALUE base)
{
long n;
ZVALUE tmp1, tmp2;
if (qiszero(q))
return NULL;
if (qisunit(q))
return qlink(&_qzero_);
if (qisint(q))
return itoq(zlog(q->num, base));
tmp1 = q->num;
tmp1.sign = 0;
if (zrel(tmp1, q->den) > 0) {
zquo(tmp1, q->den, &tmp2, 0);
n = zlog(tmp2, base);
zfree(tmp2);
return itoq(n);
}
if (zisunit(tmp1))
zsub(q->den, _one_, &tmp2);
else
zquo(q->den, tmp1, &tmp2, 0);
n = -zlog(tmp2, base) - 1;
zfree(tmp2);
return itoq(n);
}
/*
* Return the number of digits in the representation to a specified
* base of the integral part of a number.
*
* Examples: qdigits(3456,10) = 4, qdigits(-23.45, 10) = 2.
*
* One should remember these special cases:
*
* digits(12.3456) == 2 computes with the integer part only
* digits(-1234) == 4 computes with the absolute value only
* digits(0) == 1 specical case
* digits(-0.123) == 1 combination of all of the above
*
* given:
* q number to count digits of
*/
long
qdigits(NUMBER *q, ZVALUE base)
{
long n; /* number of digits */
ZVALUE temp; /* temporary value */
if (zabsrel(q->num, q->den) < 0)
return 1;
if (qisint(q))
return 1 + zlog(q->num, base);
zquo(q->num, q->den, &temp, 2);
n = 1 + zlog(temp, base);
zfree(temp);
return n;
}
/*
* Return the digit at the specified place in the expansion to specified
* base of a rational number. The places specified by dpos are numbered from
* the "units" place just before the "decimal" point, so that negative
* dpos indicates the (-dpos)th place to the right of the point.
* Examples: qdigit(1234.5678, 1, 10) = 3, qdigit(1234.5678, -3, 10) = 7.
* The signs of the number and the base are ignored.
*/
NUMBER *
qdigit(NUMBER *q, ZVALUE dpos, ZVALUE base)
{
ZVALUE N, D;
ZVALUE K;
long k;
ZVALUE A, B, C; /* temporary integers */
NUMBER *res;
/*
* In the first stage, q is expressed as base^k * N/D where
* gcd(D, base) = 1
* K is k as a ZVALUE
*/
base.sign = 0;
if (ziszero(base) || zisunit(base))
return NULL;
if (qiszero(q) || (qisint(q) && zisneg(dpos)) ||
(zge31b(dpos) && !zisneg(dpos)))
return qlink(&_qzero_);
k = zfacrem(q->num, base, &N);
if (k == 0) {
k = zgcdrem(q->den, base, &D);
if (k > 0) {
zequo(q->den, D, &A);
itoz(k, &K);
zpowi(base, K, &B);
zfree(K);
zequo(B, A, &C);
zfree(A);
zfree(B);
zmul(C, q->num, &N);
zfree(C);
k = -k;
}
else
N = q->num;
}
if (k >= 0)
D = q->den;
itoz(k, &K);
if (zrel(dpos, K) >= 0) {
zsub(dpos, K, &A);
zfree(K);
zpowi(base, A, &B);
zfree(A);
zmul(D, B, &A);
zfree(B);
zquo(N, A, &B, 0);
} else {
if (zisunit(D)) {
if (k != 0)
zfree(N);
zfree(K);
if (k < 0)
zfree(D);
return qlink(&_qzero_);
}
zsub(K, dpos, &A);
zfree(K);
zpowermod(base, A, D, &C);
zfree(A);
zmod(N, D, &A, 0);
zmul(C, A, &B);
zfree(A);
zfree(C);
zmod(B, D, &A, 0);
zfree(B);
zmodinv(D, base, &B);
zsub(base, B, &C);
zfree(B);
zmul(C, A, &B);
zfree(C);
}
zfree(A);
if (k != 0)
zfree(N);
if (k < 0)
zfree(D);
zmod(B, base, &A, 0);
zfree(B);
if (ziszero(A)) {
zfree(A);
return qlink(&_qzero_);
}
if (zisone(A)) {
zfree(A);
return qlink(&_qone_);
}
res = qalloc();
res->num = A;
return res;
}
/*
* Return whether or not a bit is set at the specified bit position in a
* number. The lowest bit of the integral part of a number is the zeroth
* bit position. Negative bit positions indicate bits to the right of the
* binary decimal point. Examples: qdigit(17.1, 0) = 1, qdigit(17.1, -1) = 0.
*/
BOOL
qisset(NUMBER *q, long n)
{
NUMBER *qtmp1, *qtmp2;
ZVALUE ztmp;
BOOL res;
/*
* Zero number or negative bit position place of integer is trivial.
*/
if (qiszero(q) || (qisint(q) && (n < 0)))
return FALSE;
/*
* For non-negative bit positions, answer is easy.
*/
if (n >= 0) {
if (qisint(q))
return zisset(q->num, n);
zquo(q->num, q->den, &ztmp, 2);
res = zisset(ztmp, n);
zfree(ztmp);
return res;
}
/*
* Fractional value and want negative bit position, must work harder.
*/
qtmp1 = qscale(q, -n);
qtmp2 = qint(qtmp1);
qfree(qtmp1);
res = ((qtmp2->num.v[0] & 0x01) != 0);
qfree(qtmp2);
return res;
}
/*
* Compute the factorial of an integer.
* q2 = qfact(q1);
*/
NUMBER *
qfact(NUMBER *q)
{
register NUMBER *r;
if (qisfrac(q)) {
math_error("Non-integral factorial");
/*NOTREACHED*/
}
if (qiszero(q) || zisone(q->num))
return qlink(&_qone_);
r = qalloc();
zfact(q->num, &r->num);
return r;
}
/*
* Compute the product of the primes less than or equal to a number.
* q2 = qpfact(q1);
*/
NUMBER *
qpfact(NUMBER *q)
{
NUMBER *r;
if (qisfrac(q)) {
math_error("Non-integral factorial");
/*NOTREACHED*/
}
r = qalloc();
zpfact(q->num, &r->num);
return r;
}
/*
* Compute the lcm of all the numbers less than or equal to a number.
* q2 = qlcmfact(q1);
*/
NUMBER *
qlcmfact(NUMBER *q)
{
NUMBER *r;
if (qisfrac(q)) {
math_error("Non-integral lcmfact");
/*NOTREACHED*/
}
r = qalloc();
zlcmfact(q->num, &r->num);
return r;
}
/*
* Compute the permutation function q1 * (q1-1) * ... * (q1-q2+1).
*/
NUMBER *
qperm(NUMBER *q1, NUMBER *q2)
{
NUMBER *r;
NUMBER *qtmp1, *qtmp2;
long i;
if (qisfrac(q2)) {
math_error("Non-integral second arg for permutation");
/*NOTREACHED*/
}
if (qiszero(q2))
return qlink(&_qone_);
if (qisone(q2))
return qlink(q1);
if (qisint(q1) && !qisneg(q1)) {
if (qrel(q2, q1) > 0)
return qlink(&_qzero_);
if (qispos(q2)) {
r = qalloc();
zperm(q1->num, q2->num, &r->num);
return r;
}
}
if (zge31b(q2->num)) {
math_error("Too large arg2 for permutation");
/*NOTREACHED*/
}
i = qtoi(q2);
if (i > 0) {
q1 = qlink(q1);
r = qlink(q1);
while (--i > 0) {
qtmp1 = qdec(q1);
qtmp2 = qmul(r, qtmp1);
qfree(q1);
q1 = qtmp1;
qfree(r);
r = qtmp2;
}
qfree(q1);
return r;
}
i = -i;
qtmp1 = qinc(q1);
r = qinv(qtmp1);
while (--i > 0) {
qtmp2 = qinc(qtmp1);
qfree(qtmp1);
qtmp1 = qqdiv(r, qtmp2);
qfree(r);
r = qtmp1;
qtmp1 = qtmp2;
}
qfree(qtmp1);
return r;
}
/*
* Compute the combinatorial function q(q - 1) ...(q - n + 1)/n!
* n is to be a nonnegative integer
*/
NUMBER *
qcomb(NUMBER *q, NUMBER *n)
{
NUMBER *r;
NUMBER *q1, *q2;
long i, j;
ZVALUE z;
if (!qisint(n) || qisneg(n)) {
math_error("Bad second arg in call to qcomb!");
/*NOTREACHED*/
}
if (qisint(q)) {
switch (zcomb(q->num, n->num, &z)) {
case 0:
return qlink(&_qzero_);
case 1:
return qlink(&_qone_);
case -1:
return qlink(&_qnegone_);
case 2:
return qlink(q);
case -2:
return NULL;
default:
r = qalloc();
r->num = z;
return r;
}
}
if (zge31b(n->num))
return NULL;
i = ztoi(n->num);
q = qlink(q);
r = qlink(q);
j = 1;
while (--i > 0) {
q1 = qdec(q);
qfree(q);
q = q1;
q2 = qmul(r, q);
qfree(r);
r = qdivi(q2, ++j);
qfree(q2);
}
qfree(q);
return r;
}
/*
* Compute the Bernoulli number with index n
* For even positive n, newly calculated values for all even indices up
* to n are stored in table at B_table.
*/
NUMBER *
qbern(ZVALUE z)
{
long n, i, k, m, nn, dd;
NUMBER **p;
NUMBER *s, *s1, *c, *c1, *t;
size_t sz;
if (zisone(z))
return qlink(&_qneghalf_);
if (zisodd(z) || z.sign)
return qlink(&_qzero_);
if (zge31b(z))
return NULL;
n = ztoi(z);
if (n == 0)
return qlink(&_qone_);
m = (n >> 1) - 1;