Skip to content

Commit

Permalink
stop polyroots segfaulting on degree-0 polynomial
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Feb 20, 2024
1 parent 4d220a5 commit 6c82695
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Basic/Math/cpoly.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ char *cpoly(double opr[], double opi[], int degree,
the zerofinder will work provided the overflowed quantity is
replaced by a large number. */

if (degree < 1)
return "algorithm only works for degree >= 1.";

if (opr[0] == 0.0 && opi[0] == 0.0)
return "algorithm fails if the leading coefficient is zero.";

Expand Down
3 changes: 2 additions & 1 deletion Basic/Math/math.pd
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ EOF
=for ref
Complex roots of a complex polynomial, given coefficients in order
of decreasing powers. As of 2.086, works with native-complex data.
of decreasing powers. Only works for degree >= 1.
As of 2.086, works with native-complex data.
=for usage
Expand Down
2 changes: 2 additions & 0 deletions t/math.t
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ $pa->inplace->erfi;
ok( all( approx( $pa, pdl(0.00886,0.0) )), "erfi inplace" );
}

eval {polyroots(1,0)};
like $@, qr/only works/, 'polyroots(1,0) throws exception not segfault';
my $coeffs = pdl(cdouble, 1,-55,1320,-18150,157773,-902055, 3416930,-8409500,12753576,-10628640,3628800);
my $roots = 1+sequence(10);
my $got;
Expand Down

0 comments on commit 6c82695

Please sign in to comment.