(I plan to make a casual blog post about this soon! This repo is a simple proof-of-concept, and should definitely not be used in production!)
DumbNum16 is an integer format for humans who don't care about accuracy. It uses only 16 bits to represent a number up to 10^34, but at a huge cost of accuracy and performance.
A DumbNum16 looks like this:
SEEEEEMM MMMMMMMM
- S: Sign bit (1 is negative)
- E: 5-bit exponent
- M: 10-bit mantissa (over 999 represents Infinity)
The integer represented is (-1)^S * M * 10^E.
When doing math with these numbers, obviously there isn't enough accuracy to get the right result, so randomness is used to determine overflow. For example, if the number stored is 234 * 10^3 and you add 1, there is only a 1/1000 chance that the mantissa will be bumped to 235. The idea is that, due to the law of large numbers, these kinds of little additions will be pretty accurate over time.
DumbNum16s have some nice properties:
- When 0 <= n <= 999, the DumbNum16 and normal integer representations are the same
- Positive DumbNum16s can be sorted by their bit representation (much like floats)
DumbNum16s are similar to Morris Counters, but are more human-readable (due to being base-10) and have better accuracy for small numbers. I doubt any of the ideas in this repo are actually new. Maybe you could use something like this for a video view count or something, where you don't actually care about the exact value.
let mut n = DumbNum::from(1_000_000);
for _ in 0..40_000 {
n += DumbNum::from(250);
}
println!("Result is {n}");- Properly handle -0 and +-Infinity in operations