• Quetzalcutlass@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    3 months ago

    I feel obligated to post a link to this excellent (albeit incomplete) writeup of posits and unums, which can more accurately represent common fractions in memory at the same (or lower) bit size compared to traditional floating point.

    Said page also includes the single best slightly-above-entry-level explanation of how floating point works I’ve ever seen.

    • LurkingLuddite@piefed.social
      link
      fedilink
      English
      arrow-up
      1
      ·
      3 months ago

      Isn’t a big point of floating point that it can easily have mathematical operators done on them on CPUs? Sure, complex circuitry is no big deal these days, but IIRC, there were very good reasons for what was chosen.

      • Quetzalcutlass@lemmy.world
        link
        fedilink
        English
        arrow-up
        1
        ·
        3 months ago

        One of the linked papers makes the claim that the circuitry would be less complex than that needed for floats, but also includes this note:

        Floats have one advantage over posits for the hardware designer: the fixed location of bits for the exponent and the fraction mean they can be decoded in parallel. With posits, there is a little serialization in having to determine the regime bits before the other bits can be decoded. There is a simple workaround for this in a processor design, similar to a trick used to speed the exception handling of floats: Some extra register bits can be attached to each value to save the need for extracting size information when decoding instructions.

        The proposed system also lacks NaN and separate positive/negative infinities, so it wouldn’t be a drop-in replacement.

    • Zarobi@aussie.zone
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 months 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
        0
        ·
        edit-2
        2 months 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 months 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.