Crummy Code from Copilot

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

All trademarks and copyrights on this page are owned by their respective owners. Quotes from linked articles are probably the property of the publications linked or the property of the person(s) quoted. The rest © 2001- 2025 by James A. Chappell