CMD | Calculator

|^^|

BC Command

The command shows how to used subtraction and showing the result answer.

[rex@ssi ~]$ echo 2201.000-1600.000 | bc -l 601.000 [rex@ssi ~]$

dc is a very archaic tool and somewhat older than bc

This is is comparison between BC and DC commands.

[rex@ssi ~]$ echo '3 4 * p' | dc 12 [rex@ssi ~]$ echo '3 * 4' | bc 12

It is one of the oldest Unix utilities, predating even the invention of the C programming language; like other utilities of that vintage, it has a powerful set of features but an extremely terse syntax.

The syntax is a reverse polish notation, which basically means that the arguments (ie numbers) come first followed by the operator. A basic example of the dc usage is:

echo '3 4 * p' | dc

Where the p is required to print the result of the calculation. bc on the other hand uses the more familiar infix notation and thus is more intuitive to use. Here is an example of bc usage:

echo '3 * 4' | bc

Which one to use?

bc is standardised by POSIX and so is probably the more portable of the two (at least on modern systems). If you are doing manual calculator work then it is definitely the choice (unless you are somewhat of a masochist). dc can still have its uses though, here is a case where the reverse polish notation comes in handy. Imagine you have a program which outputs a stream of numbers that you want to total up, eg:

23 7 90 74 29

To do this with dc is very simple (at least with modern implementations where each operator can take more than two numbers) since you only have to append a +p to the stream, eg:

{ gen_nums; echo +p } | dc

But with bc it is more complex since we not only need to put a + between each number and make sure everything is on the same line, but also make sure there is a newline at the end:

{ gen_nums | sed '$ !s/$/+/' | tr -d '\n'; echo; } | bc

source: http://unix.stackexchange.com/questions/124518/how-is-bc-different-from-dc

A basic difference between the two is that dc uses the reverse Polish notation. It requires explicit commands even in order to produce an output.

You might add two integers in bc by saying:

bc <<< "2+4"

and it would produce 6 on a line by itself. However, in dc you'd need to say:

dc <<< "2 4 +p"

SHOWING HELLO WORLD USING DC COMMAND.

source: http://codegolf.stackexchange.com/questions/22533/weirdest-obfuscated-hello-world/22619#22619

In the weblink above: it shows also some of the languge to create an output "Hello World"

[rex@ssi ~]$ dc<<<"8 9*P101P108P108P111P4 8*P81 6+P111P114P108P100P33P" Hello World![rex@ssi ~]$

eof