bug-gawk
[Top][All Lists]
Advanced

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

Re: Why inf is not supported by gawk?


From: Peng Yu
Subject: Re: Why inf is not supported by gawk?
Date: Fri, 25 Jun 2021 14:53:34 -0500

On 6/25/21, Bob Proulx <bob@proulx.com> wrote:
> Peng Yu wrote:
>> C can process "inf".
>> https://stackoverflow.com/questions/1923837/how-to-use-nan-and-inf-in-c
>
> C is not AWK and AWK is not C.  They are related.  Kissing cousins.
> Yet they are still different.

This answer does not get to the bottom of the question.

The question is why it makes sense not to support inf in awk.

What is the disadvantage of support inf besides that with "+" and "-" in awk?

Besides C, ruby supports it without "+".

https://stackoverflow.com/questions/5778295/how-to-express-infinity-in-ruby

python also supports it.

>>> print(math.inf)
inf

Javascript:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity

The list goes on.

It is an anomaly that awk does not support the version of inf without
+ and - signs.

>> Why awk does not understand "inf" as infinity? Is it better to support
>> it? (I only care about the normal mode, I don't care about any other
>> non-default modes.)
>
> This is explained in detail in the documentation.  I'll quote it.
>
>    * Allowing completely alphabetic strings to have valid numeric values
>     is also a very severe departure from historical practice.
>     ...

These just explain what is it but not why is it so.

>    * Without '--posix', 'gawk' interprets the four string values '+inf',
>      '-inf', '+nan', and '-nan' specially, producing the corresponding
>      special numeric values.  The leading sign acts a signal to 'gawk'
>      (and the user) that the value is really numeric.  Hexadecimal
>      floating point is not supported (unless you also use
>      '--non-decimal-data', which is _not_ recommended).  For example:
>
>           $ echo nanny | gawk '{ print $1 + 0 }'
>           -| 0
>           $ echo +nan | gawk '{ print $1 + 0 }'
>           -| +nan
>           $ echo 0xDeadBeef | gawk '{ print $1 + 0 }'
>           -| 0
>
>      'gawk' ignores case in the four special values.  Thus, '+nan' and
>      '+NaN' are the same.
>
>> $ gawk -e '{ print $1 + 1 }' <<< "inf"
>> 1
>> $ gawk -e '{ print $1 + 1 }' <<< "+inf"
>> +inf
>
> All correct.  As explained in the documenation.
>
> To put Infinity in the same as NaN in the documentation one might use:
>
>           $ echo +inf | gawk '{ print $1 + 0 }'
>           -| +inf
>
>           $ echo inf | gawk '{ print $1 + 0 }'
>           -| 0
>
> Bob
>
>


-- 
Regards,
Peng



reply via email to

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