4 ms·
Unless you're dealing with billions and care about pennies then using float for money is fine in 99% of cases.
by IshKebab 4y ago
Unless you're dealing with billions and care about pennies then using float for money is fine in 99% of cases.
- dec0dedab0de 4y agoThat's only true if you know that you have to round everything to the second digit. If you have a user calculating the total cost of buying something that is $7.10 and something that is $10.20, you don't want to show them $17.299999999999997. You would be better off storing everything as an integer of pennies and just displaying the dot in the frontend. To be fair, I also think that high level languages should come with types for all the major currencies.
- kazinator 4y agoI'm an expert programmer and I agree with downvoted IshKebab. This is just HN having a Reddit moment. IEEE 64 bit floats are accurate to just past 15 decimal digits. For ordinary monetary amounts, the exact figure in cents is approximated with ridiculous precision. If you rub two pennies together, you are likely causing more of a difference in the amount of copper than the IEEE 64 bit float causes in the value. You have to do many, many additions and multiplications before you get a result which has accumulated so much error that it is now closer to the wrong penny. E.g. if you don't deal with dollar amounts more than 7 figures, you have about 8 places past the decimal point; you need something like a 6 place error before the penny is affected. You can counteract this problem by correcting intermediate results to that floating-point value which is closest to exact penny result. In other words, throughout your calculation, you truncate away the difference between the result, and the best approximation of the dollar and cent value. Within this framework, you can implement all required rounding rules, too. You can take a floating-point result representing a fraction of a penny and round it according to banker's rule to the penny. Of course, you can't just ignore the issue and just blindly use floating-point for money in a serious accounting system; but that's a strawman version of using floating-point for money. Also, Microsoft Excel uses floating point. See here: https://learn.microsoft.com/en-us/office/troubleshoot/excel/floating-point-arithmetic-inaccurate-result https://learn.microsoft.com/en-us/office/troubleshoot/excel/... Vast armies of people rely on Excel for financial calculations.
- bheadmaster 4y ago> You have to do many, many additions and multiplications before you get a result which has accumulated so much error that it is now closer to the wrong penny > You can counteract this problem by correcting intermediate results to that floating-point value which is closest to exact penny result The argument seems to be "floats are okay, as long as you're careful", but forgetting to round the number in between a large number of operations is a probable mistake. Using decimals would make such a mistake impossible.
- kazinator 4y agoEven with a decimal representation, there are situations where you have to remember to round to a cent. Decimals are software libs; what could go wrong? More people use the floating-point instructions of a popular CPU than any given decimal library. If you're starting from scratch, it's probably a lot less work to write (test and debug) a Money class based on floating-point, whose overloaded math operations do the required rounding (so that code using the class cannot forget) than to make a Money class based on decimal arithmetic. (The last time I wrote an accounting system, I made a Money class based on integers. It could be retargeted to other representations easily. I could make the change and compare the ledger totals and other reports to see if there is a difference.)
- bheadmaster 4y ago> Decimals are software libs; what could go wrong? Bugs in libraries do exist, but it's much easier to fix a bug in one place, than to track down every single line where floating point operations could misbehave.
- lmm 4y ago> More people use the floating-point instructions of a popular CPU than any given decimal library. Perhaps, but how many of the former are in a position to notice accuracy issues? I have more faith in a reputable decimal library than your average FPU, frankly.
- jdmichal 4y ago
- kazinator 4y agoYou can use floating-point for money even if you're dealing with (American) billions (10 figures), and care about pennies. With 10 figures in the integer part, you have 5 more digits of precision in the fractional part, so down to the thousandths of a cent. A single addition or multiplication will not accumulate an error which affects the cent, and you can round the calculation to the best approximation of the penny in order to clip off the error.
- hn_throwaway_99 4y agoThat's absolutely, 100% false. Trivial example (I've actually hit an analogous bug in production): User has money in their wallet, and you want to check before they make withdrawals that their balance doesn't go negative. You have some logic in your code that is basically like: if (walletBalance - sumOfWithdrawals < 0) { throw new Error('overdrawn); } Try that code in Javascript where walletBalance = 0.3 and the sumOfWithdrawals = 0.2 + 0.1. Point being there are tons of operations in the financial world where you check things against 0, or want to ensure that a breakdown of smaller transactions equals a larger amount. Those all can fail with floating points but succeed with decimals.