‹ BackHN Continuity

Thread

More floating point alternatives

53 points · 51 comments · vismit2000

  1. AlotOfReading · · focus · HN ↗
    Usually, if you know enough about your algorithms to select an appropriate float alternative, you also know enough to fix your float code and that's what you should actually do.

    That said, some of these aren't alternatives. Symbolic computation is a different thing entirely. Interval arithmetic can be built atop floats (e.g. IEEE-1788) and has its own zoo of unintuitive behaviors. BCD is better called a historical artifact than an alternative these days.

    It's really just rationals and decimal floats in this list, which probably don't solve the issues you have if you're considering float alternatives.

    1. TZubiri · · focus · HN ↗
      You can't "fix" floating point code if you are looking for deterministic answers. You just have to use other data types to handle money or complex mathematical operations like 0.2+0.1, no ifs and buts.
      1. messe · · focus · HN ↗
        Floating point is deterministic, what are you talking about?

        > You just have to use other data types to handle money or complex mathematical operations like 0.2+0.1

        Such as... decimal floating point.

        1. timschmidt · · focus · HN ↗
          > Floating point is deterministic, what are you talking about?

          Order of operations can change a result, for example. I suspect you mean that the algorithm never changes. While op means that mathematical operations which most folks would expect to be reliable are not.

          1. messe · · focus · HN ↗
            They're not associative, sure. But that's a very far cry from claiming they're non-deterministic.
            1. timschmidt · · focus · HN ↗
              There are enough problems for a 44 page paper titled "What Every Computer Scientist Should Know About Floating-Point Arithmetic"[1] I don't quibble on the language because I know what people mean.

              Most folks won't encounter most of the issues, generally. But expose your code to a large enough dataset, or be like me and write a CAD/CAM system with motion control and experience most of them.

              That's why I wrote hyperreal[2]

              1: <a href="https:&#x2F;&#x2F;www.cs.tufts.edu&#x2F;cs&#x2F;40&#x2F;docs&#x2F;WhatEveryComputerScientistShouldKnowAboutFloatingPointArithmetic.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cs.tufts.edu&#x2F;cs&#x2F;40&#x2F;docs&#x2F;WhatEveryComputerScienti...

              2: <a href="https:&#x2F;&#x2F;github.com&#x2F;timschmidt&#x2F;hyperreal" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;timschmidt&#x2F;hyperreal

              1. drfloyd51 · · focus · HN ↗
                You don’t quibble about what words mean when the words you choose have very specific meanings in exactly the subject area you are talking about?

                You make it really hard to take you seriously.

                1. timschmidt · · focus · HN ↗
                  No. It&#x27;s been quite some time since I realized that all language is a pidgin used to translate between individuals&#x27; unique lived experiences and points of reference. And find communication much more fluid and less confrontational when the focus is on shared meaning rather than perfect word choice. Especially when working with non-native speakers, but also just people in general. Stephen Fry captures the feeling: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Ovi7uQbtKas" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=Ovi7uQbtKas

                  When TZubiri made their original comment, I understood they were speaking about some or all of the issues outlined in the paper I linked. If you didn&#x27;t, that&#x27;s ok. If you think the referenced paper missed something, it&#x27;s OK to add that.

                  &gt; You make it really hard to take you seriously.

                  Same, bud.

                  1. rcxdude · · focus · HN ↗
                    These kinds of comments tend to happen when you are assuming a shared meaning that isn&#x27;t so shared. Especially in this context, it&#x27;s a good idea to be precise in your terminology when there is a commonly used vocabulary for talking about it. Especially when it comes to misconceptions about floating point, which is often treated as &#x27;random&#x27; when it generally isn&#x27;t.
                    1. timschmidt · · focus · HN ↗
                      Sometimes specificity matters. Usually when looking directly at some specific piece of code. In this conversation, the intended meaning was clear. Folks just love being the pedant and the brinksmanship which comes with it. There&#x27;s a propensity to derail otherwise useful conversations into discussions like this rather than dig deeper into what people meant, and were trying to communicate.
                      1. messe · · focus · HN ↗
                        &gt; In this conversation, the intended meaning was clear

                        Apparently it wasn&#x27;t, as the commenter who originally used the term has now clarified that they did indeed mean non-deterministic. You misunderstood them.

                        So maybe being specific about terminology here isn&#x27;t just pointless pedantry?

                        1. timschmidt · · focus · HN ↗
                          Regardless of his choice of words, he&#x27;s specified that he&#x27;s speaking about IEEE754 and the sort of issues which arise systemically from the compromises inherent in it&#x27;s design.

                          He can&#x27;t attest to them being the same ones in the paper I linked, but I can. Everything he&#x27;s said is in line with the situation detailed within.

                          He and I seem to understand each other. I get what he&#x27;s saying. How the issues with IEEE754 give rise to the nondeterminism he&#x27;s talking about. I should, I wrote a whole system of libraries to deal with them. Do you understand the conversation? Because it seems like you think we&#x27;re talking about two unrelated things and you&#x27;re still arguing over terminology.

                  2. TZubiri · · focus · HN ↗
                    Thanks for following the thread. I&#x27;ll clarify on my intended meaning was indeed a strict actual definition of determinism, but a broader definition of floating point, to include its actual usage. But fwiw, it was indeed possible that I was someone who confuses determinism for precision, but no.

                    When I said that floating points are not deterministic, I wasn&#x27;t very precise, but I do think that broadly speaking, floating point arithmetic, as used today, foregoes determinism, and this results from the very ethos of the foundational IEEE754 data type, the goal is to have a data type for approximate answers, turns out that when exact answers are sacrificed in the name of speed, so is determinism. And this has huge effects on modern day, Floating Point is used on separate hardware with parallel operations, and there&#x27;s race conditions that make most Machine Learning and AI computing irreproducible, and that indeed seems to be a consequence, as you mention, of the lack of associativity of FP.

                    So, that said, I would make two clarifications:

                    &gt;-- Floating Points

                    &gt;++ Floating Point computing

                    where by Floating Point computing would mean the actual application computing that we build, as opposed to &quot;Floating points&quot; referring to the ideal ancient standardized hardware layer abstractions.

                    And if necessary:

                    &gt; -- is

                    &gt; ++ tends to be

                    In order to be perfectly correct, which after all, is what we are going after.

                    So if pressed, I wouldn&#x27;t say &quot;Floating points are not deterministic&quot; but &quot;Floating Point computing tends to be non-deterministic&quot;, but I would feel very comfortable shorthanding it to &quot;Floating Points are non-deterministic&quot; anyways.

                    The paper cited is a bit hard for me, so I can&#x27;t verify if it matches what I&#x27;m saying. But I imagine by the date, it wouldn&#x27;t be able to address the issues that we can empirically from the advent of ML systems, but maybe it did foresee from a theoretical standpoint some of their limitations.

                    There&#x27;s a between-the-lines thesis here that there&#x27;s two main schools of computing nowadays, one that seeks perfection, and another that seeks approximations, the CPU&#x2F;GPU dichotomy is roughly analogous to the Mathematics&#x2F;Physics vs Engineering&#x2F;Industrial dichotomy.rroot@t14:&#x2F;mnt&#x2F;c&#x2F;Users&#x2F;TomZubiri&#x2F;Desktop# cat fixed.txt Thanks for following the thread. I&#x27;ll clarify on my intended meaning was indeed a strict actual definition of determinism, but a broader definition of floating point, to include its actual usage. But fwiw, it was indeed possible that I was someone who confuses determinism for precision, but no.

                    When I said that floating points are not deterministic, I wasn&#x27;t very precise, but I do think that broadly speaking, floating point arithmetic, as used today, foregoes determinism, and this results from the very ethos of the foundational IEEE754 data type, the goal is to have a data type for approximate answers, turns out that when exact answers are sacrificed in the name of speed, so is determinism. And this has huge effects on modern day, Floating Point is used on separate hardware with parallel operations, and there&#x27;s race conditions that make most Machine Learning and AI computing irreproducible, and that indeed seems to be a consequence, as you mention, of the lack of associativity of FP.

                    So, that said, I would make two clarifications:

                    &gt;-- Floating Points

                    &gt;++ Floating Point computing

                    where by Floating Point computing would mean the actual application computing that we build, as opposed to &quot;Floating points&quot; referring to the ideal ancient standardized hardware layer abstractions.

                    And if necessary:

                    &gt; -- is

                    &gt; ++ tends to be

                    In order to be perfectly correct, which after all, is what we are going after.

                    So if pressed, I wouldn&#x27;t say &quot;Floating points are not deterministic&quot; but &quot;Floating Point computing tends to be non-deterministic&quot;, but I would feel very comfortable shorthanding it to &quot;Floating Points are non-deterministic&quot; anyways.

                    The paper cited is a bit hard for me, so I can&#x27;t verify if it matches what I&#x27;m saying. But I imagine by the date, it wouldn&#x27;t be able to address the issues that we can empirically from the advent of ML systems, but maybe it did foresee from a theoretical standpoint some of their limitations.

                    There&#x27;s a between-the-lines thesis here that there&#x27;s two main schools of computing nowadays, one that seeks perfection, and another that seeks approximations, the CPU&#x2F;GPU dichotomy is roughly analogous to the Mathematics&#x2F;Physics vs Engineering&#x2F;Industrial dichotomy.

                    1. timschmidt · · focus · HN ↗
                      &gt; when exact answers are sacrificed in the name of speed

                      I have found in <a href="https:&#x2F;&#x2F;github.com&#x2F;timschmidt&#x2F;hyperreal" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;timschmidt&#x2F;hyperreal and the geometry layers built atop it, that exactness and speed are not fundamentally opposed. Hyperreal retains exact mathematical structure at the scalar level, and at each level higher up through vertices, vectors, matrices, triangles, curves, etc. And uses it to choose optimal datatypes, dispatch against the fastest algorithms, and eliminate large chunks of needless calculation where simpler proofs are sufficient.

                      The downside is a bit of additional code and API complexity. But the slightly more complex API accurately represents issues like uncertainty in decidable equality of arbitrary computables in bounded time, which is a limitation of math itself, so representing it accurately in the API permits the algorithms to be more robust.

                      &gt; Floating Point is used on separate hardware with parallel operations, and there&#x27;s race conditions that make most Machine Learning and AI computing irreproducible

                      Deterministic reproducibility is, I think, one of the most awesome superpowers of a system like hyperreal. It makes simulation much more interesting and useful. As well as cross-platform use.

                    2. AlotOfReading · · focus · HN ↗

                          floating point arithmetic, as used today, foregoes determinism, and this results from the very ethos of the foundational IEEE754 data type
                      
                      This was somewhat true in the past, but the situation has been improving dramatically in recent years to the point where FP determinism is completely feasible. The remaining hurdles are primarily on the toolchains&#x2F;kernel side. I have a library called rfloat that you can drop into most C&#x2F;C++ code for practical determinism without thought (subject to documented caveats), for example. You can do the same thing manually with some more careful attention.

                      I&#x27;m in a very remote corner of the world on bad Wi-Fi though, so you&#x27;ll have to forgive omitted links.

                      1. TZubiri · · focus · HN ↗
                        &gt;in recent years to the point where FP determinism is completely feasible.

                        Ok, sure, it is feasible, but is that how it&#x27;s actually used? Or is it used in GPUs with thousands of processors running in parallel, and batching different operations, (and with temperature settings that add even more pseudo-indeterminism purposefully).

                        It&#x27;s funny that the customs actually go in the opposite direction of fabricating even more indeterminism, whether it is for being an accountability sink, or for fudging data to claim IP over the new mungled data, the FP&#x2F;GPU&#x2F;ML folk want magic, not determinism.

              2. dspillett · · focus · HN ↗
                That (no doubt excellent, but) technical PDF is overselling the problem somewhat, when what every dev needs to know is better represented by a friendlier summary like <a href="https:&#x2F;&#x2F;floating-point-gui.de&#x2F;" rel="nofollow">https:&#x2F;&#x2F;floating-point-gui.de&#x2F;
                1. timschmidt · · focus · HN ↗
                  That&#x27;s a great resource as well. Targeted at developers, rather than computer scientists. Same observations, different target audiences and expectations. You&#x27;re probably right that the more practical reference targeted at developers is more useful here. My references are full of the academic papers because of my CAD work.
                  1. dspillett · · focus · HN ↗
                    &gt; Targeted at developers

                    Sorry, yes, I probably should have worded that in a way that made the distinction more obvious.

        2. account42 · · focus · HN ↗
          CPUs have many different configuration bits to configure floating point rounding, flushing denormals, etc. which mean that in practice anything that relies on floats being deterministic has the stability of a house of cards.
          1. messe · · focus · HN ↗
            All of which are well defined and can be configured from user space. That doesn&#x27;t make them non-deterministic.
        3. [deleted] · · focus · HN ↗

          [deleted]

        4. Swapnopam · · focus · HN ↗

          [dead]

      2. AlotOfReading · · focus · HN ↗
        Floats are deterministic, but I get what you mean. Let&#x27;s discuss what&#x27;s meant by the result of a complex calculation. 0.1+0.2, or sqrt(2), or whatever.

        1. Do you want your result to exactly encode the answer without rounding error? No fixed precision type can provide this in general, so you&#x27;re stuck with symbolic approaches. If you can bound things (usually difficult), maybe you can get away with non-symbolic approaches.

        2. Do you want a sensible numeric answer? This is what floats (and many other systems) give you. The definition of &quot;sensible&quot; is inherently tricky here and there&#x27;s not a definition universally appropriate to every possible computation.

        So let&#x27;s return to 0.1+0.2=0.3000...1 specifically. There&#x27;s two common ways to think of an encoded float. One is as the directly encoded value, as you&#x27;re doing. Another way is to think of it as an interval of real numbers between the next lowest and highest intervals. Under this latter interpretation, it makes sense to discuss shortest decimal string within the interval, 0.3 in this case. There&#x27;s no ambiguity because each real lives in exactly one interval. This is what algorithms like dragon box do for float to decimal string conversion.

        What decimal floats give you is an encoding that tracks significant digits, where every decimal string exactly corresponds to a midpoint of an interval of reals. They do this at the cost of space, speed, and complexity. You don&#x27;t get an escape from the fundamental issues of fixed precision types like rounding error, numerical sensitivity, precision loss, etc. I don&#x27;t think that tradeoff makes sense for most algorithms in most contexts.

        The benefit of sticking with floats is that lots of smart people have spent countless hours trying to give non-experts a &quot;good enough&quot; path through the untamed wilds of numerical analysis, tooling to help them when they get lost, tribal knowledge to point out the edge cases, and it&#x27;s almost universally supported in hardware. By all means you should go wandering off the trail, but fully understand what you&#x27;re doing and why beforehand.

        1. SkiFire13 · · focus · HN ↗
          &gt; Another way is to think of it as an interval of real numbers between the next lowest and highest intervals. Under this latter interpretation, it makes sense to discuss shortest decimal string within the interval, 0.3 in this case.

          Note that the result of 0.1+0.2 does not lie in the interval containing 0.3, which is was confuses most people. The issue is that there is some imprecision in representing 0.1 and 0.2 too, and that compounds when summing, resulting in something that does not actually correspond to 0.3 (hence the classic 0.1+0.2!=0.3)

          1. hyperhello · · focus · HN ↗
            But, as I understand it, 1+2=3 in all of these senses using floating point; as long as you don’t go outside of a certain very large range, they are really a superset of integers.

            That makes me think that I can just plan ahead by storing the number of cents instead of dollars, or a “hack”, and then it makes me wonder why the format even requires me to do that.

            1. SkiFire13 · · focus · HN ↗
              &gt; That makes me think that I can just plan ahead by storing the number of cents instead of dollars

              That&#x27;s what is generally suggested when handling money, at least for normal businesses (in finance related businesses you might have to handle a lot more decimal places for things like exchange rates, fractions of cryptos etc etc)

              Be careful though that some currencies require more than 2 decimal places.

      3. SkiFire13 · · focus · HN ↗
        &gt; deterministic answers

        &gt; 0.2+0.1

        0.2+0.1 with floating point numbers _is_ deterministic, as you&#x27;ll always get the same answer.

        I suspect you might instead mean exact calculations&#x2F;answers (in the example above, neither 0.1, 0.2 nor 0.3 have exact representations using floating point numbers).

        And just to be clear, there are non-determinism-like issues with floating point numbers, but those are much rarer&#x2F;niche and _can_ be fixed. For example parallel summation depends on the order the summation was made, so non-determinism in the parallel implementation ripples through the summation result. Some non-basic operations (e.g. trigonometric operations) have platform dependent implementations with different roundings, so you might experience different result based on the platform you&#x27;re on.

    2. lelanthran · · focus · HN ↗
      &gt; BCD is better called a historical artifact than an alternative these days.

      It&#x27;s currently in use all over the world. You can&#x27;t do a card payment, either in-person or online, without an intermediary using ISO8583.

      1. kjs3 · · focus · HN ↗
        I think people with certain backgrounds look at stuff like x86 BCD opcodes being removed in the move from x86-32 to amd64 and think &quot;must be because noone uses it&quot;. Other backgrounds know that ain&#x27;t so.
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.