Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Mondo Math Libs

By Eric Olson
2003-08-22


Some contenders

So, you find that you need to use truly huge integers or floating point values in a Linux program. Where do you turn? In this section I'll give you a brief overview of some of the more interesting and useful MPM library packages available on the Net. By no means an exhaustive list, or even a "best of the Net" list, this is just a good starting place.

If you find any math libraries for Linux that are worthy of mention, please e-mail me at the address at the start and end of this column, because this is one topic that I will likely return to in future installments.

apfloat
This package includes support for arbitrary precision numbers in four forms: integers, floating point numbers, rational numbers, and complex numbers. The apfloat source code includes two archives: a common, base portion plus one tailored to various C++ compilers (including a generic ANSI C++ version), as well as versions optimized for 386/486 or Pentium systems. While the package's author says apfloat should be usable with nearly every C++ compiler, it includes a makefile for gcc, which makes building the library under Linux very easy.

The supported math functions include the basic four arithmetic operations plus inverse root, trigonometric functions (normal, arc, hyperbolic, and inverse hyperbolic), power, exponentiation, a function to calculate pi to a desired precision, and more. There is also a stream output operator.

The package includes full source code plus several demonstration and test programs.

apfloat

  • Main sites: www.jjj.de/mtommila/apfloat/
  • Download page: http://www.jjj.de/mtommila/apfloat/1.51/
  • Maximum precision: Limited only by memory (including disk space)
  • Language: C++
  • Licensing: Freeware, free for non-commercial use
  • Documentation: The apfloat manual is a 35-page PostScript file available from the downloads page. It provides a good introduction to the library, as well as several appendices on number theory and algorithms.

gmp
gmp stands for GNU Multiple Precision, and is, of course, the GNU entry in the field. It supports arbitrary precision operations on signed integers, rational numbers, and floating point numbers, and is, of course, quite at home with Linux.

One interesting facet of gmp is the degree to which it has been tailored to various hardware platforms. The manual points out that the package contains "carefully optimized assembly code for these CPUs: DEC Alpha, Amd 29000, HPPA 1.0 and 1.1, Intel Pentium and generic x86, Intel i960, Motorola MC68000, MC68020, MC88100, and MC88110, Motorola/IBM PowerPC, National NS32000, IBM POWER, MIPS R3000, R4000, SPARCv7, SuperSPARC, generic SPARCv8, and DEC VAX. Some optimizations also for ARM, Clipper, IBM ROMP (RT), and Pyramid AP/XP."

Not surprisingly, the manual also points out that gmp places "a general emphasis on speed (as opposed to simplicity or elegance)." This is a fair statement; gmp is very fast, but because it is written in C, it provides a lower level interface than the other MPM libraries mentioned here. In particular, the lack of operator overloading places an additional burden on the programmer and anyone modifying his or her code in the future. The lack of destructors is more problematic, in that it opens the door for the all-too-familiar memory leak problems, because the gmp data types use dynamically allocated memory, which must be freed via explicit calls to _clear functions.

The gmp package is also pretty sparse in terms of provided math functions. It supports numerous variations of the basic functions, plus square roots, I/O, and random numbers, but little else.

gmp

  • Main and download page: http://www.swox.com
  • Maximum precision: Limited by memory
  • Language: C (the main Web page mentions plans to design a C++ wrapper, but there doesn't seem to be any progress on that front yet)
  • Licensing: GPL (GNU public license)
  • Documentation: The manual is online in HTML format at http://www.swox.com/gmp/manual/gmp_toc.html, or you can download it in DVI or PostScript format.

If you experiment with gmp, be sure to download copies of the patches from the main site, because they fix some subtle bugs in the support for rational and floating point numbers.

hfloat
hfloat is a floating-point-only library that was developed under Linux using gcc 2.7.x and also includes the author's fxt-library, a collection of several FFT implementations. hfloat allows you to choose any number from 2 to 65536 for the radix used in number representations, giving it some notable flexibility. The supported operations include square root, n-th power, some trigonometric functions (not as many as apfloat), and logarithms.

hfloat has a few quirks to watch out for, including initialization syntax (described in "Hints and tips" below), and the way it represents a number's radix and precision. For example, for a base-10 number you use a radix of 10,000, not 10, and you measure precision in LIMBs, which are 16-bit unsigned integers. These aren't major problems, particularly for programmers working with this sort of package, but I recommend you pay attention to the author's descriptions and examples of these items in the manual.

The source code for hfloat is also somewhat unusual in that the author has broken it into several compilation units (the base class, one for the "guts", one for functions, etc.). This will be visible to you only if you intend to modify the package, but it adds a bit to the learning curve as you figure out where everything is.

hfloat

  • Main and download site: http://www.jjj.de/hfloat/hfloatpage.html
  • Maximum precision: Limited only by memory
  • Language: C++
  • Licensing: GPL (GNU Public License)
  • Documentation: The 15-page manual is available as a separate download, in DVI, PostScript, or Acrobat/PDF format. The manual is a bit terse, but it does cover basic usage of the package and its general layout.


Tutorial Pages:
» A look at some of the math libraries for Linux
» Ground rules
» Binary vs. decimal
» Some contenders
» Java's BigInteger and BigDecimal
» Hints and tips
» Resources


First published by

Related Tutorials:
» How to Install PHP 5 on Linux
» How to Install Apache 2 on Linux
» How to Install MySQL 5.0 on Linux
» SMB Caching
» Mound --Bind
» Tar Wild Card Interpretation

Ask A Question
characters left.