Live data from Hacker News

Your balance is $0.30000000004

medium.com

11–20 of 69 posts

Re: Your balance is $0.30000000004

#11
post #4
post #2

You would think that this is a fairly rookie error and that big companies would know better, but I regularly see this on Uber: https://i.imgur.com/qDACtG0.png

On the surface one would think big companies should "know better". I mean, a large company is the accumulation of centuries or even millennia of career experience. Unfortunately, they also contain just as much accumulated nonsense and foolishness; and enough bureaucracy and organizational cruft to hide it forever.

Plus there’s the issue that the career experience isn’t evenly distributed, so you can often have people implementing features without the background necessary to do them right.

Re: Your balance is $0.30000000004

#13

Work with cents everywhere and format the output in the UI. Done.

Not that simple, though. That's the "division and rounding" method of the article: share a $.99 fee equally between two parties. That's either 49 cents each or 50 cents each if you're not careful. In both cases, where is the extra cent?

Re: Your balance is $0.30000000004

#14
post #12

Work with cents everywhere and format the output in the UI. Done.

Unless I have to actually work with floats, I try to only do everything with int math to keep things sane. Not to mention, that it usually makes calculations faster.

Decimal floating point exists (https://en.wikipedia.org/wiki/Decimal64_floating-point_forma...), is that considered useable for financial data, or does it turn out in practice to also suffer from the same issues? Since, e.g., dividing through 3 will not be exact here either of course.

Re: Your balance is $0.30000000004

#15

Work with cents everywhere and format the output in the UI. Done.

Not that simple, though. That's the "division and rounding" method of the article: share a $.99 fee equally between two parties. That's either 49 cents each or 50 cents each if you're not careful. In both cases, where is the extra cent?

If you use floats and rounded that one cent would disappear with them as well.

Unless you're storing half cents in their account and not showing it.

Which is perfectly legitimate by the way, because if you earn a fraction of a cent interest accumulated over a couple months that might actually be a cent and you have to pay it.

The solution might be to store everything as 1/100 of a cent, as an integer.

Re: Your balance is $0.30000000004

#16

Work with cents everywhere and format the output in the UI. Done.

This approach doesn't work when dealing with fractional cents, like charging $0.0001 per hour for something.

I would rather recommend using an arbitrary precision decimal library like Decimal.js, and using strings to represent money in JSON. And of course using an appropriate database type for storing the data.

Re: Your balance is $0.30000000004

#17
post #12

Earlier quoted context omitted.

Unless I have to actually work with floats, I try to only do everything with int math to keep things sane. Not to mention, that it usually makes calculations faster.

Decimal floating point exists ( https://en.wikipedia.org/wiki/Decimal64_floating-point_forma... ), is that considered useable for financial data, or does it turn out in practice to also suffer from the same issues? Since, e.g., dividing through 3 will not be exact here either of course.

It doesn't have the same issues (at least from my experience in C#) but it runs quite a bit slower than normal floats and doubles:

http://csharphelper.com/blog/2017/05/compare-the-performance...

Re: Your balance is $0.30000000004

#18
post #16

Work with cents everywhere and format the output in the UI. Done.

This approach doesn't work when dealing with fractional cents, like charging $0.0001 per hour for something. I would rather recommend using an arbitrary precision decimal library like Decimal.js, and using strings to represent money in JSON. And of course using an appropriate database type for storing the data.

Just make the smallest possible fractional cent the unit then

Re: Your balance is $0.30000000004

#19

Work with cents everywhere and format the output in the UI. Done.

The smallest unit of US money is the mill or 1/1000 of a dollar. The smallest unit of US currency is the cent. You're supposed to work in mills and round to cents.

Or just work in floats and round as the last step. Or do both selectively depending on which rounding error works in your favor, but don't tell anyone that's what you're doing.

The real problem is that nobody at the company seems to have looked at the parts of their software that everyone sees, so what else have they not looked at?

Re: Your balance is $0.30000000004

#20
post #9

Good example of a leaky abstraction. JavaScript developers almost never have to worry about or completely understand the intricacies of floating-point arithmetic, until errors like this happen. https://en.wikipedia.org/wiki/Leaky_abstraction

This is not a leaky abstraction. JavaScript doesn't pretend that the numeric type is anything other than an IEEE-754 float. It would be a leaky abstraction if JavaScript said "this type can represent all real numbers!" but that would be insanity.

[deleted]
Post reply on HN