Mercurial > octave-nkf
comparison scripts/specfun/nthroot.m @ 19326: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 | a4c226a963c5 |
children | 4197fc428c7d |
comparison
equal
deleted
inserted
replaced
19325:64f034147e9a | 19326:238522618904 |
---|---|
72 else | 72 else |
73 y = x .^ (1/n); | 73 y = x .^ (1/n); |
74 endif | 74 endif |
75 | 75 |
76 if (integer_n && n > 0 && isfinite (n)) | 76 if (integer_n && n > 0 && isfinite (n)) |
77 ## FIXME: What is this correction for? | 77 if (isscalar (y) && y == 0) |
78 y = ((n-1)*y + x ./ (y.^(n-1))) / n; | 78 ## Don't apply correction which leads to division by zero (bug #43492) |
79 y = merge (isfinite (y), y, x); | 79 else |
80 ## FIXME: What is this correction for? | |
81 y = ((n-1)*y + x ./ (y.^(n-1))) / n; | |
82 y = merge (isfinite (y), y, x); | |
83 endif | |
80 endif | 84 endif |
81 | |
82 endif | 85 endif |
83 | 86 |
84 endfunction | 87 endfunction |
85 | 88 |
86 | 89 |
88 %!assert (nthroot (81, 4), 3) | 91 %!assert (nthroot (81, 4), 3) |
89 %!assert (nthroot (Inf, 4), Inf) | 92 %!assert (nthroot (Inf, 4), Inf) |
90 %!assert (nthroot (-Inf, 7), -Inf) | 93 %!assert (nthroot (-Inf, 7), -Inf) |
91 %!assert (nthroot (-Inf, -7), 0) | 94 %!assert (nthroot (-Inf, -7), 0) |
92 | 95 |
96 ## Bug #43492. This should not generate a division by zero warning | |
97 %!test | |
98 %! warnmsg = lastwarn (); | |
99 %! assert (nthroot (0, 2), 0); | |
100 %! assert (lastwarn (), warnmsg); | |
101 | |
93 %% Test input validation | 102 %% Test input validation |
94 %!error nthroot () | 103 %!error nthroot () |
95 %!error nthroot (1) | 104 %!error nthroot (1) |
96 %!error nthroot (1,2,3) | 105 %!error nthroot (1,2,3) |
97 %!error <X must not contain complex values> nthroot (1+j, 2) | 106 %!error <X must not contain complex values> nthroot (1+j, 2) |