This is a software 32-bit floating point implementation. While there are plenty soft floating point libraries in the wild, I am not satisfied with either of those. All of them are way too complex and pretty much undocumented.
Despite the focus on readability, there are very few comments in the code. An extensive writing is available here:
- Floating point myths and superstitions
- Computers and numbers
- Printing floats
- Summation and numerical errors
- Division and multiplication (work in progress)
This is a tiny project, the goal is to make a simple and understandable implementation of single-precision floating point. I do not care about efficiency as long as the code is simple. The goal is twofold:
- first, educate on how floating point works
- second, provdide the code that can be ported to platforms without floating point.
For example, I used it to create an eyecandy raytracer for my TinyCompiler project.
Recently, I needed to emulate floating point operations using only integer arithmetic, since floats were unavailable. I went online to look for a ready-made library and to my surprise, not only did I not find what I was looking for, but I also discovered that someone on the internet was wrong. :)
It turns out that forums are full of people who don't fully understand how computers manipulate numbers. For example, I pulled the above meme from Reddit (it was me who crossed it out). Someone was so scared of the terrible rounding errors of floating point numbers that he even made a funny picture. The only problem is that 0.5 + 0.5 is exactly equal to 1.0.
So, I decided to roll up my sleeves and reinvent the wheel. That is, to write the most unoptimized C++ library for emulating IEEE754 32-bit floating-point numbers using only 32-bit integer arithmetic.
There are two kinds of tests in the project: unit tests where TinyFloat functions are tested against C++ native float.
There is also a truncated version of PARANOIA,
a test suite written in Basic by William Kahan in 1983.
Paranoia is designed to discover obvious flaws in non-compliant floating point arithmetic and it is still used today!
git clone https://github.com/ssloy/tinyfloat.git &&
cd tinyfloat &&
cmake -Bbuild &&
cmake --build build -j &&
cd build &&
ctest . &&
./paranoia
