Nope. With about a hundred thousand factored items, things easily run off the rails. I’ve seen it. Just count cents, and see that rounding errors are kept in close, deterministic confines.
You can use Kahan summation to mitigate floating point errors. A mere 100 thousand floating point operations is a non-issue.
As a heads up computational physics and mathematics tackle problems trillions of times larger than any financial computation, that’s were tons of algorithms have been developed to handle floating point errors. Infact essentially any large scale computation specifically accounts for it.
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.
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.
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.
Nope. With about a hundred thousand factored items, things easily run off the rails. I’ve seen it. Just count cents, and see that rounding errors are kept in close, deterministic confines.
You can use Kahan summation to mitigate floating point errors. A mere 100 thousand floating point operations is a non-issue.
As a heads up computational physics and mathematics tackle problems trillions of times larger than any financial computation, that’s were tons of algorithms have been developed to handle floating point errors. Infact essentially any large scale computation specifically accounts for it.
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.
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.
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
f64will 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.As you said, better use integers. And that’s exactly what is done at this point.
Indeed, but there’s no need to shit on people using floats because in almost all cases they are fine too.
Where the heck did I “shit on people using floats”?