-
Notifications
You must be signed in to change notification settings - Fork 492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error: Balance assertion off by $-0.00 #2329
Comments
This is a precision problem. I read somewhere — maybe in the docs — that ledger defaults to 6 places of precision. Stepping through this example, the diff is below.
Lines 1733 to 1741 in d4e3c6b
|
My expectation from user point of view is that
Thanks, however my another expectation is that a tool should provide enough information to figure out what happens without running a debugger. I have ledger installed using
Reporting diff as zero is not helpful here. As a workaround, I have found a way to correct rounding errors by explicit positngs. A file worked with earlier versions required enough edits and care concerning virtual and non-virtual facets of accounts.
|
[message deleted. I think I misunderstood the problem. I don't know what's going on here] |
This is expected value for 700.67. Calling My naive attempts to reproduce the issue in python have failed from gmpy2 import mpfr
mpfr(math.ceil(float(mpfr(12.34)*mpfr(56.78))*100.-0.49999999)/pow(10., 2)) - mpfr(700.67)
# mpfr('0.0')
from gmpy2 import mpq
mpq(math.ceil(float(mpq(12.34)*mpq(56.78))*100.-0.49999999)/pow(10., 2)) - mpq(700.67)
# mpq(0,1) I have tried the Even shorter example causing the trouble:
However the following one assertion is passed
|
|
Adding @the-solipsist who had good input on #1983 |
I looked into the output of
And yes, it's something with the underlying library handling the rational number. |
@igbanam, I suspect it is not |
I have figured out what happens. Minimal example:
Current >>> from gmpy2 import mpq
>>> import math
>>> qs = mpq("700.67"); qs
mpq(70067,100)
>>> qr = mpq(700.67); qr
mpq(6163158497870479,8796093022208)
>>> math.frexp(qr.denominator)
(0.5, 44)
>>> 2**43
8796093022208
>>> float(qs) - float(qr)
0.0
>>> float(qs - qr)
4.092726157978177e-14 I think, something close to
A couple of related questions:
I still believe that reporting discrepancy of 0 is not helpful, actual number should appear even if it is not consistent with current format. |
Multiprecision rational created from a double value may have large power of 2 denominator since fractional decimal numbers can not be represented as binary floating point numbers. It leads to failed assertion when result is compared to a value converted directly from strings. Use integer multiprecision arithmetics to round numbers to ensure proper denominator. Inspired by python gmpy2 package <https://github.com/aleaxit/gmpy/blob/3e4564ae9d/src/gmpy2_mpq_misc.c#L315> See ledger#2329
It seems #2361 fixes primary cause of the issue. I can not say that I am confident with the code since it is created using monkey typing approach. Other questions like updating precision or reporting discrepancy as zero remains. |
Multiprecision rational created from a double value may have large power of 2 denominator since fractional decimal numbers can not be represented as binary floating point numbers. It leads to failed assertion when result is compared to a value converted directly from strings. Use integer multiprecision arithmetics to round numbers to ensure proper denominator. Inspired by python gmpy2 package <https://github.com/aleaxit/gmpy/blob/3e4564ae9d/src/gmpy2_mpq_misc.c#L315> The change makes `roundto` symmetric for positive/negative arguments. - See ledger#2329 - Closes ledger#1983
Multiprecision rational created from a double value may have large power of 2 denominator since fractional decimal numbers can not be represented as binary floating point numbers. It leads to failed assertion when result is compared to a value converted directly from strings. Use integer multiprecision arithmetics to round numbers to ensure proper denominator. Inspired by python gmpy2 package <https://github.com/aleaxit/gmpy/blob/3e4564ae9d/src/gmpy2_mpq_misc.c#L315> The change makes `roundto` symmetric for positive/negative arguments. Halves are rounded to nearest even. Rounded away from zero are discussed in ledger#1663 and it may be achieved with minimal modification. - See ledger#2329 - Closes ledger#1983
I have puzzled by the following error message having just zeroes in discrepancy amount and I have no idea how to debug the issue:
the test file is
The idea is to check that billing is correct: per unit price, volume and total amount are in agreement.
An attempt to specify excessive precision
failed with a similar message with more zeroes
Earlier (ledger-3.1.3) I used a bit annoying trick with no
roundto
call and an extra posting to correct rounding and an automatic transaction adding a virtual posting to cancel rounding correction. Currently another trick is required since virtual and regular accounts have diverged and assertions work in different way (#1959).How to find the real discrepancy hidden behind zeroes?
The text was updated successfully, but these errors were encountered: