changeset 19484:238522618904

nthroot.m: Fix division by zero warning when input x is 0 (bug #43492). * nthroot.m: Check whether input is a scalar 0, and don't apply integer correction to calculated root as this leads to a division by zero. Add BIST test for bug.
author Rik <rik@octave.org>
date Wed, 29 Oct 2014 08:21:24 -0700
parents 64f034147e9a
children b80b396e7d54
files scripts/specfun/nthroot.m
diffstat 1 files changed, 13 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/specfun/nthroot.m
+++ b/scripts/specfun/nthroot.m
@@ -74,11 +74,14 @@
     endif
 
     if (integer_n && n > 0 && isfinite (n))
-      ## FIXME: What is this correction for?
-      y = ((n-1)*y + x ./ (y.^(n-1))) / n;
-      y = merge (isfinite (y), y, x);
+      if (isscalar (y) && y == 0)
+        ## Don't apply correction which leads to division by zero (bug #43492)
+      else
+        ## FIXME: What is this correction for?
+        y = ((n-1)*y + x ./ (y.^(n-1))) / n;
+        y = merge (isfinite (y), y, x);
+      endif
     endif
-
   endif
 
 endfunction
@@ -90,6 +93,12 @@
 %!assert (nthroot (-Inf, 7), -Inf)
 %!assert (nthroot (-Inf, -7), 0)
 
+## Bug #43492.  This should not generate a division by zero warning
+%!test
+%! warnmsg = lastwarn ();
+%! assert (nthroot (0, 2), 0);
+%! assert (lastwarn (), warnmsg);
+
 %% Test input validation
 %!error nthroot ()
 %!error nthroot (1)