Sample Header Ad - 728x90

How to make bc handle explicitly positive numbers? Or what alternative calculator can be used with such support?

-2 votes
2 answers
203 views
Consider the following:
$ echo "-1" | bc
-1

$ echo "(1)" | bc
1

$ echo "+1" | bc
(standard_in) 1: syntax error
Minus sign and round braces are handled correctly but when explicit plus sign is specified bc falls with error. Is it possible to make it treat explicitly positive numbers just as normal positive numbers? I want to be able to calculate constructions like: - +1++2 (result must be 3) Even simple Ubuntu calculator can do such operations: Attachment with Calculator screen It is strange that bc can't. Seems like a bug. As a workaround sed can be used to preprocess the input for bc, or perhaps bc has some specific config for this? Or may be there is some alternative to bc for that? --- **Update** I've sent request to bc's support team to consider to fix this bug. Comments from [Kusalananda](https://unix.stackexchange.com/users/116858/kusalananda) : > bc is a POSIX utility, and any implementation of the utility > therefore should follow the [POSIX specification](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/bc.html) . This specification > contains a grammar which does not include the unary plus operation. If > GNU implements a unary plus in their implementation of bc, they > would need to ensure that all existing code that uses bc still > performs as before and that any future code that uses bc performs as > a reader of the standard would expect. > > Also note that -1--2 is an error, so you seem to additionally > propose a change in the precedence of the increment and decrement > operators, right? My comment: > I think that bc should be able to perform simple calculations like > simple calculator can do. If support team adds to bc at least > ability to handle cases with digits like -1--2 (with result 1) and > +1++2 (with result 3) (only unary plus operation for digits, and not falling with error while trying to execute increment / decrement > operations on digits which all the same are not supported by now) in my understanding it will > not anyhow impact existing or future POSIX-compliant code.
Asked by Anton Samokat (289 rep)
May 11, 2024, 11:57 AM
Last activity: Jun 17, 2024, 09:48 AM