lcn2 / calc

C-style arbitrary precision calculator
http://www.isthe.com/chongo/tech/comp/calc/index.html
Other
346 stars 52 forks source link

Bug: calc '-0' fails #101

Closed zmrocze closed 12 months ago

zmrocze commented 1 year ago

Hey guys!

Letting you know you don't parse correctly expressions starting with -, i.e.

$ calc '-0'

fails but

calc '0-0'

succeeds.

I don't blame you, made the same mistake in one of my older projects.

lcn2 commented 12 months ago

Hello @zmrocze,

Thanks for your bug report.

It turns out that the issue is with your command line shell and what it supplies to the calc command. And quoting as in '-0' doesn't help.

When your shell parses the command:

$ calc '-0'

your shell supplies to the calc command, the 2 argv arguments:

argv[0]: calc
argv[1]: -0

The calc command line parser then evaluates the command line, determines there is NO -0 option and throws an error.

The calc(1) man page states:

       --     The double dash indicates to calc that no more option follow.  Thus calc will ignore a later argument on the command
              line even if it starts with a dash.  This is useful when entering negative values on the command line as in:

                   calc -p -- -1 - -7

So had you done the following:

$ calc -- '-0'

or even:

$ calc -- -0

it would have worked.

See the "CALC COMMAND LINE" section of the calc(1) man page for more details.

We hope this helps!