1752795 Members
6183 Online
108789 Solutions
New Discussion юеВ

Re: UNIX arithmetic

 
SOLVED
Go to solution
Rahul_13
Advisor

UNIX arithmetic

Hi,

I have a bsic quetion on Unix aritmetic.

Below is the sample script

val1=16019
val2=2
val3=`expr $val1 / $val2`

the result should actually be "8009.5" but I am getting "8009".

Can anyone please tell me what I need to do to get the value "8009.5" as the result.

Thanks
Rahul


9 REPLIES 9
Tim Nelson
Honored Contributor

Re: UNIX arithmetic

all shells are integer math. Cannot do FP.

use "bc" for math.

echo 3.5+2|bc
5.5

or echo $val1/$val2|bc

you get the idea.
Tim Nelson
Honored Contributor

Re: UNIX arithmetic

Pardon the flaw in the exaples. I have used this a number of times for the same issue. Let me find a good example. Will get back to you.


Hold on.
Mel Burslan
Honored Contributor

Re: UNIX arithmetic

As far as I know, only arithmetic in ksh or any standart shell for that matter, is integer arithmetic. You need to devise your floating point arithmetics yourself. One time I have bookmarked this link for the purpose but never tried it. Take a peek at the script found at the following url and see if it helps you.

http://www.shelldorado.com/scripts/cmds/calc
________________________________
UNIX because I majored in cryptology...
Tim Nelson
Honored Contributor
Solution

Re: UNIX arithmetic

Try this

val3=`echo "$val1/$val2"|bs`

echo $val3

Don't ask about the bs, it just works :)

read the man page on bs and you will understand.

Sandman!
Honored Contributor

Re: UNIX arithmetic

Rahul,

Use the command line example suggested by Tim. You'll have to do the same operation in case of further manipulation of the string stored inside of val3 ex. if you need to multiply val3 by 3 to obtain 24028.5 then...

# val4=`echo "$val3*3"|bs`

hope it helps!!!
Sandman!
Honored Contributor

Re: UNIX arithmetic

Make sure that you're not typesetting the variables within your script to integer otherwise integer truncation would chop off the decimal part i.e. remove any line(s) like "typeset -i val1 val2 val3"

regards!
Bill Hassell
Honored Contributor

Re: UNIX arithmetic

Just to clarify, Unix isn't doing the arithmetic, it is your shell. All popular Unix shells are limited to integer arithmetic. AS mentioned, you can use bc to perform your floating point calculations but remember that the shell built-in commands such as eval and let will truncate variables to integers.

Now shell variables can be assigned values such as val9=123.9567 and it will look correct. That is because the shell defaults all variables to strings, not numbers. And when a variable is used in shell arithmetic, the value is reduced to an integer by truncation (not rounding). So the examp,e val9=123.9567 will be used as 123 and not rounded to 124. If you need to make calculations that may result in a fractional part, use the shell variables as string holders and do all the calculations in bc.


Bill Hassell, sysadmin
Stuart Abramson
Trusted Contributor

Re: UNIX arithmetic

ksh is completely incapable of doing floating point arithmetic.

You have to do it with "awk".

Say you want to divide 5.12 by 21.56:

TIME=21.56
echo 5.12 $TIME | awk '{printf "%.2f\n", $1 / $2 }'

yields:

0.24
James R. Ferguson
Acclaimed Contributor

Re: UNIX arithmetic

Hi:

Some modern shells, notably Korn93 and decendents, DO HAVE the ability to perform real arithmetic. You use the '-F' or -'E' typeset options.

The '-E' option specifies scientific notation. An option count (n) signifies the number of significant places. Similarly, '-F' asks for floating point notation with an option n-count signaling the number of decimal places.

On HP-UX, you can find a version as:

/usr/dt/bin/dtksh

By example:

#!/usr/dt/bin/dtksh
typeset -F R1
typeset -F3 R2
typeset -E R3
let R1=1/8
echo $R1
let R2=1/8
echo $R2
let R3=1/8
echo $R3
exit 0

...produces as output:

.1250000000
.125
0.125

Regards!

...JRF...