mmmmm! Yes, using pure integer values for currency is generally the best way to deal with it. The Bitcoin standard is that 1 BTC = 10,000,000 'satoshis'. Many rightly point out that this is a stupidly small precision, but for the time being is ok. I personally would expand this, and just use a 32/32 split and thus a 2^32 width for the fractional precision, which also makes for very easy, fast implementation. I don't really think that this would cause any issues whatsoever, really the differences between currencies would then merely be a matter of presentation, as 32 bits of fractional precision will not be exceeded in any near future situation.
On Friday, 30 November 2018 04:14:23 UTC+1, Bakul Shah wrote: > > FWIW, in some code I am writing, I considered using > fixed point decimal numbers but ended up defining a > *currency* type which is an int64 for value + a separate > unit + currency kind. Even if I use a unit of millicent, this > will allow handling amounts close to $100 Trillion. I > don't expect this limit to be a problem for my personal > finances! Performance is not an issue for my use. I > even store all the financial data in text files! > > Dealing with stuff such as currency conversion, interest > rates, stocks etc. gets a bit complicated due to their own > precision needs but for that one can look at existing > practices to do the right thing (which is, be able to accurately > implement the rules your bank etc use). > > [Aside: > Ideally this would be done using a *generic* currency > type. Something like > > import "currency" > type $ = currency.Type("$") > type £ = currency.Type("£") > > var m1 = $(5) > var m2 = $(10) > var m3 = £(2) > > m1 + m2 // ok > m2 + m3 // compile time error > m1*m2 // compile time error > m1*5 // ok > m1+5 // compile time error > > I doubt go2 will get generics flexible enough for this! > ] > > On Nov 28, 2018, at 10:47 PM, robert engels <ren...@ix.netcom.com > <javascript:>> wrote: > > For those interesting in financial apps, I have released ‘fixed' at > https://github.com/robaho/fixed a high performance fixed-point math > library primarily designed for to work with currencies. > > The benchmarks: (Decimal is the shopspring library, big Int/Float are the > stdlib) > > BenchmarkAddFixed-8 2000000000 0.83 ns/op > 0 B/op 0 allocs/op > BenchmarkAddDecimal-8 3000000 457 ns/op > 400 B/op 10 allocs/op > BenchmarkAddBigInt-8 100000000 19.2 ns/op > 0 B/op 0 allocs/op > BenchmarkAddBigFloat-8 20000000 110 ns/op > 48 B/op 1 allocs/op > BenchmarkMulFixed-8 100000000 12.4 ns/op > 0 B/op 0 allocs/op > BenchmarkMulDecimal-8 20000000 94.2 ns/op > 80 B/op 2 allocs/op > BenchmarkMulBigInt-8 100000000 22.0 ns/op > 0 B/op 0 allocs/op > BenchmarkMulBigFloat-8 30000000 50.0 ns/op > 0 B/op 0 allocs/op > BenchmarkDivFixed-8 100000000 19.3 ns/op > 0 B/op 0 allocs/op > BenchmarkDivDecimal-8 1000000 1152 ns/op > 928 B/op 22 allocs/op > BenchmarkDivBigInt-8 20000000 68.4 ns/op > 48 B/op 1 allocs/op > BenchmarkDivBigFloat-8 10000000 151 ns/op > 64 B/op 2 allocs/op > BenchmarkCmpFixed-8 2000000000 0.28 ns/op > 0 B/op 0 allocs/op > BenchmarkCmpDecimal-8 100000000 10.8 ns/op > 0 B/op 0 allocs/op > BenchmarkCmpBigInt-8 200000000 8.37 ns/op > 0 B/op 0 allocs/op > BenchmarkCmpBigFloat-8 200000000 7.74 ns/op > 0 B/op 0 allocs/op > BenchmarkStringFixed-8 20000000 99.0 ns/op > 16 B/op 1 allocs/op > BenchmarkStringDecimal-8 5000000 326 ns/op > 144 B/op 5 allocs/op > BenchmarkStringBigInt-8 10000000 209 ns/op > 80 B/op 3 allocs/op > BenchmarkStringBigFloat-8 3000000 571 ns/op > 272 B/op 8 allocs/op > > > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts...@googlegroups.com <javascript:>. > For more options, visit https://groups.google.com/d/optout. > > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.