2 * qmath - declarations for extended precision rational arithmetic
4 * Copyright (C) 1999-2007 David I. Bell
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.3 $
21 * @(#) $Id: qmath.h,v 30.3 2013/08/11 08:41:38 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/RCS/qmath.h,v $
24 * Under source code control: 1993/07/30 19:42:47
25 * File existed as early as: 1993
27 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
31 #if !defined(__QMATH_H__)
35 #if defined(CALC_SRC) /* if we are building from the calc source tree */
38 # include <calc/zmath.h>
42 #define INITCONSTCOUNT 9 /* number of initnumbs[] pre-defined constants */
45 * Rational arithmetic definitions.
48 ZVALUE num
; /* numerator (containing sign) */
49 ZVALUE den
; /* denominator (always positive) */
50 long links
; /* number of links to this value */
51 struct number
*next
; /* pointer to next number */
54 typedef struct number NUMBER
;
59 * Input. output, allocation, and conversion routines.
61 E_FUNC NUMBER
*qalloc(void);
62 E_FUNC NUMBER
*qcopy(NUMBER
*q
);
63 E_FUNC NUMBER
*uutoq(FULL i1
, FULL i2
);
64 E_FUNC NUMBER
*iitoq(long i1
, long i2
);
65 E_FUNC NUMBER
*str2q(char *str
);
66 E_FUNC NUMBER
*itoq(long i
);
67 E_FUNC NUMBER
*utoq(FULL i
);
68 E_FUNC NUMBER
*stoq(SFULL i
);
69 E_FUNC
long qtoi(NUMBER
*q
);
70 E_FUNC FULL
qtou(NUMBER
*q
);
71 E_FUNC SFULL
qtos(NUMBER
*q
);
72 E_FUNC
long qparse(char *str
, int flags
);
73 E_FUNC
void qfreenum(NUMBER
*q
);
74 E_FUNC
void qprintnum(NUMBER
*q
, int mode
);
75 E_FUNC
void qprintff(NUMBER
*q
, long width
, long precision
);
76 E_FUNC
void qprintfe(NUMBER
*q
, long width
, long precision
);
77 E_FUNC
void qprintfr(NUMBER
*q
, long width
, BOOL force
);
78 E_FUNC
void qprintfd(NUMBER
*q
, long width
);
79 E_FUNC
void qprintfx(NUMBER
*q
, long width
);
80 E_FUNC
void qprintfb(NUMBER
*q
, long width
);
81 E_FUNC
void qprintfo(NUMBER
*q
, long width
);
82 E_FUNC
void qprintf(char *, ...);
83 E_FUNC
void shownumbers(void);
84 E_FUNC
void showredcdata(void);
85 E_FUNC
void freeredcdata(void);
86 E_FUNC
void fitprint(NUMBER
*, long);
91 * Basic numeric routines.
93 E_FUNC NUMBER
*qaddi(NUMBER
*q
, long i
);
94 E_FUNC NUMBER
*qmuli(NUMBER
*q
, long i
);
95 E_FUNC NUMBER
*qdivi(NUMBER
*q
, long i
);
96 E_FUNC NUMBER
*qqadd(NUMBER
*q1
, NUMBER
*q2
);
97 E_FUNC NUMBER
*qsub(NUMBER
*q1
, NUMBER
*q2
);
98 E_FUNC NUMBER
*qmul(NUMBER
*q1
, NUMBER
*q2
);
99 E_FUNC NUMBER
*qqdiv(NUMBER
*q1
, NUMBER
*q2
);
100 E_FUNC NUMBER
*qquo(NUMBER
*q1
, NUMBER
*q2
, long rnd
);
101 E_FUNC NUMBER
*qmod(NUMBER
*q1
, NUMBER
*q2
, long rnd
);
102 E_FUNC NUMBER
*qmin(NUMBER
*q1
, NUMBER
*q2
);
103 E_FUNC NUMBER
*qmax(NUMBER
*q1
, NUMBER
*q2
);
104 E_FUNC NUMBER
*qand(NUMBER
*q1
, NUMBER
*q2
);
105 E_FUNC NUMBER
*qor(NUMBER
*q1
, NUMBER
*q2
);
106 E_FUNC NUMBER
*qxor(NUMBER
*q1
, NUMBER
*q2
);
107 E_FUNC NUMBER
*qandnot(NUMBER
*q1
, NUMBER
*q2
);
108 E_FUNC NUMBER
*qcomp(NUMBER
*q
);
109 E_FUNC NUMBER
*qpowermod(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*q3
);
110 E_FUNC NUMBER
*qpowi(NUMBER
*q1
, NUMBER
*q2
);
111 E_FUNC NUMBER
*qsquare(NUMBER
*q
);
112 E_FUNC NUMBER
*qneg(NUMBER
*q
);
113 E_FUNC NUMBER
*qsign(NUMBER
*q
);
114 E_FUNC NUMBER
*qint(NUMBER
*q
);
115 E_FUNC NUMBER
*qfrac(NUMBER
*q
);
116 E_FUNC NUMBER
*qnum(NUMBER
*q
);
117 E_FUNC NUMBER
*qden(NUMBER
*q
);
118 E_FUNC NUMBER
*qinv(NUMBER
*q
);
119 E_FUNC NUMBER
*qqabs(NUMBER
*q
);
120 E_FUNC NUMBER
*qinc(NUMBER
*q
);
121 E_FUNC NUMBER
*qdec(NUMBER
*q
);
122 E_FUNC NUMBER
*qshift(NUMBER
*q
, long n
);
123 E_FUNC NUMBER
*qtrunc(NUMBER
*q1
, NUMBER
*q2
);
124 E_FUNC NUMBER
*qround(NUMBER
*q
, long places
, long rnd
);
125 E_FUNC NUMBER
*qbtrunc(NUMBER
*q1
, NUMBER
*q2
);
126 E_FUNC NUMBER
*qbround(NUMBER
*q
, long places
, long rnd
);
127 E_FUNC NUMBER
*qscale(NUMBER
*q
, long i
);
128 E_FUNC BOOL
qdivides(NUMBER
*q1
, NUMBER
*q2
);
129 E_FUNC BOOL
qcmp(NUMBER
*q1
, NUMBER
*q2
);
130 E_FUNC BOOL
qcmpi(NUMBER
*q
, long i
);
131 E_FUNC FLAG
qrel(NUMBER
*q1
, NUMBER
*q2
);
132 E_FUNC FLAG
qreli(NUMBER
*q
, long i
);
133 E_FUNC BOOL
qisset(NUMBER
*q
, long i
);
137 * More complicated numeric functions.
139 E_FUNC NUMBER
*qcomb(NUMBER
*q1
, NUMBER
*q2
);
140 E_FUNC NUMBER
*qgcd(NUMBER
*q1
, NUMBER
*q2
);
141 E_FUNC NUMBER
*qlcm(NUMBER
*q1
, NUMBER
*q2
);
142 E_FUNC NUMBER
*qfact(NUMBER
*q
);
143 E_FUNC NUMBER
*qpfact(NUMBER
*q
);
144 E_FUNC NUMBER
*qminv(NUMBER
*q1
, NUMBER
*q2
);
145 E_FUNC NUMBER
*qfacrem(NUMBER
*q1
, NUMBER
*q2
);
146 E_FUNC NUMBER
*qperm(NUMBER
*q1
, NUMBER
*q2
);
147 E_FUNC NUMBER
*qgcdrem(NUMBER
*q1
, NUMBER
*q2
);
148 E_FUNC NUMBER
*qlowfactor(NUMBER
*q1
, NUMBER
*q2
);
149 E_FUNC NUMBER
*qfib(NUMBER
*q
);
150 E_FUNC NUMBER
*qcfappr(NUMBER
*q
, NUMBER
*epsilon
, long R
);
151 E_FUNC NUMBER
*qcfsim(NUMBER
*q
, long R
);
152 E_FUNC NUMBER
*qisqrt(NUMBER
*q
);
153 E_FUNC NUMBER
*qjacobi(NUMBER
*q1
, NUMBER
*q2
);
154 E_FUNC NUMBER
*qiroot(NUMBER
*q1
, NUMBER
*q2
);
155 E_FUNC NUMBER
*qmappr(NUMBER
*q
, NUMBER
*e
, long R
);
156 E_FUNC NUMBER
*qlcmfact(NUMBER
*q
);
157 E_FUNC NUMBER
*qredcin(NUMBER
*q1
, NUMBER
*q2
);
158 E_FUNC NUMBER
*qredcout(NUMBER
*q1
, NUMBER
*q2
);
159 E_FUNC NUMBER
*qredcmul(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*q3
);
160 E_FUNC NUMBER
*qredcsquare(NUMBER
*q1
, NUMBER
*q2
);
161 E_FUNC NUMBER
*qredcpower(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*q3
);
162 E_FUNC BOOL
qprimetest(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*q3
);
163 E_FUNC BOOL
qissquare(NUMBER
*q
);
164 E_FUNC
long qilog2(NUMBER
*q
);
165 E_FUNC
long qilog10(NUMBER
*q
);
166 E_FUNC NUMBER
*qilog(NUMBER
*q
, ZVALUE base
);
167 E_FUNC BOOL
qcmpmod(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*q3
);
168 E_FUNC BOOL
qquomod(NUMBER
*q1
, NUMBER
*q2
, NUMBER
**quo
, NUMBER
**mod
,
170 E_FUNC FLAG
qnear(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*epsilon
);
171 E_FUNC NUMBER
*qdigit(NUMBER
*q
, ZVALUE dpos
, ZVALUE base
);
172 E_FUNC
long qprecision(NUMBER
*q
);
173 E_FUNC
long qplaces(NUMBER
*q
, ZVALUE base
);
174 E_FUNC
long qdecplaces(NUMBER
*q
);
175 E_FUNC
long qdigits(NUMBER
*q
, ZVALUE base
);
176 E_FUNC
void setepsilon(NUMBER
*q
);
177 E_FUNC NUMBER
*qbitvalue(long i
);
178 E_FUNC NUMBER
*qtenpow(long i
);
182 * Transcendental functions. These all take an epsilon argument to
183 * specify the required accuracy of the calculation.
185 E_FUNC
void qsincos(NUMBER
*q
, long bitnum
, NUMBER
**vs
, NUMBER
**vc
);
186 E_FUNC NUMBER
*qsqrt(NUMBER
*q
, NUMBER
*epsilon
, long R
);
187 E_FUNC NUMBER
*qpower(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*epsilon
);
188 E_FUNC NUMBER
*qroot(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*epsilon
);
189 E_FUNC NUMBER
*qcos(NUMBER
*q
, NUMBER
*epsilon
);
190 E_FUNC NUMBER
*qsin(NUMBER
*q
, NUMBER
*epsilon
);
191 E_FUNC NUMBER
*qexp(NUMBER
*q
, NUMBER
*epsilon
);
192 E_FUNC NUMBER
*qln(NUMBER
*q
, NUMBER
*epsilon
);
193 E_FUNC NUMBER
*qlog(NUMBER
*q
, NUMBER
*epsilon
);
194 E_FUNC NUMBER
*qtan(NUMBER
*q
, NUMBER
*epsilon
);
195 E_FUNC NUMBER
*qsec(NUMBER
*q
, NUMBER
*epsilon
);
196 E_FUNC NUMBER
*qcot(NUMBER
*q
, NUMBER
*epsilon
);
197 E_FUNC NUMBER
*qcsc(NUMBER
*q
, NUMBER
*epsilon
);
198 E_FUNC NUMBER
*qacos(NUMBER
*q
, NUMBER
*epsilon
);
199 E_FUNC NUMBER
*qasin(NUMBER
*q
, NUMBER
*epsilon
);
200 E_FUNC NUMBER
*qatan(NUMBER
*q
, NUMBER
*epsilon
);
201 E_FUNC NUMBER
*qasec(NUMBER
*q
, NUMBER
*epsilon
);
202 E_FUNC NUMBER
*qacsc(NUMBER
*q
, NUMBER
*epsilon
);
203 E_FUNC NUMBER
*qacot(NUMBER
*q
, NUMBER
*epsilon
);
204 E_FUNC NUMBER
*qatan2(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*epsilon
);
205 E_FUNC NUMBER
*qhypot(NUMBER
*q1
, NUMBER
*q2
, NUMBER
*epsilon
);
206 E_FUNC NUMBER
*qcosh(NUMBER
*q
, NUMBER
*epsilon
);
207 E_FUNC NUMBER
*qsinh(NUMBER
*q
, NUMBER
*epsilon
);
208 E_FUNC NUMBER
*qtanh(NUMBER
*q
, NUMBER
*epsilon
);
209 E_FUNC NUMBER
*qcoth(NUMBER
*q
, NUMBER
*epsilon
);
210 E_FUNC NUMBER
*qsech(NUMBER
*q
, NUMBER
*epsilon
);
211 E_FUNC NUMBER
*qcsch(NUMBER
*q
, NUMBER
*epsilon
);
212 E_FUNC NUMBER
*qacosh(NUMBER
*q
, NUMBER
*epsilon
);
213 E_FUNC NUMBER
*qasinh(NUMBER
*q
, NUMBER
*epsilon
);
214 E_FUNC NUMBER
*qatanh(NUMBER
*q
, NUMBER
*epsilon
);
215 E_FUNC NUMBER
*qasech(NUMBER
*q
, NUMBER
*epsilon
);
216 E_FUNC NUMBER
*qacsch(NUMBER
*q
, NUMBER
*epsilon
);
217 E_FUNC NUMBER
*qacoth(NUMBER
*q
, NUMBER
*epsilon
);
218 E_FUNC NUMBER
*qlegtoleg(NUMBER
*q
, NUMBER
*epsilon
, BOOL wantneg
);
219 E_FUNC NUMBER
*qpi(NUMBER
*epsilon
);
220 E_FUNC NUMBER
*qcatalan(NUMBER
*);
221 E_FUNC NUMBER
*qbern(ZVALUE z
);
222 E_FUNC
void qfreebern(void);
223 E_FUNC NUMBER
*qeuler(ZVALUE z
);
224 E_FUNC
void qfreeeuler(void);
228 * pseudo-seed generator
230 E_FUNC NUMBER
*pseudo_seed(void);
234 * external swap functions
236 E_FUNC NUMBER
*swap_b8_in_NUMBER(NUMBER
*dest
, NUMBER
*src
, BOOL all
);
237 E_FUNC NUMBER
*swap_b16_in_NUMBER(NUMBER
*dest
, NUMBER
*src
, BOOL all
);
238 E_FUNC NUMBER
*swap_HALF_in_NUMBER(NUMBER
*dest
, NUMBER
*src
, BOOL all
);
242 * macro expansions to speed this thing up
244 #define qiszero(q) (ziszero((q)->num))
245 #define qisneg(q) (zisneg((q)->num))
246 #define qispos(q) (zispos((q)->num))
247 #define qisint(q) (zisunit((q)->den))
248 #define qisfrac(q) (!zisunit((q)->den))
249 #define qisunit(q) (zisunit((q)->num) && zisunit((q)->den))
250 #define qisone(q) (zisone((q)->num) && zisunit((q)->den))
251 #define qisnegone(q) (zisnegone((q)->num) && zisunit((q)->den))
252 #define qistwo(q) (zistwo((q)->num) && zisunit((q)->den))
253 #define qiseven(q) (zisunit((q)->den) && ziseven((q)->num))
254 #define qisodd(q) (zisunit((q)->den) && zisodd((q)->num))
255 #define qistwopower(q) (zisunit((q)->den) && zistwopower((q)->num))
256 #define qistiny(q) (zistiny((q)->num))
258 #define qhighbit(q) (zhighbit((q)->num))
259 #define qlowbit(q) (zlowbit((q)->num))
260 #define qdivcount(q1, q2) (zdivcount((q1)->num, (q2)->num))
261 #define qlink(q) ((q)->links++, (q))
263 #define qfree(q) {if (--((q)->links) <= 0) qfreenum(q);}
267 * Flags for qparse calls
269 #define QPF_SLASH 0x1 /* allow slash for fractional number */
270 #define QPF_IMAG 0x2 /* allow trailing 'i' for imaginary number */
274 * constants used often by the arithmetic routines
276 EXTERN NUMBER _qzero_
, _qone_
, _qnegone_
, _qonehalf_
, _qneghalf_
, _qonesqbase_
;
277 EXTERN NUMBER _qtwo_
, _qthree_
, _qfour_
, _qten_
;
278 EXTERN NUMBER
* initnumbs
[];
281 #endif /* !__QMATH_H__ */