fp64lib is a library for implementing 64-bit floating point arithmetic on the Atmel MegaAVR architecure microprocessors, like the popular Arduino UNO, Nano or Mega2560 series. The library overcomes the problem, that for these processors, the data type double is identical to float, i.e. even if double is used, the calculations are all done in with single precision, which is 5-7 digits.
fp64lib provides 14-15 digits of precision, for most routines results are even precise to +/-1 ulp. Data format is fully compatible with IEEE 754 binary64 standard (see Wikipedia):
- Sign bit: 1 bit
- Exponent: 11 bits
- Significand precision: 53 bits (52 explicitly stored)
All functions of fp64lib support
- signed 0 (zero)
- NaN
- +Inf and -Inf
- subnormal numbers
To limit code size, not all features of IEEE 754 are implemented. The major differences are:
- There are no signaling NaNs, only quite NaNs.
- No exceptions are raised.
- Out of the five rounding modes, only Round to nearest, ties to even is implemented.
- Therefore, rounding modes cannot be controlled.
The library comes with a math.h compatible head file named “fp64-math.h”. All fp64lib routines start with “fp64_”, e.g. fp64_sin or fp64_add. Library is fully compatible to usual “math.h” routines, e.g.
- Basic arithmetic functions: fp64_add(), fp64_sub(), fp64_mul(), fp64_div(), fp64_fmod(), fp64_fma()
- Check and compare functions: fp64_isinf(), fp64_isnan(), fp64_isfinite(), fp64_compare(), fp64_signbit(), fp64_fmin(), fp64_fmax(), fp64_fdim()
- Basic functions: fp64_neg(), fp64_abs(), fp64_inverse(), fp64_sqrt(), fp64_square(), fp64_trunc(), fp64_ceil(), fp64_floor(), fp64_round(), fp64_lround(), fp64_lrint()
- Trigonometric functions and their inverse: fp64_sin(), fp64_cos(), fp64_tan(), fp64_asin(), fp64_acos(), fp64_atan(), fp64_hypot(),
fp64_atan2() - Conversion functions from and to float/double: fp64_sd(), fp64_ds()
- Conversion functions from and to integer: fp64_to_int64(), fp64_to_uint64, fp64_long_to_float64(), fp64_to_int32( ), fp64_to_uint32( ), fp64_to_int16( ), fp64_to_uint16(), fp64_to_int8() , fp64_to_uint8()
- Conversion functions from and to string: fp64_to_decimalExp(), fp64_strtod(), fp64_to_string()
- Logarithmic and hyperbel function: fp64_log(), fp64_exp(), fp64_log10(), fp64_sinh(), fp64_cosh(), fp64_tanh(), fp64_ldexp(), fp64_frexp(), fp64_pow(), fp64_cbrt()
Furthermore, the library is mostly compatible with the avr_f64.c library. To make conversion from your previous avr_f64.c project easy, a avr_fp64.h header file is supplied that converts calls to avr_f64 routine to calls to fp64lib routines. Beware however, that due to the extended support of IEE 754, that the behaviour of your program might differ slightly. But usally, you will only notice that your code runs 80% faster.
Source code of the library is available on GitHub, it can be downloaded here as an plug & play Arduino library. Library reference can be found in the documentation section.
ATTENTION
The library is inteded for and usable only on the processors based on Atmels MegaAVR architecture, like the 328P used in Arduino UNO or Arduino Nano (but not Arduino Nano ESP32) or the Arduino Mega2560. It does not run on boards based on the tinyAVR architecture, like the ATTiny. The tinyAVR architecture lacks some instructions, especially hardware multiplication, making it awkward, time- and memory consuming to implement 64-bit floating point algorithms.
Other Arduino boards, like Arduino Due or Arduino GIGA, are based on different microprocessor architectures, like ARM Cortex M3, M4 or M7. For these boards, fp64lib is not needed as 64bit arithmetic is implement for the double
datatype.
The library is clearly marked that it only supports AVR/MegaAVR boards. However, the Arduino package wrongly states that also other boards are supported. This is a known bug, see github. Until this is fixed, it’s better to consult the Wikipedia List of Arduino boards and compatible systems. Check the Processor column. If the processor starts with ATmega, then your board is most likely to be supported.
Disclaimer
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
DO NOT USE IT IN ANYTHING THAT IS SOMEHOW USED IN A HAZARDOUS ENVIRONMENT OR WHERE LIFE CAN BE ENDANGERED OR WHERE CRITICAL DECISIONS ARE BASED ON.