2 * qfunc - extended precision rational arithmetic non-primitive functions
4 * Copyright (C) 1999-2007 David I. Bell and Ernest Bowen
6 * Primary author: David I. Bell
8 * Calc is open software; you can redistribute it and/or modify it under
9 * the terms of the version 2.1 of the GNU Lesser General Public License
10 * as published by the Free Software Foundation.
12 * Calc is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
15 * Public License for more details.
17 * A copy of version 2.1 of the GNU Lesser General Public License is
18 * distributed with calc under the filename COPYING-LGPL. You should have
19 * received a copy with calc; if not, write to Free Software Foundation, Inc.
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * @(#) $Revision: 30.1 $
23 * @(#) $Id: qfunc.c,v 30.1 2007/03/16 11:09:46 chongo Exp $
24 * @(#) $Source: /usr/local/src/bin/calc/RCS/qfunc.c,v $
26 * Under source code control: 1990/02/15 01:48:20
27 * File existed as early as: before 1990
29 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
37 STATIC NUMBER
**B_table
;
39 STATIC
long B_allocnum
;
40 STATIC NUMBER
**E_table
;
46 * Set the default epsilon for approximate calculations.
47 * This must be greater than zero.
50 * q number to be set as the new epsilon
57 if (qisneg(q
) || qiszero(q
)) {
58 math_error("Epsilon value must be greater than zero");
62 conf
->epsilonprec
= qprecision(q
);
63 conf
->epsilon
= qlink(q
);
70 * Return the inverse of one number modulo another.
71 * That is, find x such that:
73 * Returns zero if the numbers are not relatively prime (temporary hack).
76 qminv(NUMBER
*q1
, NUMBER
*q2
)
83 if (qisfrac(q1
) || qisfrac(q2
)) {
84 math_error("Non-integers for minv");
90 return qlink(&_qzero_
);
93 return qlink(&_qzero_
);
95 s
= (rnd
& 4) ? 0 : q2
->num
.sign
;
101 if (zmodinv(q1
->num
, tmp
, &z1
))
102 return qlink(&_qzero_
);
125 * Return the residue modulo an integer (q3) of an integer (q1)
126 * raised to a positive integer (q2) power.
129 qpowermod(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*q3
)
136 if (qisfrac(q1
) || qisfrac(q2
) || qisfrac(q3
)) {
137 math_error("Non-integers for pmod");
141 math_error("Negative power for pmod");
145 return qpowi(q1
, q2
);
147 return qlink(&_qzero_
);
149 s
= (rnd
& 4) ? 0 : q3
->num
.sign
;
154 zpowermod(q1
->num
, q2
->num
, tmp
, &z1
);
157 return qlink(&_qzero_
);
181 * Return the power function of one number with another.
182 * The power must be integral.
183 * q3 = qpowi(q1, q2);
186 qpowi(NUMBER
*q1
, NUMBER
*q2
)
190 ZVALUE num
, zden
, z2
;
193 math_error("Raising number to fractional power");
199 sign
= (num
.sign
&& zisodd(z2
));
204 * Check for trivial cases first.
206 if (ziszero(num
) && !ziszero(z2
)) { /* zero raised to a power */
208 math_error("Zero raised to negative power");
211 return qlink(&_qzero_
);
213 if (zisunit(num
) && zisunit(zden
)) { /* 1 or -1 raised to a power */
214 r
= (sign
? q1
: &_qone_
);
218 if (ziszero(z2
)) /* raising to zeroth power */
219 return qlink(&_qone_
);
220 if (zisunit(z2
)) { /* raising to power 1 or -1 */
226 * Not a trivial case. Do the real work.
230 zpowi(num
, z2
, &r
->num
);
232 zpowi(zden
, z2
, &r
->den
);
244 * Given the legs of a right triangle, compute its hypotenuse within
245 * the specified error. This is sqrt(a^2 + b^2).
248 qhypot(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*epsilon
)
250 NUMBER
*tmp1
, *tmp2
, *tmp3
;
252 if (qiszero(epsilon
)) {
253 math_error("Zero epsilon value for hypot");
262 tmp3
= qqadd(tmp1
, tmp2
);
265 tmp1
= qsqrt(tmp3
, epsilon
, 24L);
272 * Given one leg of a right triangle with unit hypotenuse, calculate
273 * the other leg within the specified error. This is sqrt(1 - a^2).
274 * If the wantneg flag is nonzero, then negative square root is returned.
277 qlegtoleg(NUMBER
*q
, NUMBER
*epsilon
, BOOL wantneg
)
279 NUMBER
*res
, *qtmp1
, *qtmp2
;
282 if (qiszero(epsilon
)) {
283 math_error("Zero epsilon value for legtoleg");
287 return qlink(&_qzero_
);
290 return qlink(&_qnegone_
);
291 return qlink(&_qone_
);
295 if (zrel(num
, q
->den
) >= 0) {
296 math_error("Leg too large in legtoleg");
300 qtmp2
= qsub(&_qone_
, qtmp1
);
302 res
= qsqrt(qtmp2
, epsilon
, 24L);
314 * Compute the square root of a real number.
315 * Type of rounding if any depends on rnd.
316 * If rnd & 32 is nonzero, result is exact for square numbers;
317 * If rnd & 64 is nonzero, the negative square root is returned;
318 * If rnd < 32, result is rounded to a multiple of epsilon
319 * up, down, etc. depending on bits 0, 2, 4 of v.
323 qsqrt(NUMBER
*q1
, NUMBER
*epsilon
, long rnd
)
326 ZVALUE tmp1
, tmp2
, quo
, mul
, divisor
;
327 long s1
, s2
, up
, RR
, RS
;
331 math_error("Square root of negative number");
335 return qlink(&_qzero_
);
336 sign
= (rnd
& 64) != 0;
337 if (qiszero(epsilon
)) {
338 math_error("Zero epsilon for qsqrt");
346 RS
^= epsilon
->num
.sign
;
348 RS
^= sign
^ epsilon
->num
.sign
;
350 RS
^= epsilon
->num
.sign
;
351 RR
= zisunit(q1
->den
) && qisunit(epsilon
);
352 if (rnd
& 32 || RR
) {
353 s1
= zsqrt(q1
->num
, &tmp1
, RS
);
357 return qlink(&_qzero_
);
365 s2
= zsqrt(q1
->den
, &tmp2
, 0);
378 zsquare(epsilon
->den
, &tmp1
);
379 zmul(tmp1
, q1
->num
, &tmp2
);
381 zsquare(epsilon
->num
, &tmp1
);
382 zmul(tmp1
, q1
->den
, &divisor
);
385 zshift(tmp2
, 2, &tmp1
);
387 s1
= zquo(tmp1
, divisor
, &quo
, 16);
389 s2
= zsqrt(quo
, &tmp1
, s1
? s1
< 0 : 16);
390 zshift(tmp1
, -1, &mul
);
391 up
= (*tmp1
.v
& 1) ? s1
+ s2
: -1;
394 s1
= zquo(tmp2
, divisor
, &quo
, 0);
396 s2
= zsqrt(quo
, &mul
, 0);
397 up
= (s1
+ s2
) ? 0 : -1;
401 up
= (long)((RS
^ *mul
.v
) & 1);
406 zadd(mul
, _one_
, &tmp2
);
414 return qlink(&_qzero_
);
417 zreduce(mul
, etemp
.den
, &tmp1
, &r
->den
);
420 zmul(tmp1
, etemp
.num
, &r
->num
);
427 * Calculate the integral part of the square root of a number.
428 * Example: qisqrt(13) = 3.
437 math_error("Square root of negative number");
441 return qlink(&_qzero_
);
444 (void) zsqrt(q
->num
, &r
->num
,0);
447 zquo(q
->num
, q
->den
, &tmp
, 0);
448 (void) zsqrt(tmp
, &r
->num
,0);
454 * Return whether or not a number is an exact square.
461 flag
= zissquare(q
->num
);
462 if (qisint(q
) || !flag
)
464 return zissquare(q
->den
);
469 * Compute the greatest integer of the Kth root of a number.
470 * Example: qiroot(85, 3) = 4.
473 qiroot(NUMBER
*q1
, NUMBER
*q2
)
478 if (qisneg(q2
) || qiszero(q2
) || qisfrac(q2
)) {
479 math_error("Taking number to bad root value");
483 return qlink(&_qzero_
);
484 if (qisone(q1
) || qisone(q2
))
490 zroot(q1
->num
, q2
->num
, &r
->num
);
493 zquo(q1
->num
, q1
->den
, &tmp
, 0);
494 zroot(tmp
, q2
->num
, &r
->num
);
501 * Return the greatest integer of the base 2 log of a number.
502 * This is the number such that 1 <= x / log2(x) < 2.
503 * Examples: qilog2(8) = 3, qilog2(1.3) = 1, qilog2(1/7) = -3.
506 * q number to take log of
511 long n
; /* power of two */
512 int c
; /* result of comparison */
513 ZVALUE tmp1
, tmp2
; /* temporary values */
516 math_error("Zero argument for ilog2");
520 return zhighbit(q
->num
);
523 n
= zhighbit(tmp1
) - zhighbit(q
->den
);
525 c
= zrel(tmp1
, q
->den
);
527 zshift(q
->den
, n
, &tmp2
);
528 c
= zrel(tmp1
, tmp2
);
531 zshift(tmp1
, -n
, &tmp2
);
532 c
= zrel(tmp2
, q
->den
);
542 * Return the greatest integer of the base 10 log of a number.
543 * This is the number such that 1 <= x / log10(x) < 10.
544 * Examples: qilog10(100) = 2, qilog10(12.3) = 1, qilog10(.023) = -2.
547 * q number to take log of
552 long n
; /* log value */
553 ZVALUE tmp1
, tmp2
; /* temporary values */
556 math_error("Zero argument for ilog10");
562 return zlog10(tmp1
, NULL
);
564 * The number is not an integer.
565 * Compute the result if the number is greater than one.
567 if (zrel(tmp1
, q
->den
) > 0) {
568 zquo(tmp1
, q
->den
, &tmp2
, 0);
569 n
= zlog10(tmp2
, NULL
);
574 * Here if the number is less than one.
575 * If the number is the inverse of a power of ten, then the
576 * obvious answer will be off by one. Subtracting one if the
577 * number is the inverse of an integer will fix it.
580 zsub(q
->den
, _one_
, &tmp2
);
582 zquo(q
->den
, tmp1
, &tmp2
, 0);
583 n
= -zlog10(tmp2
, NULL
) - 1;
589 * Return the integer floor of the logarithm of a number relative to
590 * a specified integral base.
593 qilog(NUMBER
*q
, ZVALUE base
)
602 return qlink(&_qzero_
);
604 return itoq(zlog(q
->num
, base
));
607 if (zrel(tmp1
, q
->den
) > 0) {
608 zquo(tmp1
, q
->den
, &tmp2
, 0);
609 n
= zlog(tmp2
, base
);
614 zsub(q
->den
, _one_
, &tmp2
);
616 zquo(q
->den
, tmp1
, &tmp2
, 0);
617 n
= -zlog(tmp2
, base
) - 1;
624 * Return the number of digits in the representation to a specified
625 * base of the integral part of a number.
627 * Examples: qdigits(3456,10) = 4, qdigits(-23.45, 10) = 2.
629 * One should remember these special cases:
631 * digits(12.3456) == 2 computes with the integer part only
632 * digits(-1234) == 4 computes with the absolute value only
633 * digits(0) == 1 specical case
634 * digits(-0.123) == 1 combination of all of the above
637 * q number to count digits of
640 qdigits(NUMBER
*q
, ZVALUE base
)
642 long n
; /* number of digits */
643 ZVALUE temp
; /* temporary value */
645 if (zabsrel(q
->num
, q
->den
) < 0)
648 return 1 + zlog(q
->num
, base
);
649 zquo(q
->num
, q
->den
, &temp
, 2);
650 n
= 1 + zlog(temp
, base
);
657 * Return the digit at the specified place in the expansion to specified
658 * base of a rational number. The places specified by dpos are numbered from
659 * the "units" place just before the "decimal" point, so that negative
660 * dpos indicates the (-dpos)th place to the right of the point.
661 * Examples: qdigit(1234.5678, 1, 10) = 3, qdigit(1234.5678, -3, 10) = 7.
662 * The signs of the number and the base are ignored.
665 qdigit(NUMBER
*q
, ZVALUE dpos
, ZVALUE base
)
670 ZVALUE A
, B
, C
; /* temporary integers */
674 * In the first stage, q is expressed as base^k * N/D where
679 if (ziszero(base
) || zisunit(base
))
681 if (qiszero(q
) || (qisint(q
) && zisneg(dpos
)) ||
682 (zge31b(dpos
) && !zisneg(dpos
)))
683 return qlink(&_qzero_
);
684 k
= zfacrem(q
->num
, base
, &N
);
686 k
= zgcdrem(q
->den
, base
, &D
);
688 zequo(q
->den
, D
, &A
);
706 if (zrel(dpos
, K
) >= 0) {
721 return qlink(&_qzero_
);
725 zpowermod(base
, A
, D
, &C
);
733 zmodinv(D
, base
, &B
);
744 zmod(B
, base
, &A
, 0);
748 return qlink(&_qzero_
);
752 return qlink(&_qone_
);
761 * Return whether or not a bit is set at the specified bit position in a
762 * number. The lowest bit of the integral part of a number is the zeroth
763 * bit position. Negative bit positions indicate bits to the right of the
764 * binary decimal point. Examples: qdigit(17.1, 0) = 1, qdigit(17.1, -1) = 0.
767 qisset(NUMBER
*q
, long n
)
769 NUMBER
*qtmp1
, *qtmp2
;
774 * Zero number or negative bit position place of integer is trivial.
776 if (qiszero(q
) || (qisint(q
) && (n
< 0)))
779 * For non-negative bit positions, answer is easy.
783 return zisset(q
->num
, n
);
784 zquo(q
->num
, q
->den
, &ztmp
, 2);
785 res
= zisset(ztmp
, n
);
790 * Fractional value and want negative bit position, must work harder.
792 qtmp1
= qscale(q
, -n
);
795 res
= ((qtmp2
->num
.v
[0] & 0x01) != 0);
802 * Compute the factorial of an integer.
811 math_error("Non-integral factorial");
814 if (qiszero(q
) || zisone(q
->num
))
815 return qlink(&_qone_
);
817 zfact(q
->num
, &r
->num
);
823 * Compute the product of the primes less than or equal to a number.
832 math_error("Non-integral factorial");
836 zpfact(q
->num
, &r
->num
);
842 * Compute the lcm of all the numbers less than or equal to a number.
851 math_error("Non-integral lcmfact");
855 zlcmfact(q
->num
, &r
->num
);
861 * Compute the permutation function q1 * (q1-1) * ... * (q1-q2+1).
864 qperm(NUMBER
*q1
, NUMBER
*q2
)
867 NUMBER
*qtmp1
, *qtmp2
;
871 math_error("Non-integral second arg for permutation");
875 return qlink(&_qone_
);
878 if (qisint(q1
) && !qisneg(q1
)) {
879 if (qrel(q2
, q1
) > 0)
880 return qlink(&_qzero_
);
883 zperm(q1
->num
, q2
->num
, &r
->num
);
887 if (zge31b(q2
->num
)) {
888 math_error("Too large arg2 for permutation");
897 qtmp2
= qmul(r
, qtmp1
);
912 qtmp1
= qqdiv(r
, qtmp2
);
923 * Compute the combinatorial function q(q - 1) ...(q - n + 1)/n!
924 * n is to be a nonnegative integer
927 qcomb(NUMBER
*q
, NUMBER
*n
)
934 if (!qisint(n
) || qisneg(n
)) {
935 math_error("Bad second arg in call to qcomb!");
939 switch (zcomb(q
->num
, n
->num
, &z
)) {
941 return qlink(&_qzero_
);
943 return qlink(&_qone_
);
945 return qlink(&_qnegone_
);
977 * Compute the Bernoulli number with index n
978 * For even positive n, newly calculated values for all even indices up
979 * to n are stored in table at B_table.
984 long n
, i
, k
, m
, nn
, dd
;
986 NUMBER
*s
, *s1
, *c
, *c1
, *t
;
990 return qlink(&_qneghalf_
);
992 if (zisodd(z
) || z
.sign
)
993 return qlink(&_qzero_
);
1002 return qlink(&_qone_
);
1007 return qlink(B_table
[m
]);
1009 if (m
>= B_allocnum
) {
1010 k
= (m
/QALLOCNUM
+ 1) * QALLOCNUM
;
1011 sz
= k
* sizeof(NUMBER
*);
1012 if (sz
< (size_t) k
)
1014 if (B_allocnum
== 0)
1015 p
= (NUMBER
**) malloc(sz
);
1017 p
= (NUMBER
**) realloc(B_table
, sz
);
1023 for (k
= B_num
; k
<= m
; k
++) {
1029 s
= qsub(&_qonehalf_
, c
);
1031 for (i
= 0; i
< k
; i
++) {
1032 c1
= qmuli(c
, nn
--);
1034 c
= qdivi(c1
, dd
++);
1036 c1
= qmuli(c
, nn
--);
1038 c
= qdivi(c1
, dd
++);
1040 t
= qmul(c
, B_table
[i
]);
1050 return qlink(B_table
[m
]);
1060 for (k
= 0; k
< B_num
; k
++)
1071 * Compute the Euler number with index n = z
1072 * For even positive n, newly calculated values with all even indices up
1073 * to n are stored in E_table for later quick access.
1078 long i
, k
, m
, n
, nn
, dd
;
1080 NUMBER
*s
, *s1
, *c
, *c1
, *t
;
1085 return qlink(&_qone_
);
1086 if (zisodd(z
) || zisneg(z
))
1087 return qlink(&_qzero_
);
1093 return qlink(E_table
[m
]);
1094 sz
= (m
+ 1) * sizeof(NUMBER
*);
1095 if (sz
< (size_t) m
+ 1)
1098 p
= (NUMBER
**) realloc(E_table
, sz
);
1100 p
= (NUMBER
**) malloc(sz
);
1104 for (k
= E_num
; k
<= m
; k
++) {
1108 s
= qlink(&_qnegone_
);
1110 for (i
= 0; i
< k
; i
++) {
1111 c1
= qmuli(c
, nn
--);
1113 c
= qdivi(c1
, dd
++);
1115 c1
= qmuli(c
, nn
--);
1117 c
= qdivi(c1
, dd
++);
1119 t
= qmul(c
, E_table
[i
]);
1129 return qlink(E_table
[m
]);
1139 for (k
= 0; k
< E_num
; k
++)
1149 * Catalan numbers: catalan(n) = comb(2*n, n)/(n+1)
1150 * To be called only with integer q
1159 return qlink(&_qzero_
);
1173 * Compute the Jacobi function (a / b).
1174 * -1 => a is not quadratic residue mod b
1175 * 1 => b is composite, or a is quad residue of b
1176 * 0 => b is even or b < 0
1179 qjacobi(NUMBER
*q1
, NUMBER
*q2
)
1181 if (qisfrac(q1
) || qisfrac(q2
)) {
1182 math_error("Non-integral arguments for jacobi");
1185 return itoq((long) zjacobi(q1
->num
, q2
->num
));
1190 * Compute the Fibonacci number F(n).
1198 math_error("Non-integral Fibonacci number");
1202 zfib(q
->num
, &r
->num
);
1208 * Truncate a number to the specified number of decimal places.
1211 qtrunc(NUMBER
*q1
, NUMBER
*q2
)
1216 if (qisfrac(q2
) || !zistiny(q2
->num
)) {
1217 math_error("Bad number of places for qtrunc");
1221 e
= qtenpow(-places
);
1222 r
= qmappr(q1
, e
, 2);
1231 * Truncate a number to the specified number of binary places.
1232 * Specifying zero places makes the result identical to qint.
1235 qbtrunc(NUMBER
*q1
, NUMBER
*q2
)
1240 if (qisfrac(q2
) || !zistiny(q2
->num
)) {
1241 math_error("Bad number of places for qtrunc");
1245 e
= qbitvalue(-places
);
1246 r
= qmappr(q1
, e
, 2);
1253 * Round a number to a specified number of binary places.
1256 qbround(NUMBER
*q
, long places
, long rnd
)
1261 return qlink(&_qzero_
);
1263 places
-= qilog2(q
) + 1;
1264 e
= qbitvalue(-places
);
1265 r
= qmappr(q
, e
, rnd
& 31);
1271 * Round a number to a specified number of decimal digits.
1274 qround(NUMBER
*q
, long places
, long rnd
)
1279 return qlink(&_qzero_
);
1281 places
-= qilog10(q
) + 1;
1282 e
= qtenpow(-places
);
1283 r
= qmappr(q
, e
, rnd
& 31);
1289 * Approximate a number to nearest multiple of a given number. Whether
1290 * rounding is down, up, etc. is determined by rnd.
1293 qmappr(NUMBER
*q
, NUMBER
*e
, long rnd
)
1296 ZVALUE tmp1
, tmp2
, mul
;
1301 return qlink(&_qzero_
);
1302 zmul(q
->num
, e
->den
, &tmp1
);
1303 zmul(q
->den
, e
->num
, &tmp2
);
1304 zquo(tmp1
, tmp2
, &mul
, rnd
);
1309 return qlink(&_qzero_
);
1312 zreduce(mul
, e
->den
, &tmp1
, &r
->den
);
1313 zmul(tmp1
, e
->num
, &r
->num
);
1321 * Determine the smallest-denominator rational number in the interval of
1322 * length abs(epsilon) (< 1) with centre or one end at q, or to determine
1323 * the number nearest above or nearest below q with denominator
1324 * not exceeding abs(epsilon).
1325 * Whether the approximation is nearest above or nearest below is
1326 * determined by rnd and the signs of epsilon and q.
1330 qcfappr(NUMBER
*q
, NUMBER
*epsilon
, long rnd
)
1332 NUMBER
*res
, etemp
, *epsilon1
;
1333 ZVALUE num
, zden
, oldnum
, oldden
;
1334 ZVALUE rem
, oldrem
, quot
;
1335 ZVALUE tmp1
, tmp2
, tmp3
, tmp4
;
1341 BOOL useold
= FALSE
;
1343 if (qiszero(epsilon
) || qisint(q
))
1346 esign
= epsilon
->num
.sign
;
1349 bnddencase
= (zrel(etemp
.num
, etemp
.den
) >= 0);
1351 zquo(etemp
.num
, etemp
.den
, &denbnd
, 0);
1352 if (zrel(q
->den
, denbnd
) <= 0) {
1358 epsilon1
= qscale(epsilon
, -1);
1360 epsilon1
= qlink(epsilon
);
1361 zreduce(q
->den
, epsilon1
->den
, &tmp1
, &g
);
1362 zmul(epsilon1
->num
, tmp1
, &f
);
1367 if (rnd
& 16 && !zistwo(q
->den
)) {
1373 if (rnd
& 2 && q
->num
.sign
^ esign
)
1375 if (rnd
& 4 && esign
)
1380 zcopy(q
->den
, &oldrem
);
1381 zdiv(q
->num
, q
->den
, &num
, &rem
, 0);
1385 zmul(f
, zden
, &tmp1
);
1386 zmul(g
, rem
, &tmp2
);
1387 if (ziszero(rem
) || (s
>= 0 && zrel(tmp1
,tmp2
) >= 0))
1392 zdiv(oldrem
, rem
, "
, &tmp1
, 0);
1396 zmul(quot
, zden
, &tmp1
);
1397 zadd(tmp1
, oldden
, &tmp2
);
1402 zmul(quot
, num
, &tmp1
);
1403 zadd(tmp1
, oldnum
, &tmp2
);
1410 if (zrel(zden
, denbnd
) >= 0)
1419 zsub(zden
, denbnd
, &tmp1
);
1420 zquo(tmp1
, oldden
, &k
, 1);
1438 zsub(tmp1
, tmp2
, &tmp3
);
1441 zmul(f
, oldden
, &tmp1
);
1442 zmul(g
, oldrem
, &tmp2
);
1445 zadd(tmp1
, tmp2
, &tmp4
);
1448 zquo(tmp3
, tmp4
, &k
, 0);
1452 if (!useold
&& !ziszero(k
)) {
1453 zmul(k
, oldnum
, &tmp1
);
1454 zsub(num
, tmp1
, &tmp2
);
1458 zmul(k
, oldden
, &tmp1
);
1459 zsub(zden
, tmp1
, &tmp2
);
1464 if (bnddencase
&& s
== 0) {
1465 zmul(k
, oldrem
, &tmp1
);
1466 zadd(rem
, tmp1
, &tmp2
);
1470 zmul(rem
, oldden
, &tmp1
);
1471 zmul(zden
, oldrem
, &tmp2
);
1472 useold
= (zrel(tmp1
, tmp2
) >= 0);
1476 if (!bnddencase
|| s
<= 0)
1497 * Calculate the nearest-above, or nearest-below, or nearest, number
1498 * with denominator less than the given number, the choice between
1499 * possibilities being determined by the parameter rnd.
1502 qcfsim(NUMBER
*q
, long rnd
)
1505 ZVALUE tmp1
, tmp2
, den1
, den2
;
1508 if (qiszero(q
) && rnd
& 26)
1509 return qlink(&_qzero_
);
1518 if ((rnd
& 8) && !(rnd
& 16))
1519 return qlink(&_qzero_
);
1524 if (zistwo(q
->den
)) {
1528 zadd(q
->num
, _one_
, &tmp1
);
1530 zsub(q
->num
, _one_
, &tmp1
);
1532 zshift(tmp1
, -1, &res
->num
);
1540 zmodinv(q
->num
, q
->den
, &den1
);
1542 zsub(q
->den
, den1
, &den2
);
1543 if (s
> 0 || ((zrel(den1
, den2
) < 0) ^ (!(rnd
& 16)))) {
1546 zmul(den2
, q
->num
, &tmp1
);
1547 zadd(tmp1
, _one_
, &tmp2
);
1549 zequo(tmp2
, q
->den
, &res
->num
);
1556 zmul(den1
, q
->num
, &tmp1
);
1557 zsub(tmp1
, _one_
, &tmp2
);
1559 zequo(tmp2
, q
->den
, &res
->num
);
1567 * Return an indication on whether or not two fractions are approximately
1568 * equal within the specified epsilon. Returns negative if the absolute value
1569 * of the difference between the two numbers is less than epsilon, zero if
1570 * the difference is equal to epsilon, and positive if the difference is
1571 * greater than epsilon.
1574 qnear(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*epsilon
)
1577 NUMBER qtemp
, etemp
, *qq
;
1582 if (qiszero(epsilon
))
1586 if (qiszero(epsilon
))
1587 return qcmp(q1
, q2
);
1591 return qrel(&qtemp
, &etemp
);
1596 return qrel(&qtemp
, &etemp
);
1601 res
= qrel(&qtemp
, &etemp
);
1608 * Compute the gcd (greatest common divisor) of two numbers.
1609 * q3 = qgcd(q1, q2);
1612 qgcd(NUMBER
*q1
, NUMBER
*q2
)
1619 if (qisfrac(q1
) || qisfrac(q2
)) {
1621 zgcd(q1
->num
, q2
->num
, &q
->num
);
1622 zlcm(q1
->den
, q2
->den
, &q
->den
);
1629 if (qisunit(q1
) || qisunit(q2
))
1630 return qlink(&_qone_
);
1631 zgcd(q1
->num
, q2
->num
, &z
);
1634 return qlink(&_qone_
);
1643 * Compute the lcm (least common multiple) of two numbers.
1644 * q3 = qlcm(q1, q2);
1647 qlcm(NUMBER
*q1
, NUMBER
*q2
)
1651 if (qiszero(q1
) || qiszero(q2
))
1652 return qlink(&_qzero_
);
1660 zlcm(q1
->num
, q2
->num
, &q
->num
);
1661 if (qisfrac(q1
) || qisfrac(q2
))
1662 zgcd(q1
->den
, q2
->den
, &q
->den
);
1668 * Remove all occurrences of the specified factor from a number.
1669 * Returned number is always positive or zero.
1672 qfacrem(NUMBER
*q1
, NUMBER
*q2
)
1678 if (qisfrac(q1
) || qisfrac(q2
)) {
1679 math_error("Non-integers for factor removal");
1685 return qlink(&_qzero_
);
1686 count
= zfacrem(q1
->num
, q2
->num
, &tmp
);
1689 return qlink(&_qone_
);
1691 if (count
== 0 && !qisneg(q1
)) {
1702 * Divide one number by the gcd of it with another number repeatedly until
1703 * the number is relatively prime.
1706 qgcdrem(NUMBER
*q1
, NUMBER
*q2
)
1711 if (qisfrac(q1
) || qisfrac(q2
)) {
1712 math_error("Non-integers for gcdrem");
1716 return qlink(&_qone_
);
1718 return qlink(&_qzero_
);
1719 if (zgcdrem(q1
->num
, q2
->num
, &tmp
) == 0)
1723 return qlink(&_qone_
);
1725 if (zcmp(q1
->num
, tmp
) == 0) {
1736 * Return the lowest prime factor of a number.
1737 * Search is conducted for the specified number of primes.
1738 * Returns one if no factor was found.
1741 qlowfactor(NUMBER
*q1
, NUMBER
*q2
)
1743 unsigned long count
;
1745 if (qisfrac(q1
) || qisfrac(q2
)) {
1746 math_error("Non-integers for lowfactor");
1749 count
= ztoi(q2
->num
);
1750 if (count
> PIX_32B
) {
1751 math_error("lowfactor count is too large");
1754 return utoq(zlowfactor(q1
->num
, count
));
1758 * Return the number of places after the decimal point needed to exactly
1759 * represent the specified number as a real number. Integers return zero,
1760 * and non-terminating decimals return minus one. Examples:
1761 * qdecplaces(1.23) = 2, qdecplaces(3) = 0, qdecplaces(1/7) = -1.
1764 qdecplaces(NUMBER
*q
)
1766 long twopow
, fivepow
;
1771 if (qisint(q
)) /* no decimal places if number is integer */
1774 * The number of decimal places of a fraction in lowest terms is finite
1775 * if an only if the denominator is of the form 2^A * 5^B, and then the
1776 * number of decimal places is equal to MAX(A, B).
1782 fivepow
= zfacrem(q
->den
, five
, &tmp
);
1783 if (!zisonebit(tmp
)) {
1787 twopow
= zlowbit(tmp
);
1789 if (twopow
< fivepow
)
1796 * Return, if possible, the minimum number of places after the decimal
1797 * point needed to exactly represent q with the specified base.
1798 * Integers return 0 and numbers with non-terminating expansions -1.
1799 * Returns -2 if base inadmissible
1802 qplaces(NUMBER
*q
, ZVALUE base
)
1807 if (base
.len
== 1 && base
.v
[0] == 10)
1808 return qdecplaces(q
);
1809 if (ziszero(base
) || zisunit(base
))
1813 if (zisonebit(base
)) {
1814 if (!zisonebit(q
->den
))
1816 return 1 + (zlowbit(q
->den
) - 1)/zlowbit(base
);
1818 count
= zgcdrem(q
->den
, base
, &tmp
);
1829 * Perform a probabilistic primality test (algorithm P in Knuth).
1830 * Returns FALSE if definitely not prime, or TRUE if probably prime.
1832 * The absolute value of the 2nd arg determines how many times
1833 * to check for primality. If 2nd arg < 0, then the trivial
1834 * check is omitted. The 3rd arg determines how tests to
1838 qprimetest(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*q3
)
1840 if (qisfrac(q1
) || qisfrac(q2
) || qisfrac(q3
)) {
1841 math_error("Bad arguments for ptest");
1844 if (zge24b(q2
->num
)) {
1845 math_error("ptest count >= 2^24");
1848 return zprimetest(q1
->num
, ztoi(q2
->num
), q3
->num
);