1 USING THE ARBITRARY PRECISION ROUTINES IN A C PROGRAM
3 Part of the calc release consists of an arbitrary precision math link library.
4 This link library is used by the calc program to perform its own calculations.
5 If you wish, you can ignore the calc program entirely and call the arbitrary
6 precision math routines from your own C programs.
8 The link library is called libcalc.a, and provides routines to handle arbitrary
9 precision arithmetic with integers, rational numbers, or complex numbers.
10 There are also many numeric functions such as factorial and gcd, along
11 with some transcendental functions such as sin and exp.
13 Take a look at the sample sub-directory. It contains a few simple
14 examples of how to use libcalc.a that might be helpful to look at
15 after you have read this file.
21 ...............................................................................
23 . You MUST call libcalc_call_me_first() prior to using libcalc lib functions! .
25 ...............................................................................
27 The function libcalc_call_me_first() takes no args and returns void. You
28 need call libcalc_call_me_first() only once.
34 To use any of these routines in your own programs, you need to include the
35 appropriate include file. These include files are:
37 zmath.h (for integer arithmetic)
38 qmath.h (for rational arithmetic)
39 cmath.h (for complex number arithmetic)
41 You never need to include more than one of the above files, even if you wish
42 to use more than one type of arithmetic, since qmath.h automatically includes
43 zmath.h, and cmath.h automatically includes qmath.h.
45 The prototypes for the available routines are listed in the above include
46 files. Some of these routines are meant for internal use, and so aren't
47 convenient for outside use. So you should read the source for a routine
48 to see if it really does what you think it does. I won't guarantee that
49 obscure internal routines won't change or disappear in future releases!
51 When calc is installed, all of libraries are installed into ${LIBDIR}.
52 All of the calc header files are installed under ${INCDIRCALC}.
54 If CALC_SRC is defined, then the calc header files will assume that
55 they are in or under the current directory. However, most external
56 programs most likely will not be located under calc'c source tree.
57 External programs most likely want to use the installed calc header
58 files under ${INCDIRCALC}. External programs most likely NOT want
61 You need to include the following file to get the symbols and variables
62 related to error handling:
66 External programs may want to compile with:
68 -I${INCDIR} -L${LIBDIR} -lcalc
70 If custom functions are also used, they may want to compile with:
72 -I${INCDIR} -L${LIBDIR} -lcalc -lcustcalc
74 The CALC_SRC symbol should NOT be defined by default. However if you are
75 feeling pedantic you may want to force CALC_SRC to be undefined:
85 The math_error() function is called by the math routines on an error
86 condition, such as malloc failures, division by zero, or some form of
87 an internal computation error. The routine is called in the manner of
88 printf, with a format string and optional arguments:
90 void math_error(char *fmt, ...);
92 Your program must handle math errors in one of three ways:
94 1) Print the error message and then exit
96 There is a math_error() function supplied with the calc library.
97 By default, this routine simply prints a message to stderr and
98 then exits. By simply linking in this link library, any calc
99 errors will result in a error message on stderr followed by
102 2) Use setjmp and longjmp in your program
104 Use setjmp at some appropriate level in your program, and let
105 the longjmp in math_error() return to that level and to allow you
106 to recover from the error. This is what the calc program does.
108 If one sets up calc_matherr_jmpbuf, and then sets
109 calc_use_matherr_jmpbuf to non-zero then math_error() will
110 longjmp back with the return value of calc_use_matherr_jmpbuf.
111 In addition, the last calc error message will be found in
112 calc_err_msg; this error is not printed to stderr. The calc
113 error message will not have a trailing newline.
118 #include "lib_calc.h"
124 if ((error = setjmp(calc_matherr_jmpbuf)) != 0) {
126 /* report the error */
127 printf("Ouch: %s\n", calc_err_msg);
129 /* reinitialize calc after the longjmp */
132 calc_use_matherr_jmpbuf = 1;
134 If calc_use_matherr_jmpbuf is non-zero, then the jmp_buf value
135 calc_matherr_jmpbuf must be initialized by the setjmp() function
136 or your program will crash.
138 3) Supply your own math_error function:
140 void math_error(char *fmt, ...);
142 Your math_error() function may exit or transfer control to outside
143 of the calc library, but it must never return or calc will crash.
145 External programs can obtain the appropriate calc symbols by compiling with:
147 -I${INCDIR} -L${LIBDIR} -lcalc
149 -------------------------
150 PARSE/SCAN ERROR HANDLING
151 -------------------------
153 The scanerror() function is called when calc encounters a parse/scan
154 error. For example, scanerror() is called when calc is given code
157 The variable, calc_print_scanerr_msg, controls if calc prints to stderr,
158 any parse/scan errors. By default, this variable it set to 1 and so
159 parse/scan errors are printed to stderr. By setting this value to zero,
160 parse/scan errors are not printed:
162 #include "lib_calc.h"
164 /* do not print parse/scan errors to stderr */
165 calc_print_scanerr_msg = 0;
167 The last calc math error or calc parse/scan error message is kept
168 in the NUL terminated buffer:
170 char calc_err_msg[MAXERROR+1];
172 The value of calc_print_scanerr_msg does not change the use
173 of the calc_err_msg[] buffer. Messages are stored in that
174 buffer regardless of the calc_print_scanerr_msg value.
176 The calc_print_scanerr_msg and the calc_err_msg[] buffer are declared
177 lib_calc.h include file. The initialized storage for these variables
178 comes from the calc library. The MAXERROR symbol is also declared in
179 the lib_calc.h include file.
181 Your program must handle parse/scan errors in one of two ways:
185 If you do not setup the calc_scanerr_jmpbuf, then when calc
186 encounters a parse/scan error, a message will be printed to
187 stderr and calc will exit.
189 2) Use setjmp and longjmp in your program
191 Use setjmp at some appropriate level in your program, and let
192 the longjmp in scanerror() return to that level and to allow you
193 to recover from the error. This is what the calc program does.
195 If one sets up calc_scanerr_jmpbuf, and then sets
196 calc_use_scanerr_jmpbuf to non-zero then scanerror() will longjmp
197 back with the return with a non-zero code. In addition, the last
198 calc error message will be found in calc_err_msg[]; this error is
199 not printed to stderr. The calc error message will not have a
205 #include "lib_calc.h"
211 /* delay the printing of the parse/scan error */
212 calc_use_scanerr_jmpbuf = 0; /* this is optional */
214 if ((scan_error = setjmp(calc_scanerr_jmpbuf)) != 0) {
216 /* report the parse/scan */
217 if (calc_use_scanerr_jmpbuf == 0) {
218 printf("parse error: %s\n", calc_err_msg);
221 /* initialize calc after the longjmp */
224 calc_use_scanerr_jmpbuf = 1;
226 If calc_use_scanerr_jmpbuf is non-zero, then the jmp_buf value
227 calc_scanerr_jmpbuf must be initialized by the setjmp() function
228 or your program will crash.
230 External programs can obtain the appropriate calc symbols by compiling with:
232 -I${INCDIR} -L${LIBDIR} -lcalc
234 ---------------------------
235 PARSE/SCAN WARNING HANDLING
236 ---------------------------
238 Calc parse/scan warning message are printed to stderr by the warning()
239 function. The routine is called in the manner of printf, with a format
240 string and optional arguments:
242 void warning(char *fmt, ...);
244 The variable, calc_print_scanwarn_msg, controls if calc prints to stderr,
245 any parse/scan warnings. By default, this variable it set to 1 and so
246 parse/scan warnings are printed to stderr. By setting this value to zero,
247 parse/scan warnings are not printed:
249 #include "lib_calc.h"
251 /* do not print parse/scan warnings to stderr */
252 calc_print_scanwarn_msg = 0;
254 The last calc calc parse/scan warning message is kept in the NUL
257 char calc_warn_msg[MAXERROR+1];
259 The value of calc_print_scanwarn_msg does not change the use
260 of the calc_warn_msg[] buffer. Messages are stored in that
261 buffer regardless of the calc_print_scanwarn_msg value.
263 Your program must handle parse/scan warnings in one of two ways:
265 1) print the warning to stderr and continue
267 The warning() from libcalc prints warning messages to
268 stderr and returns. The flow of execution is not changed.
269 This is what calc does by default.
271 2) Supply your own warning function:
273 void warning(char *fmt, ...);
275 Your warning function should simply return when it is finished.
277 External programs can obtain the appropriate calc symbols by compiling with:
279 -I${INCDIR} -L${LIBDIR} -lcalc
286 The output from the routines in the link library normally goes to stdout.
287 You can divert that output to either another FILE handle, or else
288 to a string. Read the routines in zio.c to see what is available.
289 Diversions can be nested.
291 You use math_setfp to divert output to another FILE handle. Calling
292 math_setfp with stdout restores output to stdout.
294 Use math_divertio to begin diverting output into a string. Calling
295 math_getdivertedio will then return a string containing the output, and
296 clears the diversion. The string is reallocated as necessary, but since
297 it is in memory, there are obviously limits on the amount of data that can
298 be diverted into it. The string needs freeing when you are done with it.
300 Calling math_cleardiversions will clear all the diversions to strings, and
301 is useful on an error condition to restore output to a known state. You
302 should also call math_setfp on errors if you had changed that.
304 If you wish to mix your own output with numeric output from the math routines,
305 then you can call math_chr, math_str, math_fill, math_fmt, or math_flush.
306 These routines output single characters, output null-terminated strings,
307 output strings with space filling, output formatted strings like printf, and
308 flush the output. Output from these routines is diverted as described above.
310 You can change the default output mode by calling math_setmode, and you can
311 change the default number of digits printed by calling math_setdigits. These
312 routines return the previous values. The possible modes are described in
319 The arbitrary precision integer routines define a structure called a ZVALUE.
320 This is defined in zmath.h. A ZVALUE contains a pointer to an array of
321 integers, the length of the array, and a sign flag. The array is allocated
322 using malloc, so you need to free this array when you are done with a
323 ZVALUE. To do this, you should call zfree with the ZVALUE as an argument
324 (or call freeh with the pointer as an argument) and never try to free the
325 array yourself using free. The reason for this is that sometimes the pointer
326 points to one of two statically allocated arrays which should NOT be freed.
328 The ZVALUE structures are passed to routines by value, and are returned
329 through pointers. For example, to multiply two small integers together,
330 you could do the following:
338 Use zcopy to copy one ZVALUE to another. There is no sharing of arrays
339 between different ZVALUEs even if they have the same value, so you MUST
340 use this routine. Simply assigning one value into another will cause
341 problems when one of the copies is freed. However, the special ZVALUE
342 values _zero_ and _one_ CAN be assigned to variables directly, since their
343 values of 0 and 1 are so common that special checks are made for them.
345 For initial values besides 0 or 1, you need to call itoz to convert a long
346 value into a ZVALUE, as shown in the above example. Or alternatively,
347 for larger numbers you can use the atoz routine to convert a string which
348 represents a number into a ZVALUE. The string can be in decimal, octal,
349 hex, or binary according to the leading digits.
351 Always make sure you free a ZVALUE when you are done with it or when you
352 are about to overwrite an old ZVALUE with another value by passing its
353 address to a routine as a destination value, otherwise memory will be
354 lost. The following shows an example of the correct way to free memory
355 over a long sequence of operations.
360 atoz("12345678987654321", &z2);
372 There are some quick checks you can make on integers. For example, whether
373 or not they are zero, negative, even, and so on. These are all macros
374 defined in zmath.h, and should be used instead of checking the parts of the
375 ZVALUE yourself. Examples of such checks are:
377 ziseven(z) (number is even)
378 zisodd(z) (number is odd)
379 ziszero(z) (number is zero)
380 zisneg(z) (number is negative)
381 zispos(z) (number is positive)
382 zisunit(z) (number is 1 or -1)
383 zisone(z) (number is 1)
384 zisnegone(z) (number is -1)
385 zistwo(z) (number is 2)
386 zisabstwo(z) (number is 2 or -2)
387 zisabsleone(z) (number is -1, 0 or 1)
388 zislezero(z) (number is <= 0)
389 zisleone(z) (number is <= 1)
390 zge16b(z) (number is >= 2^16)
391 zge24b(z) (number is >= 2^24)
392 zge31b(z) (number is >= 2^31)
393 zge32b(z) (number is >= 2^32)
394 zge64b(z) (number is >= 2^64)
396 Typically the largest unsigned long is typedefed to FULL. The following
397 macros are useful in dealing with this data type:
399 MAXFULL (largest positive FULL value)
400 MAXUFULL (largest unsigned FULL value)
401 zgtmaxfull(z) (number is > MAXFULL)
402 zgtmaxufull(z) (number is > MAXUFULL)
403 zgtmaxlong(z) (number is > MAXLONG, largest long value)
404 zgtmaxulong(z) (number is > MAXULONG, largest unsigned long value)
406 If zgtmaxufull(z) is false, then one may quickly convert the absolute
407 value of number into a full with the macro:
409 ztofull(z) (convert abs(number) to FULL)
410 ztoulong(z) (convert abs(number) to an unsigned long)
411 ztolong(z) (convert abs(number) to a long)
413 If the value is too large for ztofull(), ztoulong() or ztolong(), only
414 the low order bits converted.
416 There are two types of comparisons you can make on ZVALUEs. This is whether
417 or not they are equal, or the ordering on size of the numbers. The zcmp
418 function tests whether two ZVALUEs are equal, returning TRUE if they differ.
419 The zrel function tests the relative sizes of two ZVALUEs, returning -1 if
420 the first one is smaller, 0 if they are the same, and 1 if the first one
427 The arbitrary precision fractional routines define a structure called NUMBER.
428 This is defined in qmath.h. A NUMBER contains two ZVALUEs for the numerator
429 and denominator of a fraction, and a count of the number of uses there are
430 for this NUMBER. The numerator and denominator are always in lowest terms,
431 and the sign of the number is contained in the numerator. The denominator
432 is always positive. If the NUMBER is an integer, the denominator has the
435 Unlike ZVALUEs, NUMBERs are passed using pointers, and pointers to them are
436 returned by functions. So the basic type for using fractions is not really
437 (NUMBER), but is (NUMBER *). NUMBERs are allocated using the qalloc routine.
438 This returns a pointer to a number which has the value 1. Because of the
439 special property of a ZVALUE of 1, the numerator and denominator of this
440 returned value can simply be overwritten with new ZVALUEs without needing
441 to free them first. The following illustrates this:
448 A better way to create NUMBERs with particular values is to use the itoq,
449 iitoq, or atoq functions. Using itoq makes a long value into a NUMBER,
450 using iitoq makes a pair of longs into the numerator and denominator of a
451 NUMBER (reducing them first if needed), and atoq converts a string representing
452 a number into the corresponding NUMBER. The atoq function accepts input in
453 integral, fractional, real, or exponential formats. Examples of allocating
456 NUMBER *q1, *q2, *q3;
462 Also unlike ZVALUEs, NUMBERs are quickly copied. This is because they contain
463 a link count, which is the number of pointers there are to the NUMBER. The
464 qlink macro is used to copy a pointer to a NUMBER, and simply increments
465 the link count and returns the same pointer. Since it is a macro, the
466 argument should not be a function call, but a real pointer variable. The
467 qcopy routine will actually make a new copy of a NUMBER, with a new link
468 count of 1. This is not usually needed.
470 NUMBERs are deleted using the qfree routine. This decrements the link count
471 in the NUMBER, and if it reaches zero, then it will deallocate both of
472 the ZVALUEs contained within the NUMBER, and then puts the NUMBER structure
473 onto a free list for quick reuse. The following is an example of allocating
474 NUMBERs, copying them, adding them, and finally deleting them again.
476 NUMBER *q1, *q2, *q3;
485 Because of the passing of pointers and the ability to copy numbers easily,
486 you might wish to use the rational number routines even for integral
487 calculations. They might be slightly slower than the raw integral routines,
488 but are more convenient to program with.
490 The prototypes for the fractional routines are defined in qmath.h.
491 Many of the definitions for integer functions parallel the ones defined
492 in zmath.h. But there are also functions used only for fractions.
493 Examples of these are qnum to return the numerator, qden to return the
494 denominator, qint to return the integer part of, qfrac to return the
495 fractional part of, and qinv to invert a fraction.
497 There are some transcendental functions in the link library, such as sin
498 and cos. These cannot be evaluated exactly as fractions. Therefore,
499 they accept another argument which tells how accurate you want the result.
500 This is an "epsilon" value, and the returned value will be within that
501 quantity of the correct value. This is usually an absolute difference,
502 but for some functions (such as exp), this is a relative difference.
503 For example, to calculate sin(0.5) to 100 decimal places, you could do:
505 NUMBER *q, *ans, *epsilon;
508 epsilon = atoq("1e-100");
509 ans = qsin(q, epsilon);
511 There are many convenience macros similar to the ones for ZVALUEs which can
512 give quick information about NUMBERs. In addition, there are some new ones
513 applicable to fractions. These are all defined in qmath.h. Some of these
516 qiszero(q) (number is zero)
517 qisneg(q) (number is negative)
518 qispos(q) (number is positive)
519 qisint(q) (number is an integer)
520 qisfrac(q) (number is fractional)
521 qisunit(q) (number is 1 or -1)
522 qisone(q) (number is 1)
523 qisnegone(q) (number is -1)
524 qistwo(q) (number is 2)
525 qiseven(q) (number is an even integer)
526 qisodd(q) (number is an odd integer)
527 qistwopower(q) (number is a power of 2 >= 1)
529 The comparisons for NUMBERs are similar to the ones for ZVALUEs. You use the
530 qcmp and qrel functions.
532 There are four predefined values for fractions. You should qlink them when
533 you want to use them. These are _qzero_, _qone_, _qnegone_, and _qonehalf_.
534 These have the values 0, 1, -1, and 1/2. An example of using them is:
538 q1 = qlink(&_qonehalf_);
541 ---------------------
542 USING COMPLEX NUMBERS
543 ---------------------
545 The arbitrary precision complex arithmetic routines define a structure
546 called COMPLEX. This is defined in cmath.h. This contains two NUMBERs
547 for the real and imaginary parts of a complex number, and a count of the
548 number of links there are to this COMPLEX number.
550 The complex number routines work similarly to the fractional routines.
551 You can allocate a COMPLEX structure using comalloc (NOT calloc!).
552 You can construct a COMPLEX number with desired real and imaginary
553 fractional parts using qqtoc. You can copy COMPLEX values using clink
554 which increments the link count. And you free a COMPLEX value using cfree.
555 The following example illustrates this:
558 COMPLEX *c1, *c2, *c3;
571 As a shortcut, when you want to manipulate a COMPLEX value by a real value,
572 you can use the caddq, csubq, cmulq, and cdivq routines. These accept one
573 COMPLEX value and one NUMBER value, and produce a COMPLEX value.
575 There is no direct routine to convert a string value into a COMPLEX value.
576 But you can do this yourself by converting two strings into two NUMBERS,
577 and then using the qqtoc routine.
579 COMPLEX values are always returned from these routines. To split out the
580 real and imaginary parts into normal NUMBERs, you can simply qlink the
581 two components, as shown in the following example:
590 There are many macros for checking quick things about complex numbers,
591 similar to the ZVALUE and NUMBER macros. In addition, there are some
592 only used for complex numbers. Examples of macros are:
594 cisreal(c) (number is real)
595 cisimag(c) (number is pure imaginary)
596 ciszero(c) (number is zero)
597 cisnegone(c) (number is -1)
598 cisone(c) (number is 1)
599 cisrunit(c) (number is 1 or -1)
600 cisiunit(c) (number is i or -i)
601 cisunit(c) (number is 1, -1, i, or -i)
602 cistwo(c) (number is 2)
603 cisint(c) (number is has integer real and imaginary parts)
604 ciseven(c) (number is has even real and imaginary parts)
605 cisodd(c) (number is has odd real and imaginary parts)
607 There is only one comparison you can make for COMPLEX values, and that is
608 for equality. The ccmp function returns TRUE if two complex numbers differ.
610 There are three predefined values for complex numbers. You should clink
611 them when you want to use them. They are _czero_, _cone_, and _conei_.
612 These have the values 0, 1, and i.
618 If you wish, when you are all doen you can call libcalc_call_me_last()
619 to free a small amount of storage associated with the libcalc_call_me_first()
620 call. This is not required, but is does bring things to a closure.
622 The function libcalc_call_me_last() takes no args and returns void. You
623 need call libcalc_call_me_last() only once.
625 ## Copyright (C) 1999 David I. Bell and Landon Curt Noll
627 ## Calc is open software; you can redistribute it and/or modify it under
628 ## the terms of the version 2.1 of the GNU Lesser General Public License
629 ## as published by the Free Software Foundation.
631 ## Calc is distributed in the hope that it will be useful, but WITHOUT
632 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
633 ## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
634 ## Public License for more details.
636 ## A copy of version 2.1 of the GNU Lesser General Public License is
637 ## distributed with calc under the filename COPYING-LGPL. You should have
638 ## received a copy with calc; if not, write to Free Software Foundation, Inc.
639 ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
641 ## @(#) $Revision: 30.1 $
642 ## @(#) $Id: LIBRARY,v 30.1 2007/03/16 11:09:46 chongo Exp $
643 ## @(#) $Source: /usr/local/src/bin/calc/RCS/LIBRARY,v $
645 ## Under source code control: 1993/07/30 19:44:49
646 ## File existed as early as: 1993
648 ## chongo <was here> /\oo/\ http://www.isthe.com/chongo/
649 ## Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/