• HStone32@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    5 months ago

    I’ve wondered why programming languages don’t include accurate fractions as part of their standard utils. I don’t mind calling dc, but I wish I didn’t need to write a bash script to pipe the output of dc into my program.

    • addie@feddit.uk
      link
      fedilink
      arrow-up
      0
      ·
      5 months ago

      You can only store rational numbers as a ratio of two numbers, and there’s infinitely times more irrational numbers than rational ones - as soon as you took (almost any) root or did (most) trigonometry, then your accurate ratio would count for nothing. Hardcore maths libraries get around this by keeping the “value in progress” as an expression for as long as possible, but working with expressions is exceptionally slow by computer standards - takes quite a long time to keep them in their simplest form whenever you manipulate them.

      • CanadaPlus@lemmy.sdf.org
        link
        fedilink
        arrow-up
        1
        ·
        5 months ago

        You could choose a subset of fractions, though, and then round it to the nearest one. Maybe you could use powers of two as the denominator for easy hardware implementation. Oh wait, we’ve just reinvented floats.

    • lone_faerie@lemmy.blahaj.zone
      link
      fedilink
      English
      arrow-up
      0
      ·
      5 months ago

      A lot of work has gone into making floating point numbers efficient and they cover 99% of use cases. In the rare case you really need perfect fractional accuracy, it’s not that difficult to implement as a pair of integers.