• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..12-Oct-2021-

contrib/H12-Oct-2021-8941

examples/H12-Oct-2021-2,2421,514

tests/H03-May-2022-19,16618,578

tools/H12-Oct-2021-305190

.dockerignoreH A D12-Oct-2021110 1212

.gitattributesH A D12-Oct-202135 21

.travis.ymlH A D12-Oct-2021245 1512

ChangeLogH A D12-Oct-202122.6 KiB564457

LICENSEH A D12-Oct-20211.1 KiB2117

MakefileH A D12-Oct-20214 KiB13268

README.mdH A D12-Oct-20214 KiB10884

doc.mdH A D12-Oct-202144.6 KiB1,199962

doc.md.inH A D12-Oct-202115.7 KiB395304

gmp_compat.cH A D12-Oct-202122.1 KiB824460

gmp_compat.hH A D12-Oct-20216.9 KiB23065

imath.cH A D12-Oct-202169.5 KiB2,7811,817

imath.hH A D12-Oct-202117.3 KiB422128

imdrover.cH A D12-Oct-202135.8 KiB1,4631,112

imdrover.hH A D12-Oct-20214 KiB11273

imrat.cH A D12-Oct-202123.6 KiB941647

imrat.hH A D12-Oct-202110.5 KiB27170

imtest.cH A D12-Oct-202114.6 KiB426278

imtimer.cH A D12-Oct-20215.6 KiB216149

iprime.cH A D12-Oct-20213.6 KiB9751

iprime.hH A D12-Oct-20211.7 KiB4912

rsamath.cH A D12-Oct-20214.7 KiB14970

rsamath.hH A D12-Oct-20213.6 KiB9522

README.md

1IMath
2=====
3
4Arbitrary precision integer and rational arithmetic library.
5
6IMath is an open-source ANSI C arbitrary precision integer and rational
7arithmetic library.
8
9IMath is copyright © 2002-2009 Michael J. Fromberger.
10
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:
17>
18> The above copyright notice and this permission notice shall be included in
19> all copies or substantial portions of the Software.
20>
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
27> SOFTWARE.
28
29
30About IMath
31-----------
32
33IMath is a library written in portable ANSI C that allows you to perform
34arithmetic on integers and rational numbers of arbitrary precision.  While many
35programming languages, including Java, Perl, and Python provide arbitrary
36precision numbers as a standard library or language feature, C does not.
37
38IMath was designed to be small, self-contained, easy to understand and use, and
39as portable as possible across various platforms.  The API is simple, and the
40code should be comparatively easy to modify or extend.  Simplicity and
41portability are useful goals for some applications—however, IMath does
42not attempt to break performance records.  If you need the fastest possible
43implementation, you might consider some other libraries, such as GNU MP (GMP),
44MIRACL, or the bignum library from OpenSSL.
45
46Programming with IMath
47----------------------
48
49Detailed descriptions of the IMath API can be found in [doc.md](doc.md).
50However, the following is a brief synopsis of how to get started with some
51simple tasks.
52
53To do basic integer arithmetic, you must declare variables of type `mpz_t` in
54your program, and call the functions defined in `imath.h` to operate on them.
55Here is a simple example that reads one base-10 integer from the command line,
56multiplies it by another (fixed) value, and prints the result to the standard
57output in base-10 notation:
58
59    #include <stdio.h>
60    #include <stdlib.h>
61    #include "imath.h"
62
63    int main(int argc, char *argv[])
64    {
65      mpz_t  a, b;
66      char  *buf;
67      int    len;
68
69      if(argc < 2) {
70        fprintf(stderr, "Usage: testprogram <integer>\n");
71        return 1;
72      }
73
74      /* Initialize a new zero-valued mpz_t structure */
75      mp_int_init(&a);
76
77      /* Initialize a new mpz_t with a small integer value */
78      mp_int_init_value(&b, 25101);
79
80      /* Read a string value in the specified radix */
81      mp_int_read_string(&a, 10, argv[1]);
82
83      /* Multiply the two together... */
84      mp_int_mul(&a, &b, &a);
85
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);
91      free(buf);
92
93      /* Release memory occupied by mpz_t structures when finished */
94      mp_int_clear(&b);
95      mp_int_clear(&a);
96
97      return 0;
98    }
99
100This simple example program does not do any error checking, but all the IMath
101API functions return an `mp_result` value which can be used to detect various
102problems like range errors, running out of memory, and undefined results.
103
104The IMath API also supports operations on arbitrary precision rational numbers.
105The functions for creating and manipulating rational values (type `mpq_t`) are
106defined in `imrat.h`, so that you need only include them in your project if you
107wish to.
108