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: Bob Proulx
Subject: Re: Why inf is not supported by gawk?
Date: Fri, 25 Jun 2021 13:32:14 -0600

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.

> 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.
    ...

   * 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



reply via email to

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