Adding Floating-Point Decimals for Fun and Profit
Thread
Loading the complete thread in the background. This saved snapshot is available now. Refresh
Unofficial Hacker News client; not affiliated with Y Combinator.
Adding Floating-Point Decimals for Fun and Profit
Loading the complete thread in the background. This saved snapshot is available now. Refresh
Unofficial Hacker News client; not affiliated with Y Combinator.
amelius · · focus · HN ↗
Why not, rounding to the nearest cent is going to be much less precise for any realistic amount of dollars when using 64 bit floats.
glimshe · · focus · HN ↗
The article's images clearly show the rounding error mess your get without decimals.
amelius · · focus · HN ↗
glimshe · · focus · HN ↗
andreareina · · focus · HN ↗
sobriquet9 · · focus · HN ↗
knorker · · focus · HN ↗
You could end up with splitting an account down the middle, and ending up with an extra cent being created out of thin air, or one destroyed. In billions of transactions each day, this could be a problem for balancing books when there is no longer any equality check.
When not using floats, the rules and checks become more… deterministic, if you don't mind stretching the definition of that word a bit.
sluukkonen · · focus · HN ↗
adrian_b · · focus · HN ↗
They use binary fixed-point numbers/integers, where such computations are exact, while not having the huge computational overhead of decimal floating-point numbers.
OGWhales · · focus · HN ↗
Decimal floating-point is also hardware-supported, so it doesn’t carry the same computational overhead it does on platforms where it must be implemented in software.
adrian_b · · focus · HN ↗
IBM has done this, despite it being an inferior technical solution, because it binds those who choose it to IBM hardware.
Programming currency operations with decimal floating-point numbers is easier for naive programmers.
Even on IBM mainframes, implementing currency operations using 64-bit or 128-bit integers is much faster and less resource-consuming than with decimal numbers, but the implementation of some of the operations can be a little tricky, when it must be guaranteed that no loss of precision may occur.
I think that I might have seen recently an announcement from someone else than IBM who has introduced hardware support for decimal floating-point numbers, perhaps from Fujitsu. In any case, whoever introduces such hardware support does it to lure some customers to migrate from IBM to them, and not because it were a good solution for implementing operations with money.
OGWhales · · focus · HN ↗
[dead]
OGWhales · · focus · HN ↗
AlotOfReading · · focus · HN ↗
Changing representations isn't a substitute for the numerical analysis you should be doing for financial calcs.
vatsachak · · focus · HN ↗
bee_rider · · focus · HN ↗
I mean, just as an example, your saving account interest could be computed continuously, e^(rt), which would obviously be better approximated in floating point. But they won’t have to do any approximation if they write the contract so that it compounds nightly and is rounded to the nearest, whatever, tenth of a cent (which means that the rounding isn’t an approximation at all, it is part of the definition of the value being represented).
clickety_clack · · focus · HN ↗
sieve · · focus · HN ↗
One way to do this is to separate the computation/storage and presentation layer: use integers to store values as cents for computation/storage and only convert to dollars+cents on display. But then someone might suddenly demand three digits after the decimal point and you cannot go about changing every stored value just because the logic changed in one part of the application. So, using decimals is the safer choice.
I prefer integers for my plain text ledger software though because the format is in my control.
hansvm · · focus · HN ↗
PaulDavisThe1st · · focus · HN ↗
adrian_b · · focus · HN ↗
You just have to implement the conversion function carefully, i.e. the exchange ratio would actually be represented not by a single number, but by a ratio of suitable integers, to ensure no loss of precision during the conversion done by multiplication with extended result and then division.
adrian_b · · focus · HN ↗
If you want to be able to count trillions of trillions of dollars with a resolution of the millionth part of a cent, than you can use 128-bit integers.
Computing exactly with 128-bit integers is many times faster than computing only approximately with decimal floating-point numbers and it requires less memory storage for the same dynamic range.
fourier54 · · focus · HN ↗
adrian_b · · focus · HN ↗
This means that the 64-bit decimal64 format still provides an acceptable precision of 16 digits, even if it is lower than for 64-bit integers, which provide over 18 digits.
If the goal is to avoid rounding, 64-bit binary integers are still superior to decimal64, due to the availability of more significant digits, but due to its scale factor decimal64 is superior if you want to express a huge amount of money while allowing rounding, which would probably be needed only for amounts greater than the budgets of all countries together.
You are right that decimal64 provides a greater dynamic range than 128-bit binary integers, but even so the dynamic range of 128-bit integers is many orders of magnitude greater than needed for any imaginable amount of money, while providing exact operations performed at a speed many times greater than for decimal64.
On computers without hardware support for the IEEE standard decimal formats, much faster operations with decimal floating-point numbers can be implemented if they are not represented like in the standard, but as a product between a binary integer and an integer power of ten.
nly · · focus · HN ↗
Just before you send something on the wire you just make sure you serialize (part of which is rounding) to the appropriate ticksize.
Generally speaking tick sizes (cents, valid trade price increments, whatever) are many orders of magnitude greater than any possible calculation error, so rounding to nearest just works
sieve · · focus · HN ↗
Internally, you may use whatever precision/scale you want, and compute using doubles or decimals or integers. But I want to see 100.01 - (37.29 + 41.63) = 100.01 - 78.92 = 21.09 in all of these statements.
krige · · focus · HN ↗
jgalt212 · · focus · HN ↗
Decimals or pennies, you still have rounding errors. For example, if your contract is $100 / year. You will get the full $100 if you bill yearly, quarterly, or semi-annually. But if you bill monthly, no matter if you deal in pennies or fractional dollars, the sum total of your invoices for the year will be less than $100.
gottheUIblues · · focus · HN ↗
mytailorisrich · · focus · HN ↗
jgalt212 · · focus · HN ↗
astrobe_ · · focus · HN ↗
nitwit005 · · focus · HN ↗
sobriquet9 · · focus · HN ↗
NaoEhSavio · · focus · HN ↗
[dead]
kphorn · · focus · HN ↗
<a href="https://www.youtube.com/watch?v=yZjCQ3T5yXo" rel="nofollow">https://www.youtube.com/watch?v=yZjCQ3T5yXo
"all right so when the subroutine compounds the interest, it uses all of these extra decimal places that just get rounded off. So we simplified the whole thing, we round them all down and drop the remainder... in to an account that we opened"
dublin · · focus · HN ↗
Only true propellerheaded programmers should ever see or care about binary floating point issues. NB: I do massively scalable wireless data collection networks for IoT (using variously, floats, doubles, and half-precision, as called for by their use case), so I'm one of those people, but ordinary folks and users should never, ever, be impacted by binary imprecision.