[comp.unix.shell] Math routines

glex@uh.msc.umn.edu (Jeffrey Gleixner) (09/02/90)

In article <75@iconsys>, mday@iconsys (Matt Day) writes:
> In article <SCOTT.90Aug30124930@tab00.larc.nasa.gov>
scott@tab00.larc.nasa.gov (Scott Yelich) writes:
> >As a side question, does ANYONE have any bourne shell routines which do
> >math... reasonably effeciently?  (For numbers > 1000?)
> 
> For the Bourne shell, the most efficient way to do math is to use the "expr"
> ...  If you have a shell script that needs to do
> a lot of math, I suggest you use the C shell built in math operators; it's

If you're going to do a lot of math write a {your favorite language besides
*sh here} program to do the math for you.  Even if you're doing a small 
amount of math it will really speed it up.
i.e.
#!/bin/sh
count 1 6 | while read value ; do
        some commands...
done

=========
/*
 *      Count.c
 *
 *      print succesive numbers from start to end.
 *      Used in scripts instead of `expr $i +1`
 */
#include <stdio.h>
main(argc,argv)
int argc; char *argv[];
{
register int i,j;
i=atoi(argv[1]);
j=atoi(argv[2]);
while (i <= j)
        printf(" %d",i++);
}
--
glex@msc.umn.edu  ===  " I don't go out with girls that say bitch'n " ===

scott@tab00.larc.nasa.gov (Scott Yelich) (09/03/90)

>#include <stdio.h>
>int argc; char *argv[];
>while (i <= j)
> printf(" %d",i++);

Hmmm.. that's the STRANGEST sh program I have ever seen...
It must be that CSH that I keep hearing about!  :-)

This next routine isn't supposed to be sophisticated... but it works--
just give it two SMALL NUMBERS. 

ADDITION ()
 {
  NUM1="$1"
  NUM2="$2"
  shift $#
  while test $# -lt $NUM1
  do
    set - x $@
  done
  NUM1="$@"
  shift $#
  while test $# -lt $NUM2
  do
    set - x $@
  done
  NUM2="$@"
  set - $NUM1 $NUM2
  TOTAL=$#
 }

I am working on a sort of LIBRARY of SHroutines where all I have to do is
include the clode  ``. COUNT'' for the next... and use the code.

COUNT ()
 {
  test $# = 0 &&\
   {
    echo "$0: Usage: $0 value [...]"
    exit 1
   } ||\
    {
     for i
      {
       set ""
       while :
       do
         set $* " x"
         echo $#
         if test $# = $i; then
           break
         fi
       done
     }
    }
 }

Of course, ``COUNT x'' is a great counter!  :-)

The main problem with the two loops is the ``set " $*"'' command.
This is what takes so much time when the argument list grows.

--
Signature follows. [Skip now]

 -----------------------------------------------------------------------------
 Scott D. Yelich                         scott@[xanth.]cs.odu.edu [128.82.8.1]
 After he pushed me off the cliff, he asked me, as I fell, ``Why'd you jump?''
 Administrator of:    Game-Design requests to <game-design-request@cs.odu.edu>
 ODU/UNIX/BSD/X/C/ROOT/XANTH/CS/VSVN/
 -----------------------------------------------------------------------------

stewart@cbnewsl.att.com (richard.a.stewart) (09/04/90)

In article <2494@uc.msc.umn.edu>, glex@uh.msc.umn.edu (Jeffrey Gleixner) writes:
> > In article <SCOTT.90Aug30124930@tab00.larc.nasa.gov> writes:
> > >As a side question, does ANYONE have any bourne shell routines which do
> > >math... reasonably effeciently?  (For numbers > 1000?)

> If you're going to do a lot of math write a {your favorite language besides
> *sh here} program to do the math for you.  Even if you're doing a small 
> amount of math it will really speed it up.

Generally concur except for prior suggestion re "expr".
-----
Not my favorite language(:-) but one might also consider
a (Bourne) shell counting function using dc:

	cnt() { echo "[p1+d${2}!<0]s0${1}l0xq"|dc; }

   This counts at a rate of about 100/second (on a 3B2) which may be adequate
   for many shell programming purposes although MUCH slower than "count".
   The function saves 99.6+% in "diskspace" over a private a.out from count.c;
   "count" of course might well be shared by all users as /usr/lbin/count.
-----
   Other possibilities for consideration could include bc, awk, awkcc,
   various public domain calculators and "Hoc" (SEE Appendices 2&3 of
   "The UNIX Programming Environment" by Kernighan & Pike)