> Solution: Round after every operation No, the solution is not to use floating point numbers to store money. I worked on an iOS app in fintech for years, and let me assure you, using floating point numbers to calculate currency is an exercise in frustration and lack of correctness. When balances are wrong you're losing your customers money, which in turn loses trust in your product. You know it's inexact, an approxi…
How do the balances get incorrect with 2^53 cents of precisely-roundable range?
Using floating-point numbers for money
121–128 of 128 posts
Re: Using floating-point numbers for money
#122Re: Using floating-point numbers for money
#123Large financial calculations where errors accumulate are more often than not done in database queries, not in the application software. What is faster in Postgresql? Double floats and TRUNC after each operation or using NUMERIC(precision, scale)?
Re: Using floating-point numbers for money
#124Earlier quoted context omitted.
Could you help me understand where I’ve gone wrong here? I think there’s some miscommunication going on.
Are you familiar with binary coded decimal? Decimal format floats are in somewhat the same vein. See, for instance, https://en.m.wikipedia.org/wiki/Decimal32_floating-point_for...
Re: Using floating-point numbers for money
#125Earlier quoted context omitted.
Are you familiar with binary coded decimal? Decimal format floats are in somewhat the same vein. See, for instance, https://en.m.wikipedia.org/wiki/Decimal32_floating-point_for...
This is basically what I meant when I said "computers use binary"
Re: Using floating-point numbers for money
#126Earlier quoted context omitted.
This is basically what I meant when I said "computers use binary"
That sense is trivially true, but it doesn't follow that you have binary fractions.
> it doesn't follow that you have binary fractions
Is that in reference to something I said earlier, or are we just falling deeper down a rabbit hole of hair splitting?
The point I was driving at is that a given number we think about in decimal like 12.3456 can be represented in binary in more than one way, one of which are rationals backed by integers for num/den, another is floating point with mantissa and exponent. Which way is chosen ultimately affects the outcomes of computations. I was trying to explain why OP was seeing unexpected results. I don't think I've been mistaken in the explanations...
Re: Using floating-point numbers for money
#127Earlier quoted context omitted.
That sense is trivially true, but it doesn't follow that you have binary fractions.
Sorry, but I'm not parsing this sentence: > it doesn't follow that you have binary fractions Is that in reference to something I said earlier, or are we just falling deeper down a rabbit hole of hair splitting? The point I was driving at is that a given number we think about in decimal like 12.3456 can be represented in binary in more than one way, one of which are rationals backed by integers for num/den, another is…
So it's not "since computers use binary", but "since we use binary floating point". There are fair arguments that we made that choice because of efficiency concerns driven by the fact that "computers use binary", but I don't think that really helps the explanation.
Note that the original objection was picking at a nit that I am not sure I would have picked at, I am just seeking to clarify.
Re: Using floating-point numbers for money
#128No, do not do financial calculations in binary floating point. Converting between base 2 and base 10 fractions can do funny things (including during the rounding step, which is also 99% guaranteed to be inaccurate because base 10 fractions can't be exactly represented as base 2 fractions). Most professional financial packages use fixed point decimal, which can easily be implemented by specifying a fractional unit (su…
A notable exception to the general lack of floating decimal support is C#, which has had floating decimal (System.Decimal) support in the CLR from the start (128 bits with a scale of 0 to 28). https://docs.microsoft.com/en-us/dotnet/csharp/language-refe...
[0]: https://developer.apple.com/documentation/foundation/nsdecim...
[1]: https://developer.apple.com/documentation/foundation/nsdecim...