cemyuksel / cyCodeBase

An open source programming resource intended for graphics programmers.
MIT License
271 stars 60 forks source link

Infinite loops #20

Closed ubitux closed 1 year ago

ubitux commented 1 year ago

The following causes an infinite loop:

cy::Polynomial<float,4> poly = {0.f, 0.f, 0.f, -3.9f, -0.6f};
poly.Roots(roots);

It can be worked around with:

diff --git a/cyPolynomial.h b/cyPolynomial.h
index 9fdf4b8..842493e 100644
--- a/cyPolynomial.h
+++ b/cyPolynomial.h
@@ -494,7 +494,7 @@ inline ftype RootFinderNewton::FindOpen( ftype const coef[N+1], ftype const deri

        bool otherside = IsDifferentSign( ym, yr );

-       while ( yr != 0 ) {
+       while ( std::isfinite(yr) && yr != 0 ) {
                if ( otherside ) {
                        if constexpr ( openMin ) {
                                return FindClosed<N,ftype,boundError>( coef, deriv, xr, xm, yr, ym, xError );

But there are others. Typically:

cy::Polynomial<float,5> poly = {1.f, 0.f, 0.f, 0.f, 0.f, 0.f};
poly.Roots(roots);

You may want to address these. It seems to be because of some NaN slipping in sometimes, messing up the algorithm.

cemyuksel commented 1 year ago

Thanks for bringing these up. The cause of this issue was my decision to ignore duplicated roots of quadratic polynomials. It is fixed in the latest version.