Bug #1185

gmt math (and grdmath) goes into an infinite loop given alpha close to 0

Added by Masakazu about 1 month ago. Updated about 1 month ago.

Status:ClosedStart date:2018-01-18
Priority:NormalDue date:
Assignee:Paul% Done:

100%

Category:-
Target version:Candidate for next bugfix release
Affected version:all Platform:

Description

I found that gmt math TCRIT goes into an infinite loop when a value close to 0 is given as "alpha", the first input parameter of TCRIT. The following is an example:

$ echo 10 > nu.txt
$ gmt math 1e-15 nu.txt TCRIT = /dev/stdout  # OK
-86.5254349447
$ gmt math 1e-16 nu.txt TCRIT = /dev/stdout  # infinite loop
gmtmath: gmtstat_student_t_a:  Bad argument(s).
gmtmath: gmtstat_student_t_a:  Bad argument(s).
  : (infinite loop)
^C
$ gmt math -1e-16 nu.txt TCRIT = /dev/stdout  # in reality, alpha should not be less than zero...
  (silence)
^C
$ gmt math 1.01 nu.txt TCRIT = /dev/stdout  # alpha should not be greater than one...
  (silence)
^C

This endless looping occurs with GMT5.4.x and GMT4.5.x.
The function gmt_tcrit() in gmt_stat.c uses the result of gmt_zcrit() as the first guess, and gmt_zcrit() returns -Inf or NaN in the above cases. Therefore, the iterative calculation in gmt_tcrit() cannot converge in such cases.
I think we can avoid the infinite loop by applying the patch below.

--- a/src/gmt_stat.c
+++ b/src/gmt_stat.c
@@ -1465,6 +1465,7 @@ double gmt_tcrit (struct GMT_CTRL *GMT, double alpha, double nu) {
         sign = -1.0;
     }
     t_low = gmt_zcrit (GMT, alpha);
+    if (isinf(t_low) != 0 || isnan(t_low)) return (t_low);
     t_high = 5.0;
     NU = irint(nu);
     gmtstat_student_t_a (GMT, t_high, NU, &p_high);

Associated revisions

Revision 19625
Added by Paul about 1 month ago

Address issue #1185

Revision 19626
Added by Paul about 1 month ago

Address issue #1185

History

#1 Updated by Paul about 1 month ago

  • Status changed from New to Resolved
  • Assignee set to Paul
  • Target version set to Candidate for next bugfix release
  • % Done changed from 0 to 100

Thanks for this patch. Fixed in r19626 for both 5.4 and 6 (r19626) and GMT4.

#2 Updated by Masakazu about 1 month ago

Thank you very much for fixing this issue.

#3 Updated by Paul about 1 month ago

  • Status changed from Resolved to Closed

You're welcome. Closed as fixed.

Also available in: Atom PDF