On 10/08/2020 03:52, nugget wrote:
> As a summary, what i think I leared:
>
> * The design of Beancounts D() technically allows allows higher
> precision (float) numbers as input.
Floating points numbers have finite precision. Decimal numbers (as in
Python Decimal objects) have arbitrary precision. Thus this statement is
technically false.
Furthermore, floating point numbers do not represent numerical values in
a decimal (as in "base 10") notation, thus they cannot represent exactly
the decimal numbers used to count currency. For example, a floating
point number cannot represent 1.1 or 2.2 exactly. You can convince
yourself of this trying to sum those two numbers:
>>> 1.1 + 2.2
3.3000000000000003
Note that this particular representation of the result depends on how,
by default, Python displays floating point numbers:
>>> '{:.32f}'.format(1.1 + 2.2)
'3.30000000000000026645352591003757'
That's why financial transactions (among other things) are better
recorded using a numerical representation rooted on a base 10 notation,
as the one offered by Python Decimal numbers.
Of course, if you try to convert a floating point number to a Decimal
number you cannot recover from the rounding error introduced by the
floating point representation. Thus:
>>> Decimal(1.1) + Decimal(2.2)
Decimal('3.300000000000000266453525910')
However,
>>> Decimal('1.1') + Decimal('2.2')
Decimal('3.3')
I think this is nicely explained in the documentation I linked in a
previous mail:
https://docs.python.org/3.8/library/decimal.html. Is
there anything not clear in the documentation?
> * But the design philosophy does not. It requires string typed numbers
> with maximum precision to the second digit. Higher precision is not
> supported and might cause problems.
No! You can use as many decimal digits as you want. However, this this
choice is determined by the currency you use as it defines the resiltuon
at which financial transactions can be recorded. For USD an EUR (the
currencies I use the most) the resolution is cents, thus my examples use
two decimal digits. If there would be no Euro and Italy would still be
using the Italian Lira (ITL) I would be using a resolution of 50 ITL...
> * It is up to the user to to deal with higher precisions. For example,
> treating reminders of divisions. There is no general solution on how
> to treat this.
I don't know what you mean with "dealing with higher precision" as
beancount and Python Decimal use arbitrary precision, thus provide you
tools to work with any precision you desire.
Treating reminder of division has nothing to do with beancount, it is a
property of quantized physical systems: there is no way to split a $0.01
coin (and have it still be valid currency) as there is no way to split a
photon. By extension, this also apply to transactions that do not
physically exchange coins or bills (you cannot write a check for amounts
with resolution higher than $0.01, etc...).
Cheers,
Dan