4 ms·
`equal(0.3, 0.2 + 0.1); // true` how is this wizardry possible?
by ipsum2 2y ago
`equal(0.3, 0.2 + 0.1); // true`
how is this wizardry possible?
- defrost 2y agoIs What Every Computer Scientist Should Know About Floating-Point Arithmetic wrong ??!! addendum: why are obviously rhetorical questions are taken so literally here?
- leansensei 2y agoBecause text doesn't convey sarcastic voice tonality, so the intent is far from obvious.
- defrost 2y agoSarcastic? Okay, if you say so. Picking out an obvious define function that compares a float with a float sum of that nature should indicate an good understanding of why that might be called wizardry and deserving of a second look. Hats off to the peer comment that suggested scaling against epsilon rather than simpliy regurging the substitution "as was" from the header. The scaling is better in general, optional in some specific contexts.
- ipsum2 2y agoIt's meant as both humorous and a nerd snipe :)
- wjbr 2y agostatic int pretty_float_equal (float a, float b) { return fabsf(a - b) < FLT_EPSILON; }
- slimsag 2y agoit uses absolute difference epsilon equality ('close enough to be considered equal'): static int pretty_float_equal (float a, float b) { return fabsf(a - b) < FLT_EPSILON; } static int pretty_double_equal (double a, double b) { return fabs(a - b) < DBL_EPSILON; } static int pretty_long_double_equal (long double a, long double b) { return fabsl(a - b) < LDBL_EPSILON; }
- vbezhenar 2y agoThis is wrong code. It only works somewhat correctly when a and b around 1.
- masklinn 2y agoIt uses type dispatch to perform an epsilon comparison: static int pretty_float_equal (float a, float b) { return fabsf(a - b) < FLT_EPSILON; } So it’s https://docs.python.org/library/math.html#math.isclose https://docs.python.org/library/math.html#math.isclose
- hmry 2y agoThis code is incorrect, but I don't blame them. :) Probably one of the most common float-related mistakes, even among people who "know how floats work". FLT_EPSILON is the difference between 1.0 and the next larger float. It's impossible for numbers less than -2.0 or greater than 2.0 to have a difference of FLT_EPSILON, they're spaced too far apart. You really want the acceptable error margin to be relative to the size of the two numbers you're comparing. Also, everyone should read the paper "What, if anything, is epsilon?" by Tom7
- im3w1l 2y agoI would go even further and say that any equality comparison of float numbers has to be a one-off special case. You need to know how much error can arise in your calculations, and you need to know how far apart legitimately different numbers will for your particular data. And of course the former has to be smaller than the latter.
- aartaka 2y agoIndeed, FLT_EPSILON is not a one-size-fits-all solution, but it's good enough for frequent case of comparing big enough numbers, which is not covered by regular ==. So it's a convenience/correctness trade-off I'm ready to make.
- hmry 2y agoIf the numbers you are comparing are greater than 2, abs(a - b) < FLT_EPSILON is equivalent to a == b. Because it's not possible for two large numbers to not be equal, but also closer together than FLT_EPSILON.