5 ms·
PS. If you design software that works with money amounts, always use fixed point decimals. Don't use floats, it's just wrong! Eh, doubles are fine for (most)
by CamperBob2 4y ago
PS. If you design software that works with money amounts, always use fixed point decimals. Don't use floats, it's just wrong!
Eh, doubles are fine for (most) currencies. Just don't do comparisons without appropriate epsilons.
People who compoare floating-point numbers for equality are going to make other fundamental mistakes with whatever data type you force them to work with.
- worik 4y agoYes But actually, since fixed point decimals are not all ways available use the smallest unit of account, not smallest legal tender, and integer arithmetic And learn how to round
- adamwk 4y agoSmallest unit also has issues. For instance the Indonesian rupiah technically is made up of 100 sen, but the currency is so inflated nobody uses it and currency libraries behave differently (even different versions of the same library). We had a bug where different OS versions provided different values when normalizing it to a smallest unit integer. If you really don’t have access to a decimal type I think the best solution is to convert it to micro-units (price * 10^6). This is what Android does in its billing library.
- worik 4y agoUse sen. It can be transferred by bank transfer. What people use as cash is a distraction, generally
- radu_floricica 4y agoI thought that until literally two days ago. Turns out that if you sum a bunch of 0.37 (not even huge numbers, just around a few thousand) you end up with differences on the order of 10-20. Both in mysql and in Java. No, this doesn't makes sense to me either - the differences should be a LOT farther than 3 digits. And yet. You should have seen my face when debugging this.
- zeven7 4y agoI'm curious about this. Could you provide an example? Here's what I'm seeing: --- JavaScript (function() { let inc = 0.37; let times = 5000; let total = 0; for (let i = 0; i < 5000; i++) { total += inc; } console.log('expected:', inc \* times); console.log('actual:', total); })(); expected: 1850 actual: 1849.9999999997679 --- Java public class MyClass { public static void main(String args[]) { double inc = 0.37; double times = 5000; double total = 0; for (double i = 0; i < 5000; i++) { total += inc; } System.out.println("expected: " + (inc \* times)); System.out.println("actual: " + total); } } expected: 1850.0 actual: 1849.9999999997679 --- update: Changing `double` to `float` in Java yields: expected: 1850.0 actual: 1849.9778 and maybe that lines up with what you meant by "the differences should be a LOT farther than 3 digits", though it's hard to tell what you mean by "differences on the order of 10-20".
- HNDen21 4y agosame in SQL Server declare @i float =0.37,@times int = 0,@total float = 0 while @times < 5000 begin set @total+= @i set @times +=1 end select @total 1849.99999999977 or select sum(t) from( select top 5000 convert(float, 0.37) t from sysobjects a cross join sysobjects b) z 1849.99999999977
- radu_floricica 4y agosomething like, in mysql: create table t ( value double(10,2) not null ); put a bunch of values, something like 15k with maybe half of them being 0.37. Do the sum two ways: - export them in excel and do a sum - select sum(value) from t and you get two different values, with a difference of something like 15. Replace double(10,2) with decimal(10,2), and the sum(value) works correctly. I still have no idea why this happens. I could expect a tiny difference, and code like your above will indeed give differences like this, well under 1.0, but my particular scenario had differences many orders of magnitude higher. Still can't explain.
- 4y ago
- stevoski 4y agoSomebody is wrong on the Internet today. Very wrong.
- Kon-Peki 4y agoIf I want to know the monthly payment on a $500,000 mortgage for 30 years at 6%, should I use: 1. Decimal math 2. Binary floating point math 3. Domain knowledge trumps all; it makes no difference
- gibspaulding 4y agoThen of course you'll have to work back to compute an APR for that by a crazy iterative formula outlined by the US gov. Which would you use for that process?
- meese712 4y agoirr? The library we use uses doubles https://github.com/eric-malachias/irr https://github.com/eric-malachias/irr. We also use doubles for the amortization table and doubles for everything.
- meese712 4y agoOh hey I work in mortgage! We use plain old doubles for everything and there's some rounds in there and occasionally an exotic round to the nearest 1/8th. Everything ends up matching fine where it needs to.
- wyager 4y agoCurrency quantities are inherently exact. If you're doing epsilon comparisons on currency quantities, you are making a fundamental ontological error.
- CamperBob2 4y agoNobody gives a hoot about 0.000001 cents. Round to the nearest 1/100 cent after adding or subtracting doubles, and you will be fine in 99.99999% of applications. The cardinal sin isn't using doubles for currency; it's using them without understanding either the tool or the job that you're asking the tool to perform.
- wyager 4y agoYou've clearly never worked with accountants.
- CamperBob2 4y ago[Citation needed]
- mths 4y agoYou've clearly never worked with accountants. [1] [1] https://news.ycombinator.com/item?id=34717487 https://news.ycombinator.com/item?id=34717487
- CamperBob2 4y agoI don't know what I expected
- 8n4vidtmkvmk 4y agoI've been using doubles for currency knowing full well that i probably shouldn't. for 8 years now. so far I've only noticed being off by a cent here and there. yep.. just don't care. will cost more than a penny to fix it now.
- goto11 4y agoAccounting and banking does not allow epsilons. You always need to be able to account for every last cent on every account and every transaction. And the precision you need is fixed, so there is no purpose in using floating points. I guess there are some contexts where floats are fine for monetary amounts, for example if you make economic forecasts or simulations. As long as the amounts are not real-world transactions, floats are probably fine.
- CamperBob2 4y agoYou always need to be able to account for every last cent on every account Then that's your epsilon. Or, more reasonably, one or two powers of 10 further down. Absolutely nobody cares about a millionth of a cent. A few people care about a thousandth, though. Most people get antsy if you can't pin a calculation down to the nearest cent. Floats are pretty much never fine for currency, but doubles are usually OK. There's a world of difference between a 24-bit mantissa and a 53-bit mantissa.
- goto11 4y agoThe problem is that some decimal amounts, e.g. 10 cent ($ 0.10) cannot be represented precisely using binary floating point. It doesn't matter how many bits you use since 0.1 have an infinite expansion in binary. This is exactly what the article describes and explains. 10 cent plus 20 cent does not equal 30 cent, when using binary floating point. This is not acceptable in accounting, since at one point the error may accumulate and cause an error at the size of a cent (or more).