help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Make division by zero legal


From: Neil R. Ormos
Subject: Re: Make division by zero legal
Date: Tue, 3 Aug 2021 23:22:30 -0500 (CDT)

Andrew J. Schorr wrote:
> Carl Friedberg wrote:

>> (I am a lurker on the gawk-bug list; and this
>> is my first posting on help-gawk)

>> I have been following this discussion, and I
>> agree with the view of Neil Ormos. There are
>> ways to deal with divide by 0 if you don't want
>> the default behavior; please don't change
>> something that is so embedded in GAWK.

>> My 2 cents, in case anyone is counting

> The -M behavior will be changed to match the
> regular behavior -- it will throw a fatal error
> on division by zero.  Do you object to that
> change? It seems undesirable to have
> inconsistent behavior with -M.

If you do conform the -M behavior for attempts to
divide by zero, you might also consider conforming
the behavior when sqrt() and log() are called on a
negative argument.  Without -M, gawk prints a
warning.  With -M, no warning is printed.  (Also,
the sign of the NaN result changes; I don't know
if the sign is meaningful.)  I haven't tried the
trig functions.


(I don't know why divide-by-zero provokes a fatal
error but sqrt(-2) merits only a warning, but that
seems to have been the rule for a long time.)

                           sqrt(-2)    sqrt(-2)    1/0        1/0
Awk Version                Result      Warning     Result     Error
========================   ==========  ==========  =========  =========
("original") awk   [1]     1           no          <none>     fatal
BusyBox Awk        [2]     nan         no          <none>     fatal
mawk               [3]     -nan        no          inf        no
Gawk 3.1.7         [4]     -nan        warning     <none>     fatal
Gawk 4.1.4         [5]     -nan        warning     <none>     fatal
Gawk 4.1.4 with -M [6]     nan         no          inf        no
Gawk 5.1.0         [7]     -nan        warning     <none>     fatal
Gawk 5.1.0 with -M [8]     +nan        no          +inf       no
========================   ==========  ==========  =========  =========


[1] ("original") awk version 20121220
    original-awk: division by zero  source line number 1

[2] BusyBox v1.20.2 (2015-02-06 20:45:10 EST) multi-call binary.
    awk: cmd. line:1: Division by zero

[3] mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

[4] GNU Awk 3.1.7
    gawk: warning: sqrt: called with negative argument -2
    gawk: fatal: division by zero attempted

[5] GNU Awk 4.1.4, API: 1.1 (GNU MPFR 3.1.5, GNU MP 6.1.2)
    gawk: cmd. line:1: warning: sqrt: called with negative argument -2
    gawk: cmd. line:1: fatal: division by zero attempted

[6] GNU Awk 4.1.4, API: 1.1 (GNU MPFR 3.1.5, GNU MP 6.1.2)  With -M flag

[7] GNU Awk 5.1.0, API: 3.0 (GNU MPFR 3.1.5, GNU MP 6.1.2)
    gawk-5.1.0: cmd. line:1: warning: sqrt: received negative argument -2
    gawk-5.1.0: cmd. line:1: error: division by zero attempted

[8] GNU Awk 5.1.0, API: 3.0 (GNU MPFR 3.1.5, GNU MP 6.1.2)  With -M flag



reply via email to

[Prev in Thread] Current Thread [Next in Thread]