Subject: Re: floating point negative zero in awk
To: Alan Barrett <apb@cequrux.com>
From: Jason Thorpe <thorpej@shagadelic.org>
List: tech-userlevel
Date: 08/27/2007 17:10:54
On Aug 27, 2007, at 12:26 PM, Alan Barrett wrote:
> 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?
No objection from me. Does POSIX have anything to say about this?
What does GNU AWK do?
>
>
> --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)) {
-- thorpej