3 ms·
Floating point numbers are the worst ways to work with non-integer numbers, unless you count all the other ways of working with non-integer numbers, which are w
by klodolph 3y ago
Floating point numbers are the worst ways to work with non-integer numbers, unless you count all the other ways of working with non-integer numbers, which are worse (ratios, fixed-point).
This is kind of a ha-ha cute response and not something you should try to dissect, but it’s worth thinking about the reasons why float beat out both ratios and fixed-point as the preferred way to represent numbers—those types both come with traps that are somewhat more severe than the traps you encounter with floats.
- kstenerud 3y agoQuite right. The main issue is that everyone thinks they know how floating point works, which is why they continue to fall into these traps. Like time, floating point is deceptively hard.
- maxbond 3y agoIs there anything I can read about the sharp edges of these alternate representations? Lots of ink is spilled about the perils of floats, but in the past when I've been tempted to write a fixed point or ratio implementation (for mathy personal projects where they were especially useful), I've had a hard time finding much written about them at all.
- lifthrasiir 3y agoYou can instead list your criteria for good number format and look at alternatives with those lenses. Floating point is designed for a good balance between dynamic range and precision, and IEEE 754 binary formats can be seen as a FP standard particularly optimized for numerical calculation. There are several other FP formats. The most popular one is IEEE 754 minus subnormal numbers, followed by bfloat16, IEEE 754 decimal formats (formerly IEEE 854) and posits. Only first two have good hardware supports. The lack of subnormal number means that `a <=> b` can't be no longer rewritten to `a - b <=> 0` among others but is widely believed to be faster. (I don't fully agree, but it's indeed true for existing contemporary hardwares.) IEEE 754 decimal formats are notable for lack of normalization guarantee. Posits are, in some sense, what IEEE 754 would have been if designed today, and in fact aren't that fundamentally different from IEEE 754 in my opinion. Fixed-point formats share pros and cons of finitely sized integer numbers and you should have no difficulty to analyze them. In short, they offer a smaller dynamic range compared to FP, but its truncation model is much simpler to reason. In turn you will get a varying precision and out-of-bound issues. Rational number formats look very promising at the beginning, but they are much harder to implement efficiently. You will need a fast GCD algorithm (not Euclidean) and also have to handle out-of-bound numerators and denumerators. In fact, many rational number formats rely on arbitrary-precision integers precisely for avoiding those issues, and inherit the same set of issues---unbounded memory usage and computational overhead. Approximate rational number formats are much rarer, and I'm only aware of the Inigo Quilez's floating-bar experiment [1] in this space. Interval/ball/affine arithmetics and others are means to automatically approximate an error analysis. They have a good property of being never incorrect, but it is still really easy for them to throw up and give a correct but useless answer like [-inf, inf]. Also they are somewhat awkward in a typical procedural paradigm because comparisons will return a tri-state boolean (true, false, unsure). Nevertheless they are often useful when correctly used. Fredrik Johansson's Arb [2] is a good starting point in my opinion. Finally you can model a number as a function that returns a successively accurate approximation. This is called the constructive or exact real number, and simultaneously most expensive and most correct. One of the most glaring problems is that an equality is not always decidable, and practical applications tend to have various heuristics to get around this fact. Amazingly enough, Android's built-in calculator is one of the most used applications that use this model [3]. [1] https://iquilezles.org/articles/floatingbar/ https://iquilezles.org/articles/floatingbar/ [2] https://arblib.org/ https://arblib.org/ [3] https://dl.acm.org/doi/pdf/10.1145/2911981 https://dl.acm.org/doi/pdf/10.1145/2911981
- maxbond 3y agoWonderful comment, thank you very much for taking the time.
- deleted 3y ago[deleted]