ba-st / Aconcagua

Measures as first class objects, that is, an object that encapsulates a number with its unit.
MIT License
14 stars 3 forks source link

Money Documentation #23

Open seandenigris opened 3 years ago

seandenigris commented 3 years ago

I'd like to add some information to the docs about modeling money. This has come up many times on the MLs of the various Smalltalk flavors, and Aconcagua has been mentioned many times. However, even after reading and re-reading, I'm still not clear how Aconcagua addresses the float problem. Namely, by way of example:

result := 0.
interval := 10.21.
10000 timesRepeat: [ result := result + interval ].
result = 102100.00000001806 "(not 102100.0)"

At first, I thought that using ScaledDecimal might help, but there were various comments saying that Pharo's ScaledDecimal is ill-conceived and maybe FixedDecimal would be better.

Another solution I thought of would be to convert any decimal money to dollars and cents and only convert back for display. For example:

Float>>dollars
    ^ (AmDollar uniqueInstance * self // 1) +
(self \\ 1 * 100) asInteger cents.

Any feedback or input would be appreciated...

@gcotelli @mtaborda @hernanwilkinson

gcotelli commented 3 years ago

Hi @seandenigris , Aconcagua uses the numeric infrastructure the language provided. For modelling monetary units is usually not a good idea to use floats due to its inexact behavior, unless you're doing some approximation that has no analytical counterpart in which case there's already an error margin considered.

So, in the common case you can use fractions if you need absolute precision or a fixed point kind number for the internal representation. Of course you will need some "formatters" to get a user friendly representation of the number especially when using fractions as the underlying representation. And you will need probably some way to round the monetary measurements to his minimal representation when representing things like payments that are not allowed to have an arbitrary number of decimal places.