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