fp is an ISO/IEC 9899:1999 (C99) compliant software floating point library. It supports up to 14 decimal digits of precision, utilizing an internal format based on packed BCD. I chose this format to mimic the floating point representation used on the TI-8x line of calculators. This format takes up slightly more (2 bytes) space than an IEEE 754 binary64 type, but it has several advantages:
- It's easy to read. Since the data is in BCD, you can examine and manipulate the hexadecimal representation easily.
- It accurately stores common values such as 0.1. This is not possible in base-2 floating point representations.
- tools/fpp: Preprocessor used to turn
%FP(...)
or%FP_CONST(...)
into an internal representation. - tools/fp-calc: Simple REPL used to provide interactive testing of library.
- Basic arithmetic (+, -, *, /) and comparison
- Polynomial evaluation using the Horner scheme
- Conversion operations to/from various data types
- Arithmetic-geometric mean
- Exponential functions (exp, pow, log, sqrt) (WIP)
- Basic trigonometric functions (sin, cos, tan)
The only programs required to build this program are gcc, bison and flex. To build and install, all you have to do is
$ make && sudo make install
The API is designed for ease of use over efficiency. You can find examples available in the ex
directory. If %FP(...)
or %FP_CONST(...)
is used in your source code for floating-point constants, you need to run your code through the fpp
preprocessor before compiling. A sample invocation is given below:
$ fpp mycode.c | gcc -xc - -o mycode