1833695 Members
3720 Online
110062 Solutions
New Discussion

Division Operation

 
SOLVED
Go to solution
Gilbert Standen_1
Frequent Advisor

Division Operation

how do i set precision of a result ? At command line:
#let m=7
#let n=2
#let r=$m/$n
#echo $r
3

Only want the integer portion of calc so this is result needed.

How to guarantee that when run code on an arbitrary UNIX box it's going to return same precision ? How to incorporate precision of result into the code ?


If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
7 REPLIES 7
RAC_1
Honored Contributor
Solution

Re: Division Operation

Shell arithmatic oprations are not precise and has limitations.

You can do "echo "4/3"|bc"

Anil
There is no substitute to HARDWORK
Rajeev Tyagi
Valued Contributor

Re: Division Operation

Gilbert,

One of the method you can use is.

touch /tmp/anyfile
export m=7
export n=2
cat /tmp/anyfile | awk '{print '$m'/'$n'}'


A. Clay Stephenson
Acclaimed Contributor

Re: Division Operation

You don't need to concern yourself at all because all shell arithmatic is integer (there is one shell that can do floating-point math but you must be explicit in your shell commands) and thus all will truncate (not round) the result.

It's generally considered better form to type your variables so that

typeset -i m=7
is better than
let m=7
If it ain't broke, I can fix that.
Gilbert Standen_1
Frequent Advisor

Re: Division Operation

Got it. Much appreciated!
echo "scale=0; 10/3" | bc
3
echo "scale=2; 10/3" | bc
If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
A. Clay Stephenson
Acclaimed Contributor

Re: Division Operation

You really don't have it. There is no reason to call the external utility bc (or awk, Perl) because the shell can only do integer math. It's already done it for you. Your original approach is perfectly portable. The times that external programs like bc are called are those in which non-integer math are needed.
If it ain't broke, I can fix that.
Gilbert Standen_1
Frequent Advisor

Re: Division Operation

Had replied before seeing your post, see now that integer math is the default, which is good for me.

Regarding your pointer on the uses of "let" vs. "typeset" what are the advantages of "typeset" :-)

here's a related question:

typeset -i p=50
typeset -i q=$p
typeset -i r=1

Have made the change you suggested for my uses of "let" but when "read" is used, what are the implications ? Is there a way to sort of use "typeset" when using "read" at the end of a string of piped ops ?

ls | wc -l | sed 's/^[ \t]*//;s/[ \t]*$//' | read histfilect






If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
A. Clay Stephenson
Acclaimed Contributor

Re: Division Operation

Yes, simply put the typeset in another statement.

typeset -i histfileect=0
ls | wc -l | sed 's/^[ \t]*//;s/[ \t]*$//' | read histfilect

The typeset applies to all following references of the variable.
If it ain't broke, I can fix that.