bug-gawk
[Top][All Lists]
Advanced

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

Re: gawk fails when comparing input record with variable passed as param


From: david kerns
Subject: Re: gawk fails when comparing input record with variable passed as parameter
Date: Thu, 30 Jan 2025 07:10:54 -0700

On Thu, Jan 30, 2025 at 7:04 AM Andrew J. Schorr <
aschorr@telemetry-investments.com> wrote:

> Hi,
>
> There are a couple of different things going on here:
>
> 1. In your Repeat-By script, the original problem is caused by
> floating-point precision issues. Please take a look at this:
>
>
> https://www.gnu.org/software/gawk/manual/html_node/Arbitrary-Precision-Arithmetic.html
>
> 2. In your Fix, you're comparing a string (var) to a strnum ($1).
> Please take a look at this:
>
> https://www.gnu.org/software/gawk/manual/html_node/Variable-Typing.html
>
> You may want to play around with the -M option to gawk,
> and the typeof() function can also help you to see what's
> going on.
>
> Regards,
> Andy
>
> On Thu, Jan 30, 2025 at 12:29:50PM +0000, ismael@barros2.org wrote:
> > Configuration Information [Automatically generated, do not change]:
> > Machine: x86_64
> > OS: linux-gnu
> > Compiler: gcc
> > Compilation CFLAGS: -g -O2 -fno-omit-frame-pointer
> -mno-omit-leaf-frame-pointer
> -ffile-prefix-map=/build/gawk-Le8UNP/gawk-5.2.1=. -flto=auto
> -ffat-lto-objects -fstack-protector-strong -fstack-clash-protection
> -Wformat -Werror=format-security -fcf-protection
> -fdebug-prefix-map=/build/gawk-Le8UNP/gawk-5.2.1=/usr/src/gawk-1:5.2.1-2build3
> -DNDEBUG
> > uname output: Linux Bebop2 6.8.0-51-generic #52-Ubuntu SMP
> PREEMPT_DYNAMIC Thu Dec  5 13:09:44 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
> > Machine Type: x86_64-pc-linux-gnu
> >
> > Gawk Version: 5.2.1
> >
> > Attestation 1:
> >       I have read
> https://www.gnu.org/software/gawk/manual/html_node/Bugs.html.
> >       Yes
> >
> > Attestation 2:
> >       I have not modified the sources before building gawk.
> >       True
> >
> > Description:
> >       When comparing an input record with a variable passed as
> parameter, the result is sometimes incorrect.
> >
> >       For instance, in the command 'echo "054287e607" | awk -v
> var="1000000024e76630"', the expression ($1==var) is supposed to evaluate
> to ("054287e607"=="1000000024e76630"), so it should be FALSE, but it's TRUE
> instead.
> >
> > Repeat-By:
> >       echo "054287e607" | LC_ALL=C awk -v var="1000000024e76630" '{
> print $1, var, ($1==var), ("054287e607"==var), ($1=="1000000024e76630") }'
> >
> >       Actual output:    054287e607 1000000024e76630 1 0 0
> >       Expected output:  054287e607 1000000024e76630 0 0 0
> >
> > vFix:
> >       Declaring the variable in the BEGIN section instead of using -v
> fixes the issue:
> >
> >       echo "054287e607" | LC_ALL=C awk 'BEGIN { var="1000000024e76630" }
> { print $1, var, ($1==var), ("054287e607"==var), ($1=="1000000024e76630") }'
> >       054287e607 1000000024e76630 0 0 0
>
>
explicitly:
$ echo "054287e607" | LC_ALL=C awk -v var="1000000024e76630" ' { print
typeof($1), typeof(var), $1, var, ($1==var), ("054287e607"==var),
($1=="1000000024e76630") }'
strnum strnum 054287e607 1000000024e76630 1 0 0

$ echo "054287e607" | LC_ALL=C awk 'BEGIN { var="1000000024e76630" } {
print typeof($1), typeof(var), $1, var, ($1==var), ("054287e607"==var),
($1=="1000000024e76630") }'
strnum *string* 054287e607 1000000024e76630 0 0 0

and the fix:
$ echo "054287e607" | LC_ALL=C awk -v var="1000000024e76630" ' { print
typeof($1), typeof(var), $1, var, ($1==var ""), ("054287e607"==var),
($1=="1000000024e76630") }'
strnum strnum 054287e607 1000000024e76630 0 0 0


reply via email to

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