/bin/sh and BIG NUMBERS

Mikko Työläjärvi mbsd at pacbell.net
Sun Apr 6 15:07:31 PDT 2003


On Sun, 6 Apr 2003, Bill Vermillion wrote:

> On Sun, Apr 06, 2003 at 12:00 ,
> freebsd-hackers-request at freebsd.org exclaimed "Las Cucarachas
> entran, Pero no puede en salir", and then rambled on saying with:
>
> >    2. Re: /bin/sh and BIG NUMBERS (Giorgos Keramidas)
>
>
> > Message: 2
> > Date: Sun, 6 Apr 2003 06:24:50 +0300
> > From: Giorgos Keramidas <keramida at ceid.upatras.gr>
> > Subject: Re: /bin/sh and BIG NUMBERS
> > To: Alex Semenyaka <alexs at ratmir.ru>
> > Cc: freebsd-hackers at freebsd.org
> > Message-ID: <20030406032450.GC4130 at gothmog.gr>
> > Content-Type: text/plain; charset=us-ascii
>
> > On 2003-04-05 07:06, Alex Semenyaka <alexs at ratmir.ru> wrote:
> > > I found that /bin/sh cannot handle numbers those do not fit to integer
> > > type.  That is not too bad. Too bad that it just silently warps them
> > > in arithmetical operations:
>
> > > alexs at snark> /bin/sh -c 'echo $((10000000000-1))'
> > > 2147483646
>
> Not all shells have that problem.  I'm using the real KSH from AT&T
> via the ports.  It's returns 999999999 quite nicely :-)

Ksh seems to use floats for calculations, and so instead silently
drops precision when dealing with sufficiently large numbers:

 atlas% ksh93 -c 'echo $((10000000000002-1))'
 1e+13

Zsh uses 64 bit ints, which silently overflow, albeit at larger values
than FreeBSD's sh(1).  The same goes for bash.

Looks like shells aren't the best environment for bignum arithmetic :^)

     $.02,
     /Mikko


More information about the freebsd-hackers mailing list