[Top][All Lists]

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

Re: Make division by zero legal

From: Peng Yu
Subject: Re: Make division by zero legal
Date: Tue, 3 Aug 2021 10:32:20 -0500

On 8/3/21, Andrew J. Schorr <> wrote:
> On Mon, Aug 02, 2021 at 09:04:52PM -0500, Neil R. Ormos wrote:
>> Andrew J. Schorr wrote:
>> > Nethox wrote:
>> >> [...]
>> >> $ gawk -M 'BEGIN { print 1/0; print "workaround" }'
>> >> +inf
>> >> workaround
>> > I can't speak to why gawk currently crashes on
>> > division by zero; it does seem odd that -M
>> > behaves differently.
>> Notwithstanding mawk and gawk -M, other awks do
>> treat division by zero as a fatal error and, like
>> gawk, have for years.
>> I rely on that behavior.  Perhaps others do, also.
>> Inasmuch as there are several trivial workarounds
>> available to the OP, I hope his or her proposed
>> change will not be incorporated into gawk as
>> default behavior.
> This is basically a bug in the -M implementation. It needs
> to be brought in line with the regular non-M behavior to
> throw a fatal division by zero error. So the "workaround"
> is not a good long-term plan.

I don't think -M implementation is buggy. It is the 1/0-to-error behavior buggy.

A warning may be returned (but there should be a way to turn the
warning off) for division by zero. But a fatal error definitely should
not be returned in such a case.

Besides the R example that I provided, python/numpy also allows
division by zero. Basically, for any package dealing with numerical
algorithms seriously, they allow division by zero. The fact that awk
does not allow it suggests that awk is deficient for implementing
numerical algorithms, which does not have to be so.

>>> x = np.arange(5)
>>> y = numpy.arange(start=1, stop=6)
>>> numpy.true_divide( y, x)
<stdin>:1: RuntimeWarning: divide by zero encountered in true_divide
array([       inf, 2.        , 1.5       , 1.33333333, 1.25      ])


reply via email to

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