cheusov / runawk

Powerful wrapper for AWK interpreter
http://sourceforge.net/projects/runawk/
82 stars 3 forks source link

erroneous output for abs( ) #5

Open mogando668 opened 2 years ago

mogando668 commented 2 years ago

all of these are positive values, but frequently abs() erroneously negate them, cuz the function was performing a string compare, in which leading-edge spaces ( " " ) , tabs ( \t ), "+", or "-", all rank lower in byte-ordinance value compared to "0"

    # gawk profile, created Sun Jun  5 06:08:41 2022

    # BEGIN rule(s)

    BEGIN {
     1      print abs("+0.000000000000000000001"), abs(" 0.000000000000000000001")
     1      print abs(".5"), abs(.5)
     1      print abs("+5"), abs(+5)
     1      print abs(sprintf("%+.20f", exp(1))), abs("+" exp(1)), abs(exp(1))
    }

    # Functions, listed alphabetically

     9  function abs(v)
    {
     9      if (v < 0) { # 6
     6          return -v
     3      } else {
     3          return v
        }
    }
——————————
-1e-21 -1e-21
-0.5 .5
-5 +5
-2.71828 -2.71828 2.71828