(The regression is incorrect from econometric point of view, just to give an easily
reproducible example) 
open /usr/local/share/gretl/data/misc/denmark.gdt 
ols LRM const LRY 
dw=$dwpval 
(here Durbin-Watson = 0.298798) 
From script output:  
? dw=$dwpval Imhof integral gave negative
value 
Error executing script: halting > dw=$dwpval 
May be it worth to include into the function something like this:  if dw<dw0 (or if 
Imhof <=0) or pvalue<p0  print( pvalue<e-N )for some N, say e-16? 
R ouput for the same dataset (dwtest{lmtest}) 
DW = 0.2988, p-value < 2.2e-16