Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think the biggest challenge is that negative numbers are fundamentally a hack in this context. You don't have negative $100 in your bank account, and you don't pay negative $100 for something. Instead, you are $100 in debt or you receive $100, which is qualitatively different. Such qualitative differences should be represented using the type system.




Overdrafting accounts or negative balance in a bank account has nothing to do with what I was talking about.

A negative number in my comment is just the "from" part of a flow.

Just like in engineering, if you consider a dam, you can talk about the outflow as "-100 L/min" to indicate that the dam looses 100 litres per minute, for instance.

That does not mean that the litres of water anywhere ever becomes negative.

You do not say using negative numbers in engineering is a hack just because some numbers (like volume of water in the dam) is constrained to be positive.

In accounting one instead talks about the dam being debited 100 L and the electric plant being credited 100 L. That is basically just a different way of spelling "minus". It is the same thing just using different words.

And it is of course entirely possible to overdraft an account by debiting too much and get an invalid state entirely without negative numbers.

Negative numbers are actually a "hack" (or useful invention) in engineering and everywhere else too. They don't actually "exist", it is an abstraction similar to imaginary numbers and real numbers. But all of these are useful.


Negative numbers are a hack in the sense that they can be confusing, and it's easy to make mistakes with them. For example, I would interpret "-100 L/min outflow" as an indirect equivalent of "100 L/min inflow". To avoid confusion, you could drop terms "inflow" and "outflow" completely and talk about "-100 L/min (net) flow". Or you could separate the type and the magnitude of the flow.

There's a difference between UI and code. In code, it is more complicated to do

    if (change.direction == OUT) {
        total -= change.value
    } else {
        total += change.value
    }
than to simply use negative numbers and do

    total += change
The same in accounting, it can in my experience often be convenient that your Entry database tables look like

    account          amount
    ---              ---
    income/sales     -100
    receivables      +100
rather than

    account       debit    credit
    ---           ---      ---
    income/sales           100
    receivables   100

The first one is just simpler, and you also get the nice graph properties (cuts, zero-sums, etc) discussed in the OP. Simpler database model, simpler code, less cases to test, easier to do ad hoc analysis queries, etc.

Of course in the UI one can simply flip signs on credit-normal accounts to avoid displaying negative numbers if one wishes.


Negative numbers are probably convenient, if your background is in a field, where numerical calculations are the norm. Traditional accounting is better justified from a mathematical perspective, as mathematics is more about definitions than calculations.

There are usually multiple levels of abstraction in code. When choosing the one to use for a particular task, it's a good idea to prioritize both simplicity and conceptual clarity. Bad things often happen, when the two priorities disagree. And you should try to use the same terminology and same concepts as the user as much as reasonably possible. Otherwise it's easy to make wrong choices by thinking about the problem from a wrong perspective.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: