fix(bignum): MonomialEval drops highest-degree coefficient and panics on constants

MonomialEval started Horner evaluation from poly[len-2] instead of the
highest-degree coefficient poly[len-1], so it silently ignored the top
coefficient (1 + 2x at x=3 gave 1 instead of 7) and indexed poly[-1] on a
constant polynomial, panicking. Start from poly[n] and iterate from n-1,
matching the documented y = sum x^i * poly[i].

Closes #579
This commit is contained in:
dualfroz committed 2026-09-02 04:47:48 +02:00
1 parent 3733383faf
commit dbeea4d2e3
2 files changed
+26 -2

No files matched your search

+2 -2
View File
@@ -7,8 +7,8 @@ import (
// MonomialEval evaluates y = sum x^i * poly[i].
func MonomialEval(x *big.Float, poly []*big.Float) (y *big.Float) {
n := len(poly) - 1
y = new(big.Float).Set(poly[n-1])
for i := n - 2; i >= 0; i-- {
y = new(big.Float).Set(poly[n])
for i := n - 1; i >= 0; i-- {
y.Mul(y, x)
y.Add(y, poly[i])
}