# [GRASS-dev] r.regression.line crash in awk (awk debugging)

Hamish hamish_b at yahoo.com
Sat Aug 8 23:26:19 EDT 2009

```[divide by zero in awk]

Markus Neteler wrote:
> ok... but how to add "protection"?
> The original input was 81 rows (pixels with certain
> temperature and their corresponding altitude from DEM).
>
> It prints out
> print tot, sumX, sumsqX, sumY, sumsqY, sumXY
> 7432 9.17582e+06 1.14575e+10 7432 7432 9.17582e+06
>
> Mhh, same situation again... perhaps I am just trying
> nonsense :)

"awk was developed before IEEE 754 arithmetic became widely available,
so the language does not fully support Infinity and NaN. In particular,
current awk implementations trap attempts to divide by zero, eve though
that is perfectly well-defined in IEEE 754 arithmetic."
--
'Classic shell scripting' by Arnold Robbins, Nelson Beebe
p.229 (O'Reilly Press)

tests:
\$ awk 'BEGIN {print 0.0/0.0}'
awk: fatal: division by zero attempted

\$ gawk 'BEGIN {print 0.0/0.0}'
gawk: fatal: division by zero attempted

\$ mawk 'BEGIN {print 0.0/0.0}'
nan

but I guess we really can't expect mawk to be installed.

if( sumsqX == sumX^2/tot ) { B = NaN; }
else { B = (sumXY - sumX*sumY/tot)/(sumsqX - sumX*sumX/tot); }
\
if( sumsqY == sumY^2/tot ) { R = NaN; }
else { R = (sumXY - sumX*sumY/tot)/((sumsqX - sumX^2/tot)*(sumsqY - sumY^2/tot))^0.5; }

or perhaps  if(sumsqX - sumX^2/tot < 1e-15 )  { B = NaN; } ...  ?

in this case due to squaring I don't think fabs() is needed.. will \$tot
always be positive?  (AFAICT fabs() doesn't exist in awk anyway)

> PS: Should I commit the BEGIN initialization anyway?

I don't think it is strictly needed, but it is good habit to do so.

Hamish

```