From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15331 invoked by alias); 4 Sep 2012 18:33:23 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 30656 Received: (qmail 4514 invoked from network); 4 Sep 2012 18:33:21 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 Received-SPF: neutral (ns1.primenet.com.au: 209.85.215.171 is neither permitted nor denied by SPF record at ntlworld.com) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-proxyuser-ip:date:from:to:subject:message-id:in-reply-to :references:x-mailer:mime-version:content-type :content-transfer-encoding:x-gm-message-state; bh=7SqhF26sHDG2ovYGyyYVXL9f60pEH1vke3w7YK5QNmo=; b=X9+O8jROrmXDvKCcqrYY+b3kQjpYqSdX2YH3S7+Yt/pJN9CvRb3ajBDa/tq0ufYeGg exqE661fKu/1yA5seMxxWTR4RU906tmqDMVdGC4wesF4lMzygvm4vzqL3rvXNiU+7IV1 LMnTxUXV2e5NbT+ahX9aDa1yx0q07Zhd390idiFMhd6u/b4pE5tmhDQuH2G0Z+4twzA0 Hph+DR3J070shiqQG2/oYxThDlkbzGB0xmGeHaGM/R02e0+mFsUhkusecpSIrv+niJY/ 4CKLlC9JTpfuqo1Zrc1CGakS3bL0WvJYiFe86iIlyJyzP+9M5W6Yxd87Xkhkd398pd/f QtTw== X-ProxyUser-IP: 86.6.16.18 Date: Tue, 4 Sep 2012 19:26:44 +0100 From: Peter Stephenson To: zsh-workers@zsh.org Subject: Re: PATCH: prevent SIGFPE on systems where LONG_MIN < -LONG_MAX Message-ID: <20120904192644.661329c3@pws-pc.ntlworld.com> In-Reply-To: <20120903162602.GP19561@xvii.vinc17.org> References: <20120903155732.6e0ed125@pwslap01u.europe.root.pri> <20120903162602.GP19561@xvii.vinc17.org> X-Mailer: Claws Mail 3.8.0 (GTK+ 2.24.7; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQnmoGaWHtmbnVGojm5TQI+FE1Rzn6Y1QwZDdFyeHEz2j/ax3oI/tClmvBu4iizt1XhnvsDs On Mon, 3 Sep 2012 18:26:02 +0200 Vincent Lefevre wrote: > On 2012-09-03 17:08:18 +0200, J=C3=A9r=C3=A9mie Roquet wrote: > > I think we want a warning, as we have for division by zero. >=20 > I don't think so. The division by zero is mathematically undefined, > so that a warning is fine. But a division by -1 could be replaced by > a negate operation; then you have the usual modular arithmetic rules. > They already occur for >=20 > $ echo $[ 2**63 ] > -9223372036854775808 >=20 > anyway. Right, so we could just do this as a special case as below that gives answers that are plausible as far as fixed precision integer arithmetic goes even if they're not mathematically accurate, which they can't be for the division (they can for the mod). The result should now be sane everywhere. =20 > > The shell should not crash, neither should it return incorrect > > results. >=20 > Agreed. Well, that depends what you mean by an incorrect result. Mathematically incorrect results are inevitable if you don't account of the precision, but we don't warn for any other rounding error so I agree there's no reason to here. Index: Src/math.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D RCS file: /cvsroot/zsh/zsh/Src/math.c,v retrieving revision 1.41 diff -p -u -r1.41 math.c --- Src/math.c 19 Jun 2011 16:26:11 -0000 1.41 +++ Src/math.c 4 Sep 2012 18:23:36 -0000 @@ -1053,14 +1053,34 @@ op(int what) return; if (c.type =3D=3D MN_FLOAT) c.u.d =3D a.u.d / b.u.d; - else - c.u.l =3D a.u.l / b.u.l; + else { + /* + * Avoid exception when dividing the smallest + * negative integer by -1. Always treat it the + * same as multiplication. This still doesn't give + * numerically the right answer in two's complement, + * but treating both these in the same way seems + * reasonable. + */ + if (b.u.l =3D=3D -1) + c.u.l =3D - a.u.l; + else + c.u.l =3D a.u.l / b.u.l; + } break; case MOD: case MODEQ: if (!notzero(b)) return; - c.u.l =3D a.u.l % b.u.l; + /* + * Avoid exception as above. + * Any integer mod -1 is the same as any integer mod 1 + * i.e. zero. + */ + if (b.u.l =3D=3D -1) + c.u.l =3D 0; + else + c.u.l =3D a.u.l % b.u.l; break; case PLUS: case PLUSEQ: --=20 Peter Stephenson Web page now at http://homepage.ntlworld.com/p.w.stephenson/