On Wed, 24 Jan 2018 at 18:14 Clément Pit-Claudel <[email protected]>
wrote:

> Hi all,
>
> I'm having trouble using the roundto function.  In particular, I don't
> understand the following results:
>
>     $ ledger eval '(roundto($0.31 / 2, 2))'
>     $0.16
>
>     $ ledger eval '(roundto($0.31 / 2, 2) == $0.16) ? "equal" : "not
> equal"'
>     not equal
>

It looks like this a result of how roundto is implemented [0]. During
rounding the amount is converted from an arbitrary precision rational a
double, rounded and then converted back to a rational. For instance,
roundto($0.2, 1) gives:

Rational before rounding = 1/5
Rounded to 0.200000000000000011102230246251565404236316680908203125
Rational after rounding = 3602879701896397/18014398509481984

Because of the limited precision of doubles the rounded value of some
numbers cannot be represented. These numbers will be converted to a
rational that *looks* correct when displayed but does not equal the true
number.

I suspect (but have not checked) that the $0.00 and -$0.00 totals you are
seeing are very small numbers that are not exactly zero but rounds to zero
when displayed with the given precision.

[0]
https://github.com/ledger/ledger/blob/b7b0280066ab36fd8edf5121826780beac391157/src/amount.cc#L681-L687

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"Ledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to