• Zarobi@aussie.zone
    link
    fedilink
    English
    arrow-up
    5
    ·
    3 days ago

    As far as I understand, it seems to sacrifice accuracy at larger numbers, for more accuracy at smaller numbers. As they say, that seems like a reasonable tradeoff, because we tend to mostly use smaller numbers in our software.

    In my honest opinion though, if accuracy is a concern, I reach for decimal or integer based mathematical libraries. For example, in a JavaScript project that needed to handle currency, I used Dinero, because $0.1 + $0.2 needs to actually work properly. As far as I understand, it just does all math in integers, and then you can display it as a string in the UI or export it as an integer for storage as a realised decimal in the database.

    • iocase@lemmy.zip
      link
      fedilink
      English
      arrow-up
      3
      ·
      2 days ago

      It’s easier to just compute the smallest atomic amount of a currency, usually cents, using integers. Then display it as full dollars, pounds, euros—what have you—to the end user. For Division you need some way to account for the remainder across transactions, but that’s probably rules from accounting and not integer math.

      What’s the benefit of Dinero? Is that like the financial equivalent of the iseven() dependency?

      • Zarobi@aussie.zone
        link
        fedilink
        English
        arrow-up
        1
        ·
        2 days ago

        JavaScript is a bit funny and doesn’t actually have native integers… every number is a floating point, which means even whole number math gets weird sometimes. Not often, but enough that cents were lost and people were upset.

        I haven’t looked at how Dinero works under the hood, but I never had any problems at all with it after we started using it. Front end works in cents, back end works in decimal, is what I’d suggest for anyone else reading this. Statically typed languages are much easier to control and you don’t need a library at all if you’re careful.

        • iocase@lemmy.zip
          link
          fedilink
          English
          arrow-up
          1
          ·
          2 days ago

          That’s cursed.

          I don’t know how people deal with JavaScript. It’s like it was made for rapidly making shitty canoes out of tarps and duct tape for the early internet, and now you’re expected to build deep sea submersibles with reinforced tarps and extra-strong duct tape. Wtf…

          • Zarobi@aussie.zone
            link
            fedilink
            English
            arrow-up
            1
            ·
            2 days ago

            Yeah… I like the syntax, but I hate the old jank. Still one of my favourite languages regardless. But definitely your first year will be variations of “why does JavaScript do that” 😂 I damn near quit when I had to do timezone and scheduling stuff with native date objects

            • iocase@lemmy.zip
              link
              fedilink
              English
              arrow-up
              2
              ·
              2 days ago

              Btw I’m not the one downvoting you someone else is. And yeah it sounds like an entirely different beast from normal programming almost like memory constrained embedded programming is its own beast.

                • iocase@lemmy.zip
                  link
                  fedilink
                  English
                  arrow-up
                  2
                  ·
                  2 days ago

                  Lol that makes a lot of sense now! I thought you had a hate stalker downvoting every new comment you make which seems to be a thing on Lemmy for some reason.

                  • Zarobi@aussie.zone
                    link
                    fedilink
                    English
                    arrow-up
                    1
                    ·
                    2 days ago

                    I do also have those, but they got bored and lost interest. They do still occasionally get triggered by my username though 😂