1. 18
  1.  

  2. 29

    Replace JS for your favorite language:

    https://0.30000000000000004.com/

    JS just happens be everyone’s favorite punching bag nowadays ;)

    1. 9

      Most other languages I know of let you specify that a number is an integer ;)

      EDIT: wow this was a very bad brain fart, and I’m leaving it here as a testament to how quickly people will pile on on Javascript without thinking. Sorry everyone.

      1. 17

        0.1 + 0.2 doesn’t work well for integer math either.

        1. 2

          Multiple them by enough zeros to turn them into integers. Do the math in integer form for integer rules (1 + 2). Reverse the process to get float results.

          I’ve done that before a long time ago. I can’t remember why I did. Maybe just to dodge floats’ problems during number crunching. I know peoples’ reactions to it were funny.

          1. 2

            I’m going to point out that COBOL handles this tricky calculation “unsurprisingly” by default. We should all switch from JavaScript to COBOL.

          2. 2

            Upvoting because the edit is a good example of self-awareness and humility.

          3. 5

            the author does state:

            This behaviour is not unique to JavaScript. It is seen in every programming language where doubles are available, including C, C++, C#, Erlang, Java, Python and Rust.

          4. 6

            I very much like the way this blog presented the numbers out in full, that helps to see what’s really going on behind the scenes.

            1. 5

              It’s actually kind of interesting that you can exactly represent doubles in decimal but you can’t exactly represent decimal numbers as doubles. This is because 2 divides 10 but 10 does not divide 2. So with enough decimal digits, you have enough 2s in the denominator to exactly represent a fractional binary number.

            2. 3

              [Super Mario 64 menu theme]

              Great and creative way to set the tone!

              1. 2

                Yeah computers are stupid

                1. 2

                  Whoever attempts floating-point calculation with JavaScript is, of course, in a state of sin.

                  John von Neumann, probably.

                  1. 1

                    What language should one use for floating point calculations?

                    1. 6

                      TCL, of course, because EIAS!

                      1. 3

                        In most languages there are libraries to deal with precise numbers. For example, you can use Decimal in Python. https://docs.python.org/3/library/decimal.html

                        But it may not be as fast or have other inconveniences (for example, defining explicitly the precision)

                        1. 2

                          There are some good examples in the list @HugoDaniel posted in this thread.

                          Here’s something interesting for JS: http://mikemcl.github.io/decimal.js/

                          1. 1

                            If you want it to work correctly, then probably SPARK, C w/ tooling, or Gappa w/ language of your choosing. If performance isn’t an issue, there’s a pile of languages with arbitrary-precision arithmetic plus libraries for those without it. I’d say that’s the options.

                            Meanwhile, there’s work in formal methods on Exact-Real Arithmetic that could give us new options later. There was an uptick in it in 2018. I’m keeping an distant eye on it.