• Stars
    star
    124
  • Rank 282,104 (Top 6 %)
  • Language
    C
  • License
    Other
  • Created almost 12 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Arbitrary precision integer and rational arithmetic library

IMath

Arbitrary precision integer and rational arithmetic library.

Unit tests

IMath is an open-source ISO C arbitrary precision integer and rational arithmetic library.

IMath is copyright © 2002-2009 Michael J. Fromberger.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About IMath

IMath is a library written in portable ISO C that allows you to perform arithmetic on integers and rational numbers of arbitrary precision. While many programming languages, including Java, Perl, and Python provide arbitrary precision numbers as a standard library or language feature, C does not.

IMath was designed to be small, self-contained, easy to understand and use, and as portable as possible across various platforms. The API is simple, and the code should be comparatively easy to modify or extend. Simplicity and portability are useful goals for some applications—however, IMath does not attempt to break performance records. If you need the fastest possible implementation, you might consider some other libraries, such as GNU MP (GMP), MIRACL, or the bignum library from OpenSSL.

Programming with IMath

Detailed descriptions of the IMath API can be found in doc.md. However, the following is a brief synopsis of how to get started with some simple tasks.

To do basic integer arithmetic, you must declare variables of type mpz_t in your program, and call the functions defined in imath.h to operate on them. Here is a simple example that reads one base-10 integer from the command line, multiplies it by another (fixed) value, and prints the result to the standard output in base-10 notation:

#include <stdio.h>
#include <stdlib.h>
#include "imath.h"

int main(int argc, char *argv[])
{
  mpz_t  a, b;
  char  *buf;
  int    len;

  if(argc < 2) {
    fprintf(stderr, "Usage: testprogram <integer>\n");
    return 1;
  }

  /* Initialize a new zero-valued mpz_t structure */
  mp_int_init(&a);

  /* Initialize a new mpz_t with a small integer value */
  mp_int_init_value(&b, 25101);

  /* Read a string value in the specified radix */
  mp_int_read_string(&a, 10, argv[1]);

  /* Multiply the two together... */
  mp_int_mul(&a, &b, &a);

  /* Print out the result */
  len = mp_int_string_len(&a, 10);
  buf = calloc(len, sizeof(*buf));
  mp_int_to_string(&a, 10, buf, len);
  printf("result = %s\n", buf);
  free(buf);

  /* Release memory occupied by mpz_t structures when finished */
  mp_int_clear(&b);
  mp_int_clear(&a);

  return 0;
}

This simple example program does not do any error checking, but all the IMath API functions return an mp_result value which can be used to detect various problems like range errors, running out of memory, and undefined results.

The IMath API also supports operations on arbitrary precision rational numbers. The functions for creating and manipulating rational values (type mpq_t) are defined in imrat.h, so that you need only include them in your project if you wish to.

More Repositories

1

jrpc2

A Go implementation of a JSON-RPC 2.0 server and client.
Go
62
star
2

twitter

A Go client for the Twitter v2 API (in development).
Go
30
star
3

taskgroup

A Go package for managing a group of collaborating goroutines.
Go
20
star
4

otp

A Go implementation of the HOTP (RFC 4226) and TOTP (RFC 6238) algorithms.
Go
15
star
5

ffs

Flexible filesystem (experimental).
Go
9
star
6

mds

Generic data structures in Go.
Go
6
star
7

tomledit

Edit the structure of TOML documents
Go
6
star
8

misctools

Various and sundry miscellaneous command-line tools. Unsupported and subject to arbitrary changes without warning.
Go
6
star
9

cityhash

A straightforward transliteration of the CityHash non-cryptographic hash algorithm from C++ into Go. Based on https://github.com/google/cityhash
Go
6
star
10

wschannel

An implementation of the jrpc2 Channel over websocket.
Go
5
star
11

washcookies

A tool to clean up MacOS browser cookies based on a user-defined file of rules
Python
5
star
12

kythebox

Configurations for a Kythe developer installation
Shell
4
star
13

sqlitestore

A blob.Store implementation on SQLite3.
Go
4
star
14

msync

Synchronization management types in Go
Go
4
star
15

command

A lightweight subcommand-handling library
Go
4
star
16

snapback

A tarsnap backup script written in Go.
Go
3
star
17

bplist

A library to read and write binary property list files.
Go
3
star
18

twig

A basic command-line Twitter client.
Go
3
star
19

chirp

A lightweight binary RPC protocol.
Go
3
star
20

ctrl

Control-flow management for Go main programs.
Go
3
star
21

miser

A version of the "Miser" text adventure game for Chipmunk BASIC
BASIC
2
star
22

atomicfile

All-or-nothing file replacement using atomic renames.
Go
2
star
23

jtree

Experimental JSON stream parser.
Go
2
star
24

gopkg

A library to query the godoc.org JSON API.
Go
2
star
25

recipes

Family recipes and other food-related documentation.
Markdown
2
star
26

ffuse

A filesystem driver for FUSE using ffs.
Go
1
star
27

scrubby

A tag-soup parser for HTML-like markup languages.
Python
1
star
28

ffstools

Command-line tools for the FFS module.
Go
1
star
29

lice

A command-line tool for generating license files.
Go
1
star
30

s3store

A blob.Store implementation on S3
Go
1
star
31

gcsstore

A blob.Store implementation using GCS.
Go
1
star
32

notifier

A local utility service
Go
1
star
33

wirepb-experiment

Experimental wire-format protobuf canonicalizer
Go
1
star
34

cookies

A library to read and write browser cookies.
Go
1
star
35

tt2rom

Command-line tool to convert ASM charts into Intel ROM files
C
1
star
36

cache

Packages that implement string-keyed caching of byte buffers.
Go
1
star
37

curled

A wrapper for libcurl based on ctypes.
Python
1
star
38

hypercard

Tools for manipulating HyperCard stacks
Go
1
star
39

bitstream

A Go package to support reading and writing of variable-width binary values to or from a stream of bytes.
Go
1
star
40

staticfile

An obsolete static file generator for Go (use https://pkg.go.dev/embed instead)
Go
1
star
41

eyepopper

A local POP3 implementation that provides a read-only view of a collection of Unix mailbox files or Maildir directories.
Python
1
star
42

badgerstore

An implementation of the blob.Store interface over BadgerDB.
Go
1
star
43

wirepb

A low-level protocol buffer wire format scanner in Go.
Go
1
star