Why Gawk Thinks 2.0e-318 is Greater Than 2.0

gawknumeric dataUbuntu

I am trying to find the maximum value of a column of data using gawk:

gawk 'BEGIN{max=0} {if($1>0+max) max=$1} END {print max}' dataset.dat

where dataset.dat looks like this:

2.0
2.0e-318

The output of the command is

2.0e-318

which is clearly smaller than 2.

Where is my mistake?

Edit

Interestingly enough, if you swap the rows of the input file, the output becomes

2.0

Edit 2

My gawk version is GNU Awk 4.2.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2).

Best Answer

The 0+ needs to be prefixed to each $1 to force a numeric conversion. max does not need 0+ -- it is already cast to numeric when it is stored.

Paul--) AWK='
> BEGIN { max = 0; }
> 0+$1 > max { max = 0 + $1; }
> END { print max; }
> '
Paul--) awk "${AWK}" <<[][]
> 2.0
> 2.0e-318
> [][]
2
Paul--) awk "${AWK}" <<[][]
> 2.0e-318
> 2.0
> [][]
2
Related Question