divide by zero processing transaction

35 views
Skip to first unread message

Tavis Ormandy

unread,
Mar 2, 2023, 11:25:16 PM3/2/23
to ledge...@googlegroups.com
Hello! I've managed to break my ledger file again :(

$ ledger bal
Error: Divide by zero

I minimized it down to this transaction (numbers changed for privacy):

commodity FOO
format 1,000.00 FOO

2022/12/31 * Opening Balances
Assets:Brokerage 1.208 FOO @ $8.87
Equity:Opening Balances

2023/01/03 * Transaction
Assets:Brokerage -1.211 FOO @ $16.79
Assets:Brokerage -0.003 FOO @ $16.79 ; Fractional shares liquidated
Assets:Checking $20.38
Income:Capital Gains

$ ledger --args-only -f zero.ldg bal
While parsing file "zero.ldg", line 12:
Error: Divide by zero
$ ledger --version
Ledger 3.3.0-20230208, the command-line accounting tool

I always seem to hit these edge cases :)

Thanks, Tavis.

--
_o) $ lynx lock.cmpxchg8b.com
/\\ _o) _o) $ finger tav...@sdf.org
_\_V _( ) _( ) @taviso

Ismael Bouya

unread,
Mar 3, 2023, 8:14:14 AM3/3/23
to ledge...@googlegroups.com
Hello!

(Fri, Mar 03, 2023 at 04:25:06AM -0000) Tavis Ormandy :
> Hello! I've managed to break my ledger file again :(
>
> $ ledger bal
> Error: Divide by zero
>
> I minimized it down to this transaction (numbers changed for privacy):
>
> commodity FOO
> format 1,000.00 FOO
>
> 2022/12/31 * Opening Balances
> Assets:Brokerage 1.208 FOO @ $8.87
> Equity:Opening Balances
>
> 2023/01/03 * Transaction
> Assets:Brokerage -1.211 FOO @ $16.79
> Assets:Brokerage -0.003 FOO @ $16.79 ; Fractional shares liquidated
> Assets:Checking $20.38
> Income:Capital Gains

You said that commodity FOO had a precision of 2 decimal digits, but in
your entry you have 3, and the second one evaluates to 0 due to that
precision.

If you replace your format with
commodity FOO
format 1,000.000 FOO
it starts working fine again.

Although formally correct with that change, on the spirit you prbably
wanted that instead in your second transaction:

2023/01/03 * Transaction
Assets:Brokerage -1.211 FOO {$8.87} @ $16.79
Assets:Brokerage -0.003 FOO {$8.87} @ $16.79 ; Fractional shares liquidated
Assets:Checking $20.38
Income:Capital Gains

(check with the ledger bal command to see the difference)
--
Ismael

Ismael Bouya

unread,
Mar 3, 2023, 8:36:20 PM3/3/23
to ledge...@googlegroups.com
Hello!

(Fri, Mar 03, 2023 at 04:25:06AM -0000) Tavis Ormandy :
> Hello! I've managed to break my ledger file again :(
>
> $ ledger bal
> Error: Divide by zero
>
> I minimized it down to this transaction (numbers changed for privacy):
>
> commodity FOO
> format 1,000.00 FOO
>
> 2022/12/31 * Opening Balances
> Assets:Brokerage 1.208 FOO @ $8.87
> Equity:Opening Balances
>
> 2023/01/03 * Transaction
> Assets:Brokerage -1.211 FOO @ $16.79
> Assets:Brokerage -0.003 FOO @ $16.79 ; Fractional shares liquidated
> Assets:Checking $20.38
> Income:Capital Gains

You said that commodity FOO had a precision of 2 decimal digits, but in
your entry you have 3, and the second one evaluates to 0 due to that
precision.

If you replace your format with
commodity FOO
format 1,000.000 FOO
it starts working fine again.

Although formally correct with that change, on the spirit you prbably
wanted that instead in your second transaction:

2023/01/03 * Transaction
Assets:Brokerage -1.211 FOO {$8.87} @ $16.79
Assets:Brokerage -0.003 FOO {$8.87} @ $16.79 ; Fractional shares liquidated
Assets:Checking $20.38
Income:Capital Gains

signature.asc

Tavis Ormandy

unread,
Mar 4, 2023, 10:42:48 AM3/4/23
to ledge...@googlegroups.com
On 2023-03-03, Ismael Bouya wrote:
> Hello!
>
> (Fri, Mar 03, 2023 at 04:25:06AM -0000) Tavis Ormandy :
>> Hello! I've managed to break my ledger file again :(
>>
>> $ ledger bal
>> Error: Divide by zero
>>
> You said that commodity FOO had a precision of 2 decimal digits, but in
> your entry you have 3, and the second one evaluates to 0 due to that
> precision.
>

Yes, that is how I want it formatted - but I want it tracked
internally at higher precision. I think that is a valid thing to want, right?

> Although formally correct with that change, on the spirit you prbably
> wanted that instead in your second transaction:
>
> 2023/01/03 * Transaction
> Assets:Brokerage -1.211 FOO {$8.87} @ $16.79
> Assets:Brokerage -0.003 FOO {$8.87} @ $16.79 ; Fractional shares liquidated
> Assets:Checking $20.38
> Income:Capital Gains
>
> (check with the ledger bal command to see the difference)

Oops, yes, you're right - it was just a fictional transaction to demonstrate the issue :)

Martin Michlmayr

unread,
Mar 4, 2023, 7:58:44 PM3/4/23
to ledge...@googlegroups.com
* Tavis Ormandy <tav...@gmail.com> [2023-03-04 15:42]:
> Yes, that is how I want it formatted - but I want it tracked
> internally at higher precision. I think that is a valid thing to want, right?

Yeah, I thought "format" was for *display* precision only, so it
doesn't make sense that this cuts off precision. Am I
misunderstanding "format?"

--
Martin Michlmayr
https://www.cyrius.com/

John Wiegley

unread,
Mar 23, 2023, 2:30:40 PM3/23/23
to Martin Michlmayr, ledge...@googlegroups.com
>>>>> "MM" == Martin Michlmayr <t...@cyrius.com> writes:

MM> Yeah, I thought "format" was for *display* precision only, so it doesn't
MM> make sense that this cuts off precision. Am I misunderstanding "format?"

This has been fixed in https://github.com/ledger/ledger/pull/2224. Good find!

John
Reply all
Reply to author
Forward
0 new messages