From 6c8269513929e812ccd141443b9ad13b7f016ee9 Mon Sep 17 00:00:00 2001 From: Ed J Date: Tue, 20 Feb 2024 16:31:40 +0000 Subject: [PATCH] stop polyroots segfaulting on degree-0 polynomial --- Basic/Math/cpoly.c | 3 +++ Basic/Math/math.pd | 3 ++- t/math.t | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Basic/Math/cpoly.c b/Basic/Math/cpoly.c index 986276405..751899627 100644 --- a/Basic/Math/cpoly.c +++ b/Basic/Math/cpoly.c @@ -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."; diff --git a/Basic/Math/math.pd b/Basic/Math/math.pd index 78609b721..77f268296 100644 --- a/Basic/Math/math.pd +++ b/Basic/Math/math.pd @@ -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 diff --git a/t/math.t b/t/math.t index 7e43e0ec1..3b1bce949 100644 --- a/t/math.t +++ b/t/math.t @@ -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;