bug-gawk
[Top][All Lists]
Advanced

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

isnan() and isinf() User-Defined Functions for awk


From: J Naman
Subject: isnan() and isinf() User-Defined Functions for awk
Date: Sun, 28 Jan 2024 12:41:20 -0500

Configuration Information [Automatically generated, do not change]:
Gawk Version: GNU Awk 5.3.0, API 4.0
Machine: AMD64(64-bit gawk)
OS: Windows_NT = Win 7 [Version 6.1.7601]
platform: mingw
Compiler: ?
Compilation CFLAGS: ?
uname output: ?
LC_ALL: C
I have read: https://www.gnu.org/software/gawk/manual/html_node/Bugs.html
  Yes or No: Yes
gawkbug ver: win.22.1226

Description: I have been unable to find awk user-level functions that work
for isnan() and isinf(). The C and other lang functions do not easily
convert or adapt to an awk library function. If appropriate, consider
incorporating into gawk documentation (UG 16.4.1.4 ?) and/or to a place
such as the directory awklib/eg/test-programs in the gawk distribution.
Free to copy, copyright to FSF, etc.

Repeat-By: FYI: nan = sqrt(-1) causes a warning in stderr.
Indeterminate forms return NaN without a gawk warning or error:
inf - inf (infinity minus infinity) returns NaN.
0 * inf  (zero multiplied by infinity) returns NaN.

Fix: Two functions below, both work regardless of +/- sign of nan or inf.
function isnan(arg)
{
 return arg != arg     # Nan is the only value != to itself
}

function isinf(arg)
{
return (0 != (arg * 0)) && (arg == arg)
# inf * 0 is nan; NaN * 0 is nan; arg == arg is true for inf, not NaN
#    0 * all other scalars == 0, including strings & strong regexp
}

# these two awk functions should be portable to any awk implementation
#    because they are harmless (always return 0) if "nan" and/or "inf" are
not implemented


reply via email to

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