Subject: Re: floating point negative zero in awk
To: None <tech-userlevel@NetBSD.org>
From: Alan Barrett <apb@cequrux.com>
List: tech-userlevel
Date: 08/27/2007 21:26:54
Thanks to all who answered my earlier questions about negative zero.
Of the tools supplied with NetBSD that are capable of performing
arbitrary arithmetic, awk seems to be the only one that prints
negative zero as the result from simple calculations:
$ echo $(( -1 * 0 ))
0
$ expr -1 \* 0
0
$ echo '-1.0 * 0.0' | bc
0
$ echo '-1.0 0.0 * p' | dc
0
$ echo | awk '{ print -1 * 0 }'
-0
Would it be OK if I commit the following patch, which ensures that
arithmetic in awk will never yield a negative zero?
--apb (Alan Barrett)
Index: src/dist/nawk/tran.c
--- tran.c 26 Jul 2006 20:46:37 -0000 1.9
+++ tran.c 27 Aug 2007 19:12:48 -0000
@@ -285,6 +285,7 @@ Awkfloat setfval(Cell *vp, Awkfloat f) /
{
int fldno;
+ f += 0.0; /* normalise negative zero to positive zero */
if ((vp->tval & (NUM | STR)) == 0)
funnyvar(vp, "assign to");
if (isfld(vp)) {