introducing
float-abel is a Rust library I've released for reproducible, retractable, floating point addition. It makes 0.1 + 0.2 - 0.2 == 0.1 instead of 0.10000000000000003. It comes with adapters for differential-dataflow and dbsp, and is designed so that people can build other adapters independently. It's MIT or Apache licensed, and developed with Claude. I am using it in a number of projects I'm developing.
computers are bad at math, actually
Floating point numbers are strange beasts. You would think a + b - b = a. It should. That would make sense. So it doesn't. You're left with some weird little error value that changes based on the numbers you set for a and b.
They're trying to represent every possible real number in a constant number of bits, which is impossible. That leads to gaps in resolution. There are numbers they just can't hold, and so those get shifted slightly to ones that fit. Usually this doesn't matter a lot, even though at large scales it gets kind of silly: a float that holds 1e100 can't tell the difference between 0.0 and 1.0. Seriously. 1e100 + 1.0 - 1e100 == 0.0 according to f64. That's how low the resolution is, to cover a wider span of numbers.
It becomes more of a problem when you add three or more of these numbers together. Because the "float" in "floating point" is that the resolution changes with every value, sliding the "point", always trying to strike the best balance between size before the "point" and precision after it. For every value those can be different, and that means the resolution gaps don't line up. You have to align them to a common resolution, and it's not always possible. This introduces more error.
Worse, the alignment can be different for each pair of numbers. If you have 3 numbers and add the first to the second, they're going to have to align one way. If you add the second to the third, they'll align a different way. So now addition and subtraction, which are supposed to work in any order, change based on which way you combine the numbers.
the kind of math you need for distributed systems
This still may not seem like it matters a lot. However, a lot of the ways we distribute work across multiple workers fundamentally come down to breaking the work up into random chunks, sending chunks to each worker, and then adding them all together at the end. With floats, the results aren't going to perfectly match across runs, especially if your worker count changes.
Again, maybe that doesn't seem like a huge deal. So it's off by a little bit. Should it matter to me? Well, yeah, if you care about processing streaming data efficiently.
One of the greatest advancements of the past 15 years has been in incremental view maintenance. Frameworks like differential-dataflow and dbsp let you very efficiently manage standing views on top of changing data. They let you query data in a way where it stays live and up to date without re-running the entire query every time new events stream in. Instead, the query only costs what it takes to run over what changed. It's like magic.
Until you want to work with floats. Specifically, floats that don't have a consistent shape. If you know all your numbers are super small and need a specific level of precision, you can just use a fixed-point number. Same if you want to work with currency, you can use decimal. Incremental views work fine with non-floating numbers with decimal points.
There are times that doesn't work, though, and you can't predict the size or precision you need in advance. Say, personalized PageRank, or Laplacians. You're gonna want real floats.
The problem is that incremental engines need the values they track to have some special properties which rule out floats. They have to add up and subtract the same no matter the grouping or sequence, forming what math nerds call an "abelian group". And after subtraction you have to be able to tell the difference between zero and one, because this kind of system drops keys when their values are at zero, and stops displaying them.
float-abel: a Retina display for f64
And that's why float-abel exists. It just...uses a higher resolution, so this stops being such a big problem. It looks at floats in terms of their "pixels". The smallest positive number an f64 can store is 2⁻¹⁰⁷⁴, so you can look at any number they represent as an integer multiple of it. Then it's just binary addition, consistent and exact. It's kinda like a HiDPI display for aligning sampled reals, with a pixel grid so small you can't see the dots. Its numbers always add up the same way, and its zero is exact, so its values can be used in incremental engines.
An f64 can handle 16 significant digits. float-abel can handle up to 38 digits while staying within 256 bits on the stack. Or 655 digits within 300 bytes, enough to hold every sum of f64s. And if that's not enough, it can move to the heap and grow to over 10k significant digits, then slim back down when it isn't needed, letting it multiply up to 16 floats deep. It's simd optimized for ARM and x86, and once you're summing a stream of 100k-200k floats, it's faster to incrementally add each value that streams in with this crate than to do the full recompute on f64s.
Check it out: https://github.com/jbrjake/float-abel