Live data from Hacker News

Floats Are Weird

a.exozy.me

61–70 of 120 posts

Re: Floats Are Weird

#61
post #28

Earlier quoted context omitted.

Assuming you want your money to actually add up correctly, then floats are always the wrong choice. If you’re not interested in accurate accounting, the. Sure floats are fine, but when you’re working with money, accurate accounting tends to be the expectation.

Addition is one of those things that does work pretty predictably and error free with floats. The problem with 0.30000000000000004 etc is usually that the things you are adding are not what you expect (float(0.1) != 0.1), i.e. the difficulty usually is string float conversions rather than float arithmetic itself.

Even that error is a problem. Doing a single addition might be fine, but doing thousands or millions of additions, and those tiny errors add up to something appreciable.

If you’re doing money operations at a scale where the computational difference between using true number type with infinite precision vs floats is worth thinking about, then you’re also in the territory where tiny floating point errors really stack up into a problem.

As a consequence, there’s very few scenarios where using floats for money actually makes sense. Either your computation is so simple there’s no real benefit to using floats rather than infinite precision number types, or your computation is so large, that floating point errors sum to meaningful amounts.

The only scenario I can think of where floats might be the right choice is on tiny embedded system where computational power and memory is very limited, and working with infinite precision types is a real problem. But in that scenario, you probably don’t need to be educated on the issues with floats. But if you’re the kind of person who unsure if you should use floats for money, then the answer is almost certainly a resounding “No. Do not use floats for money values”.

Re: Floats Are Weird

#62
post #17

I decided years ago that the next time I hear someone suggesting we use floats / doubles to represent money amounts, I am going to punch them in the face.

I heard that countless times, and I understand the reason (mainly: floats are binary, money amounts are decimal), but then, how do you split a $10 bill between 3 people.

$3.33 for each is not good, because in the end you have to pay $10, not $9.99. You can use a double, which is more precise, but in a less predictable way. You can use factions, which is exact, but it may become unmanageable after some time. Or you can have one of the three pay $3.34, but which one? I guess there are rules for that, probably a lot more complicated than "use an integer number of cents".

Re: Floats Are Weird

#63
post #37

Earlier quoted context omitted.

The rule as stated is way too strong, for example option prices are money amounts but floats are unavoidable in calculating them. For a less exotic example, consider that Excel, widely used by actual accountants, uses floats throughout to represent numbers.

lol… If you’d ever had to bill millions of customers for precise amounts of electricity and gas at precise prices… you would hate floats and you’d hate that any idiot will act as though excel is gospel truth.

That's also the case with integer or fixed-point calculations. You generally don't care about the accuracy of specific calculation (unless it results in edge cases like a catastrophic cancelation in floats), but you do make sure that the resulting invoice is free from any sort of numeric artifacts like the sum of ratios equals to 99.9% or 100.1%.

Re: Floats Are Weird

#64
post #45

Hotter take: Floats are bad and shouldn't be used. It's clear that even experienced programmers do NOT understand how they work, how they don't work, and the pile of edge cases. Use something saner, like binary coded decimal, larger types, and use floats as a last and very approximate resort.

No, it's clear people don't know how to compute (because they're not taught how to). The vast majority of issues that people encounter with floating point aren't floating point issues, it's "how do I perform this calculation without infinite space and time", and these issues occur whether or not you do it by hand, or use a machine to do it for you. This issue becomes really obvious when you teach early-undergrad science labs, because people conflate the number of decimal points used with the number of significant figures used. This can be seen between the definition of the speed of light (which has in effect infinite precision because we defined what it is) as 299792458 m/s, vs the gravitational constant with 0.000 000 000 066 74 m^3 /kg /s^2 which is generally regarded as the most inaccurate and hardest to measure physical constant (usually G * M can be measured more accurately, so you'd rather use that).

Re: Floats Are Weird

#65
post #46
post #17

I decided years ago that the next time I hear someone suggesting we use floats / doubles to represent money amounts, I am going to punch them in the face.

We use long double to present financial money amounts with a little safety on top of it before it’s consumed by whatever JavaScript (Typescript really) frontend it heads to. Works fine. Outside of the need for speed it’s one of the few areas we use c in our backend services. We don’t store the data in floats or anything resembling it, however.

Are you using also doubles for calculations or just presentation? Either way doesnt pass the smell test for me.

Re: Floats Are Weird

#66
post #28

Earlier quoted context omitted.

Addition is one of those things that does work pretty predictably and error free with floats. The problem with 0.30000000000000004 etc is usually that the things you are adding are not what you expect (float(0.1) != 0.1), i.e. the difficulty usually is string float conversions rather than float arithmetic itself.

Even that error is a problem. Doing a single addition might be fine, but doing thousands or millions of additions, and those tiny errors add up to something appreciable. If you’re doing money operations at a scale where the computational difference between using true number type with infinite precision vs floats is worth thinking about, then you’re also in the territory where tiny floating point errors really stack u…

I would argue the more correct answer is if you don't know what maths to use for money (including e.g. any legal rules about how you do it), you shouldn't be doing maths on money.

Re: Floats Are Weird

#67
post #26
post #17

I decided years ago that the next time I hear someone suggesting we use floats / doubles to represent money amounts, I am going to punch them in the face.

This gets repeated a lot, and I don't disagree. But I find odd that doubles would be so unsuitable for monetary (and other similar) arithmetic; in principle you have 15 significant digits which should be more than enough, and precise control how the results are rounded. And all the basic arithmetic should return correctly rounded values to the last ULP. So it is weird that those tools are still not good enough and it…

The problem is intrinsic to floats, not just due to bad implementations.

Some values like 0.3 simply cannot be represented precisely with floats.

Re: Floats Are Weird

#68
post #55

Earlier quoted context omitted.

> That is the one example that floats around a lot, but its also imho not very good one. '0.1', '0.2', and '0.3' are not floating point values, so the premise is flawed. No, it’s the entire point. None of the values we deal with day to day are binary floating point, and certainly not currencies. So this sort of representational approximations is a major and constant issue of using floats. > Also `round(0.1 + 0.2, 15)…

> See above, rounding off and collecting error after every arithmetic operation is not the expected norm and what developers are taught. Question is, is that a problem with developers or floats? :) Ecosystem and tooling might help here, iirc that is something Kahan himself has been complaining about a lot. For example hypothetically you could have something like FP contexts or specialized high-level types where you c…

Question is, is that a problem with developers or floats?

Of course with floats. Requirements come from decimal-expecting people and developers have to convert requirements into an algorithm. If there’s a fundamental semantic or at least syntactic obstacle, it’s not a problem with developers.

Iow, if a language/system only has floats as “numbers”, it sucks for most business-level calculations.

Re: Floats Are Weird

#69
post #65
post #46

Earlier quoted context omitted.

We use long double to present financial money amounts with a little safety on top of it before it’s consumed by whatever JavaScript (Typescript really) frontend it heads to. Works fine. Outside of the need for speed it’s one of the few areas we use c in our backend services. We don’t store the data in floats or anything resembling it, however.

Are you using also doubles for calculations or just presentation? Either way doesnt pass the smell test for me.

It depends we sometimes do since quadruple precision with checks tends to be safe, but for the most parts we don’t as most things are basically transactions unless you need to display something.

Re: Floats Are Weird

#70
post #62
post #17

I decided years ago that the next time I hear someone suggesting we use floats / doubles to represent money amounts, I am going to punch them in the face.

I heard that countless times, and I understand the reason (mainly: floats are binary, money amounts are decimal), but then, how do you split a $10 bill between 3 people. $3.33 for each is not good, because in the end you have to pay $10, not $9.99. You can use a double, which is more precise, but in a less predictable way. You can use factions, which is exact, but it may become unmanageable after some time. Or you ca…

Accountants avoid academic penny drama.

If you have a few grown-up adult parties, e.g. cofounders, partners, then split like [(n-1) x round(total/n), whats_left]. The last one is how sql select sees it.

If you have potentially penny-hysterical kinds (taxes, anonymous group customers), round in their favor and throw pennies into your own expenses.

If n ~~ total, e.g. $100.00 over 700 people, don’t do that, it’s bad accounting.

I worked with finance and accounting half my life. They just don’t fall into these philosophical dilemmas.

Post reply on HN