PDA

View Full Version : command-line mathematics



munkyeetr
May 5th, 2007, 06:38 PM
I am curious if there is anything standard in the bash shell or the terminal that allows you to perform simple mathematics from the command line.

I am currently writing some c programs that will allow me to evaluate equations using syntax like:



add 10 12.34
22.34

sub 30.20 16 4
10.20


I am also creating modules for division, multiplication and percent finding.

I guess I have a couple questions:

Is there anything like this already available?
Would there be interest in having this available?

I have already compiled and am running the addition module, but I am pretty new to C programming, so if anyone is interested in looking at the source and giving some input, I can make it available.

Thanx

aanderse
May 5th, 2007, 06:57 PM
I am curious if there is anything standard in the bash shell or the terminal that allows you to perform simple mathematics from the command line.

I am currently writing some c programs that will allow me to evaluate equations using syntax like:



add 10 12.34
22.34

sub 30.20 16 4
10.20


I am also creating modules for division, multiplication and percent finding.

I guess I have a couple questions:

Is there anything like this already available?
Would there be interest in having this available?

I have already compiled and am running the addition module, but I am pretty new to C programming, so if anyone is interested in looking at the source and giving some input, I can make it available.

Thanx

if you're doing this for a learning exercise in c then good stuff, that's a neat idea.
if you actually just want to be able to do simple arithmetic in bash though, the syntax is like so:
echo $(( 5+4 ))

good luck

allyn
May 5th, 2007, 07:12 PM
i use the dc command for quick arithmetic. uses reverse polish notation. there's also a command bc.

munkyeetr
May 5th, 2007, 07:17 PM
Nice. thank for that; echo works well.

I like to screw around, so I'll probably keep writing the c modules, and may even write a wrapper script for echo to do math.

Cheers.

chriscando
May 5th, 2007, 08:47 PM
Yea a little wrapper script would be nice, then you can just put it in your PATH and use 'add' and 'sub' commands like you mentioned above.

something like this (for add or sub)

#!/bin/bash

if [ $# -lt 1 ]; then
echo "usage: $0 <value1> <value2> [<valueN>]"
exit 0
fi

sum=0
for arg in $@
do
sum=$(($sum+$arg))
done

echo $sum

but this wont do decimals.. hmmm

jfinkels
May 5th, 2007, 08:56 PM
The interactive python interpreter does math pretty well.

sdide
May 5th, 2007, 09:35 PM
~$ echo 10 + 12.34 | bc
22.34

Im not sure how to understand
"sub 30.20 16 4
10.20"

does that mean "sub" takes arg1 and then subtracts all futher arguments (eg, arg2, arg3, etc.. )

anyways

~$ echo 30.20 - 16 - 4 | bc
10.20

if you like to introduce:
sum = arg1 + arg2 + arg3 + ... + argN
and
sub = arg1 - (arg2 + arg3 + ... + argN)


then making with your favorite editor (vi of course)

~# vi /usr/local/bin/sum
echo $@ | sed 's/ / + /g' | bc

and

~# vi /usr/local/bin/sub
echo $@ | sed 's/ / - /g' | bc

bapoumba
May 6th, 2007, 12:35 AM
Thread moved to "Programming Talk".

Nikron
May 6th, 2007, 12:39 AM
The interactive python interpreter does math pretty well.

seconded. It's great for some quick calculations

Tomosaur
May 6th, 2007, 12:42 AM
You could use the 'let' command:



let i=5+7 && echo $i

amo-ej1
May 7th, 2007, 07:52 AM
Even octave works from the command line which offers you functionality up to matrix calculations ... and I also second bc. Advantage is that these offer better support (arbitrary precision etc) over a bash echo