help-gawk
[Top][All Lists]

## Re: Make division by zero legal

 From: Peng Yu Subject: Re: Make division by zero legal Date: Sun, 1 Aug 2021 17:27:59 -0500

```No. In R, it is perfectly legal.

R> 1/0
[1] Inf

It is not necessarily an error to use 0 as a denominator. It could be
just a very small number that is too close to zero to be represented
in a floating-point number. This is very common in math formulas. For
example, the following is a formula related to pearson correlation
that allowing 1/0 would make the implementation simpler (otherwise,
if-statement is needed to test the zero denominators.)

Therefore, it is better to allow 0 as a denominator.

On 8/1/21, Wolfgang Laun <wolfgang.laun@gmail.com> wrote:
> Programming languages are permitted to have their own rules regarding
> arithmetic: integer and floating point. In many cases, the definition of
> the IEEE standard regarding floating point arithmetic came after the
> definition of the language. The more abstract concept of arithmetic in
> awk's (and many other languages') definitions makes it undesirable to
> continue program execution after a division by zero.
>
> Which numeric algorithm would benefit from a continuation after an
> intermediate result of infinity? Isn't it preferable to learn about a
> problem in your program at the earliest possible moment?
>
> Wolfgang
>
>
>
> On Sun, 1 Aug 2021 at 19:24, Peng Yu <pengyu.ut@gmail.com> wrote:
>
>> Hi,
>>
>> https://en.wikipedia.org/wiki/Division_by_zero
>>
>> The above page says.
>>
>> "The IEEE floating-point standard, supported by almost all modern
>> floating-point units, specifies that every floating-point arithmetic
>> operation, including division by zero, has a well-defined result. The
>> standard supports signed zero, as well as infinity and NaN (not a
>> number)."
>>
>> \$ awk -e '{print 1/0 }'
>> awk: cmd. line:1: error: division by zero attempted
>>
>> But awk does not treat the above division as legal. Should this be
>> changed so that it gives less surprising results?
>>
>> In the meantime, what is the best walkaround to get awk conformed to
>> the IEEE standard?
>>
>> --
>> Regards,
>> Peng
>>
>>
>
> --
> Wolfgang Laun
>

--
Regards,
Peng

```