4 Arbitrary precision integer and rational arithmetic library.
6 IMath is an open-source ANSI C arbitrary precision integer and rational
9 IMath is copyright © 2002-2009 Michael J. Fromberger.
11 > Permission is hereby granted, free of charge, to any person obtaining a copy
12 > of this software and associated documentation files (the "Software"), to deal
13 > in the Software without restriction, including without limitation the rights
14 > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 > copies of the Software, and to permit persons to whom the Software is
16 > furnished to do so, subject to the following conditions:
18 > The above copyright notice and this permission notice shall be included in
19 > all copies or substantial portions of the Software.
21 > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 IMath is a library written in portable ANSI C that allows you to perform
34 arithmetic on integers and rational numbers of arbitrary precision. While many
35 programming languages, including Java, Perl, and Python provide arbitrary
36 precision numbers as a standard library or language feature, C does not.
38 IMath was designed to be small, self-contained, easy to understand and use, and
39 as portable as possible across various platforms. The API is simple, and the
40 code should be comparatively easy to modify or extend. Simplicity and
41 portability are useful goals for some applications—however, IMath does
42 not attempt to break performance records. If you need the fastest possible
43 implementation, you might consider some other libraries, such as GNU MP (GMP),
44 MIRACL, or the bignum library from OpenSSL.
46 Programming with IMath
47 ----------------------
49 Detailed descriptions of the IMath API can be found in [doc.md](doc.md).
50 However, the following is a brief synopsis of how to get started with some
53 To do basic integer arithmetic, you must declare variables of type `mpz_t` in
54 your program, and call the functions defined in `imath.h` to operate on them.
55 Here is a simple example that reads one base-10 integer from the command line,
56 multiplies it by another (fixed) value, and prints the result to the standard
57 output in base-10 notation:
63 int main(int argc, char *argv[])
70 fprintf(stderr, "Usage: testprogram <integer>\n");
74 /* Initialize a new zero-valued mpz_t structure */
77 /* Initialize a new mpz_t with a small integer value */
78 mp_int_init_value(&b, 25101);
80 /* Read a string value in the specified radix */
81 mp_int_read_string(&a, 10, argv[1]);
83 /* Multiply the two together... */
84 mp_int_mul(&a, &b, &a);
86 /* Print out the result */
87 len = mp_int_string_len(&a, 10);
88 buf = calloc(len, sizeof(*buf));
89 mp_int_to_string(&a, 10, buf, len);
90 printf("result = %s\n", buf);
93 /* Release memory occupied by mpz_t structures when finished */
100 This simple example program does not do any error checking, but all the IMath
101 API functions return an `mp_result` value which can be used to detect various
102 problems like range errors, running out of memory, and undefined results.
104 The IMath API also supports operations on arbitrary precision rational numbers.
105 The functions for creating and manipulating rational values (type `mpq_t`) are
106 defined in `imrat.h`, so that you need only include them in your project if you