Hacker News new | ask | show | jobs
by ploureiro 4609 days ago
"my own BitcoinJS fork (which also adds other improvements) does use plain numbers to store the number of satoshis."

Why wouldn't I feel tempted to use his fork?..

1 comments

my thoughts exactly. For those who don't know, never ever use floats for things like money. Try in windows calculator for example sqrt(4) - 2 and see why.

Strangely enough he talks about floating point rounding errors in one of the next paragraphs?

> For those who don't know, never ever use floats for things like money.

This is normally a very good rule to follow. The reason is that floating point is binary, whereas cash is decimal, so even an innocent number like 0.4 has no exact representation in binary (it's actually an infinite tail: 0.011001100110011...).

Here, however, I'm using floats NOT to store decimals; every single value that I store is an integer. There is absolutely no danger in using floating point numbers to store relatively small integers like 1253251126; the issue only arises in the context of very large numbers (specifically, those above 2^53) and decimals. If anyone can come up with a remotely realistic series of integer manipulations that will cause an inaccuracy in Javascript where all values always stay below 2^50.9, I will certainly abandon my choice of moving to integers at once; otherwise, I see no problem.

Still seems risky to me. There are some numeric calculations where input and output values are small but intermediate representations might be many times larger. It's not certain that all the people working after you are going to be aware of the assumptions intrinsic in your code.
It's explained rather well in the article, over several paragraphs, why it's okay to use floating point in this case.

The first thing we did in the programming class at university was to learn how the IEEE floating point standard works...

That sounds like a very dull programming class...
Yeah, motivating students was not part of their job description. It was all rather backward, and I expect many professors saw it as a way of weeding out the unworthy (here's a bunch of very abstract math, study this for months but we won't tell you why it's important to know as a physicist).
Floating point arithmetic is some of the least abstract math. (That doesn't make it more interesting without the right background and mindset, though.)
True, I wasn't clear I guess, but I was trying to say that not explaining WHY we had to learn something was a general feature of my university education, e.g. group theory in the case of math. They just put us physics students with the math students and we were expected to be motivated by the pure fun of learning math. If I had known how much group theory gets used in physics, I'd have put in more effort.

Same thing with the IEEE standard, it's not hard, but it was presented in a dull way, all theory.