Search code examples
mathawkfloating-pointnan

AWK: Getting +nan when calculating power of number


Problem when using variable

echo ""| awk '{
x=-0.35
print x^1.35
print -0.35^1.35
}'

Result

+nan
-0.242377

Using GNU awk


Solution

  • The output is correct.

    The power operator ^ has higher precedence than the negation operator. Therefore, x^1.35 is (-0.35)^1.35 (a negative number to a non-integer power is a complex number, interpreted as a -nan), but -0.35^1.35 is -(0.35^1.35), a negated positive power of a positive number.