@b0rk a) cool deep dive & explainer
b) I wish there was at least a passing mention that it doesn't need to be that way, or that it isn't that way in all languages.
@b0rk FWIW I'd note:
1/10 in binary is a non-terminating:
0x1.999999999999999.....* 2^-4
so it must be rounded to fit in a finite number of digits. (2/10 is just twice that). Add these two rounded things together and you get something different than if you "symbolically" worked it out on paper. This isn't a "binary" problem it's a finite digits one. If working with a decimal floating point you'd hit the same "problem"...say with 1/3 + 2/3.
@b0rk Nice. Too many summaries of floating point cover the mechanical structure, but not operations on them. (We'll "skip the witches" of the Quake algorithm for now…)
It's not tensegrity (I may watch too many civil engineering channels) but this structural summary and overview/explanation of limitations came up recently in my recommended.
https://www.youtube.com/watch?v=Oo89kOv9pVk
Financial values?
Don't. Just… just don't.
This means JSON is fundamentally not suited to financials.
Example:
from a unit test:
// dang double precision
EXPECT_EQ(0.30000000000000004), Interpolate(range, 0.75));
@b0rk Just had to write a nice unit test for basic math. Turns out it's more broken than I expected.
@b0rk this reminds me of rounding numbers in JavaScript. Needing to Math.Round(3.5) I’d expect it to be 4, but you have to multiply by 100 and divide by 100. On paper it’s the same, but float numbers are fun. This actually helps explain this kid of situation now!
Math.round(total * 100) / 100
@b0rk wonderful post! I always struggle to explain that. Now I can point learners to your post.
Would it make sense for you to add a small mention of Python's Decimal class in the standard library as a possible workaround in certain situations?
@b0rk I very recently saw an online homework marking system that marked an exactly correct answer "0.000" wrong. It was expecting 4.523e-16 O.o
This lead me to find this interesting paper:
Toronto and McCarthy, ‘Practically Accurate Floating-Point Math’. "With the right tools, floating-point code can be debugged like any other code, drastically improving its
accuracy and reliability."
Investgates the "badlands" of some FP functions.
Downloadable here e.g. https://jeapostrophe.github.io/home/static/tm-cise2014.pdf