Skip to content

Fields that look like numbers compare as strings #150

Open

Description

@jayhemnani9910

$1 > 100 keeps 50, and $1 == 1 drops 1.0:

$ printf '50 150\n1.0 2\n10 9\n' | awk '$1 > 100'
50 150
$ printf '50 150\n1.0 2\n10 9\n' | awk '$1 == 1'
$ printf '50 150\n1.0 2\n10 9\n' | awk '$1 < $2'
1.0 2
10 9

gawk 5.3.2, mawk and nawk print nothing, 1.0 2, and 50 150 / 1.0 2.

POSIX makes a field that looks like a number a numeric string, so it compares numerically with a number or with another numeric string. Here every field is a plain string (typeof($2) on x 10 prints string; gawk prints strnum), so these comparisons are done byte by byte. The same rule covers getline var, split() elements, ARGV, ENVIRON and -v values.

AwkValue::StrNum exists but nothing creates it yet, so this looks like the planned place for it. Is that the plan? I can send a PR for fields if a direction is agreed.

Checked on main 8f8cf8d.

Activity

  1. Alonely0 commented on Oct 27, 2026

    @Alonely0
    Collaborator

    AwkValue::StrNum exists but nothing creates it yet, so this looks like the planned place for it. Is that the plan? I can send a PR for fields if a direction is agreed.

    The problem StrNum has is it's trying to juggle two jobs at once: num/str caching and user input distinction. StrNums ought only be observable via typeof when constructed from external input (records, fields...), but they are still useful to cache a string's numeric value and vice versa (which is what gawk does internally if their docs are anything to go by).

    I've reviewed your PR, and came to the conclusion that we really should just bail on using an enum for type representation. It was convenient while it lasted... I would rather do this refactoring, though I'm not going to have the time this week due to my midterm exams.

  2. added
    bug
    on Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions