help-gawk
[Top][All Lists]
Advanced

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

Re: ensure numeric comparison


From: Peng Yu
Subject: Re: ensure numeric comparison
Date: Mon, 9 May 2022 11:36:14 -0500

On 5/8/22, Andrew J. Schorr <aschorr@telemetry-investments.com> wrote:
> Hi,
>
> On Fri, May 06, 2022 at 05:28:39PM -0500, Peng Yu wrote:
>> I want to make sure whether the following code is guaranteed to
>> compare $1 and $2 numerically as long as the input of the 1st and 2nd
>> fields are legitimate numbers or empty strings (which are considered
>> as 0).
>>
>> awk -e '{ print ($1 < $2) }' < input
>>
>> Or there is any corner case when even the 1st and 2nd fields are
>> legitimate numbers or empty strings, they may be compared as strings.
>>
>> Note although an empty string compared with an empty string is a
>> string comparison, the result is the same as 0 and 0 comparison, so it
>> is still effectively considered as a numerical comparison for the
>> current discussion.
>
> This discussion has gone off the rails. If you've got user input
> of an empty string, then gawk will consider it to be a string, not
> a number, and the comparision will be a string comparison. If you
> want to ensure a numeric comparison, you must coerce both arguments
> to be numbers. This is typically done by adding zero.
>
> It's explained by this table in the docs:
>
>    When two operands are compared, either string comparison or numeric
> comparison may be used.  This depends upon the attributes of the
> operands, according to the following symmetric matrix:
>
>         +----------------------------------------------
>         |       STRING          NUMERIC         STRNUM
> --------+----------------------------------------------
>         |
> STRING  |       string          string          string
>         |
> NUMERIC |       string          numeric         numeric
>         |
> STRNUM  |       string          numeric         numeric
> --------+----------------------------------------------
>
> And by this bit of sample code showing that an empty string is considered
> to be a string:
>
> bash-4.2$ echo 1::b | gawk -F: '{print typeof($1), typeof($2), typeof($3)}'
> strnum string string

It is not always the case. Empty strings may be turned into
"unassigned". See below for a test case. That's why I want to ask to
make sure if there are corner cases. Obviously, it is hard not to miss
corner cases, as you missed this one :)

$ awk -e '{ print typeof($1) }' <<< ''
unassigned

> The beauty of gawk is that it's very easy to test and get your answer:
>
> bash-4.2$ echo :-1 | gawk -F: '{print ($1 < $2)}'
> 1
> bash-4.2$ echo :-1 | gawk -F: '{print ($1+0 < $2+0)}'
> 0
>
> Is everything now crystal clear?

OK. I got it.

When "unassigned" is included, what is the comparison table look like?

-- 
Regards,
Peng



reply via email to

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