this post was submitted on 24 Sep 2025
96 points (97.1% liked)

Programming

23462 readers
16 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] Womble@piefed.world -3 points 1 month ago* (last edited 1 month ago) (11 children)

You are underestimating how precice doubles are. Summing up one million doubles randomly selected from 0 to one trillion only gives a cumulative rounding error of ~60, that coud be one million transactions with 0-one billion dollars with 0.1 cent resolution and ending up off by a total of 6 cents. Actually it would be better than that as you could scale it to something like thousands or millions of dollars to keep you number ranger closer to 1.

Sure if you are doing very high volumes you probably dont want to do it, but for a lot of simple cases doubles are completely fine.

Edit: yeah using the same million random numbers but dividing them all by 1000 before summing (so working in kilodollars rather than dollars) gave perfect accuracy, no rounding errors at all after one million 1e-3 to 1e9 double additions.

[–] Treczoks@lemmy.world 4 points 1 month ago (10 children)

The issue is different. Imagine you have ten dollars that you have to spread over three accounts. So this would be 3.33 for each, absolute correctly rounded down. And still, a cent is missing in the sum. At this point, it is way easier to work with integers to spread leftovers - or curb overshots.

[–] FizzyOrange@programming.dev 2 points 1 month ago (4 children)

That doesn't make any sense. As you say, in that case you have to "spread leftovers", but that isn't really any more difficult with floats than integers.

It's better to use integers, sure. But you're waaaay over-blowing the downsides of floats here. For 99% of uses f64 will be perfectly fine. Obviously don't run a stock exchange with them, but think about something like a shopping cart calculation or a personal finance app. Floats would be perfectly fine there.

[–] amju_wolf@pawb.social 3 points 1 month ago

As someone who has implemented shopping carts, invoicing solutions and banking transactions I can assure you floats will be extremely painful for you.

A huge benefit of big decimals is that they don't allow you to make a mistake (as easily) as floats where imprecision just "creeps in".

load more comments (3 replies)
load more comments (8 replies)
load more comments (8 replies)