Hacker News new | ask | show | jobs
by barbersturgeon 3128 days ago
Honest question - why is that something to avoid? What does the floating point representation buy you that say an int64 doesn't, with the same choice of units ie. 1 = 10^-6 dollars?
2 comments

What if you need to calculate the percentage difference between two monetary values?

Ints wont cut it there.

Sure they will. To truncated whole percents, (N2-N1)×100÷N1, using integer ops, gets you that, to whole percents rounded half up, ((N2-N1)×100+50)÷N1 does it. For additional decimal places, increase the fixed multiplier (and added factor for round-half-up) by the appropriate power of 10.

Even if you are dealing in units of hundreds of billions of dollars tracked to mils, this gets you up to, I think, a hundred-thousandth of a percent with int64.

But with floats you just use a divide operation.
“Won’t cut it” and “will require 1-2 more operations” are very much not the same thing.
Tracking the decimal point is all that comes to my mind.