Subject: Re: 64 bit shell arithmetic
To: Markus Mayer <mmayer@redback.com>
From: David Laight <david@l8s.co.uk>
List: tech-userlevel
Date: 03/24/2007 08:02:44
On Fri, Mar 23, 2007 at 05:17:37PM -0700, Markus Mayer wrote:
> Hi,
> 
> It looks like I discovered a problem with shell arithmetic with large
> number (numbers larger than "signed long").
> 
> Here is an example (determine the amount of RAM and convert it into
> megabytes):
> 
> # total_ram=`/sbin/sysctl hw.usermem64 | sed -e 's/.* = //'`
> # echo $total_ram
> 8388505600
> # ram_mb=$(($total_ram/1048576))
> # echo $ram_mb
> -192
> 
> -192 MB is obviously wrong.

The shell arithemtic is done with 32bit signed integers.
So just isn't going to work with arbitrarily large values.

It does look as thought the shell is using 'signed int', whereas the
posix spec (at least the TOG pages) say 'signed long' (at least).
That change can be made - and only affects 64bit ports.

	David

-- 
David Laight: david@l8s.co.uk