From owner-freebsd-hackers@FreeBSD.ORG Sun Apr 6 19:49:23 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 9180637B401 for ; Sun, 6 Apr 2003 19:49:23 -0700 (PDT) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id E37FB43FAF for ; Sun, 6 Apr 2003 19:49:22 -0700 (PDT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.12.9/8.12.9) id h372nJhM021836; Sun, 6 Apr 2003 21:49:19 -0500 (CDT) (envelope-from dan) Date: Sun, 6 Apr 2003 21:49:19 -0500 From: Dan Nelson To: Mikko =?utf-8?B?VHnDtmzDpGrDpHJ2aQ==?= Message-ID: <20030407024918.GI28391@dan.emsphone.com> References: <20030406190054.AA08537B404@hub.freebsd.org> <20030406213453.GB4780@wjv.com> <20030406145748.O1122@atlas.home> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030406145748.O1122@atlas.home> X-OS: FreeBSD 5.0-CURRENT X-message-flag: Outlook Error User-Agent: Mutt/1.5.4i cc: freebsd-hackers@freebsd.org Subject: Re: /bin/sh and BIG NUMBERS X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Apr 2003 02:49:23 -0000 In the last episode (Apr 06), Mikko Ty?l?j?rvi said: > On Sun, 6 Apr 2003, Bill Vermillion wrote: > > On Sun, Apr 06, 2003 at 12:00 , > > > On 2003-04-05 07:06, Alex Semenyaka 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@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 :^) You could always rewrite libgmp as a set of shell functions :) POSIX requirements for math in shells are pretty lenient: * Only signed long integer arithmetic is required. As an extension, the shell may recognize arithmetic expressions beyond those listed. The shell may use a signed integer type with a rank larger than the rank of signed long. The shell may use a real-floating type instead of signed long as long as it does not affect the results in cases where there is no overflow. -- Dan Nelson dnelson@allantgroup.com