While doing a Bing search for c++ polynomial, Copilot generates the following code:
Note the method, Polynomial::evaluate(double x) const
While this will calculate the correct value, this naïve implementation is not a good way to evaluate a polynomial.
A better implementation would be:
Each iteration has one addition and one multiplication, while the Copilot version has one addition, one multiplication, and one call to std::pow per iteration. Also, the good version has no <cmath> dependency.
Copilot will generate something like the “better version” if one searches for c++ polynomial horner’s method. Since Horner’s Method is the preferred way to evaluate polynomials, one has to wonder why Copilot generates a not very good naïve implementation by default.
Here’s the full source of the “better version”:
See also: Polynomial