S Lazy-H
  • Home
  • About
  • Posts
  • Contact
  • Slide Rules
  • A Biker’s Tale

Derivatives

mathematics
software
C++
Author

Sam Hutchins

Published

October 18, 2025

As I posted a bit about double integrals in Part 1 and Part 2, I thought I might go the other direction and post something about derivatives, or differentiation. Recall the integral example formula:

\[\sideset{}{_{-20}^{20}}\int{40 - 0.03 x^{2}} \: dx\]

Now the derivative formula is a bit different in function, but can act on the same parameters, as so:

\[f' = \lim_\limits{_{h \rightarrow 0}} \frac{f(x + h) - f(x)}{h} \quad or \quad \frac{d}{dx} (x) = 0 \]

So, if you had a function, \(x^{2}\), the derivative would be, \(2x\). Or more formally, \(x^{n}\) becomes \(n \cdot x^{n-1}\), and \(\sin(x)\) becomes \(\cos(x)\). In other words, it’s all about slope. So, back down to Earth, and to put it into perspective, let’s say we have a circle formula, \(\pi r^{2}\). Then the derivative of that would be, \(2 \pi r\), or \(\pi d\). So, we find the circumference of a circle is the derivative of the circle’s area. But, I digress.

In this post, we will talk about a simple C++ program to calculate the derivative of a polynomial. A quick description of the functionality is where the input string is split into tokens and for each term, the derivative is calculated separately and then added to get the final result. This was pieced together from other snippets on the Internet to do similar functions. Some limitations are the formats of the inputs require the power symbol for each substring. And so, \(4x^{3}\), would be entered as ‘4x^3’, for example. If \(x\) is to be entered for a power of one, the input would require ‘1x^1’. If an input was not required, ‘0’ could be entered. Also, the inputs only accept integers. So, 4.9x^2 would be calculated as 4x^2. This is a future TODO.

If we had a polynomial formula, \(4x^2 + 11x^5 -4x^3 + 6x^3\), which would look like:

\[\frac{d}{dx} (4x^2 + 11x^5 -4x^3 + 6x^3) = 4533\]

Anyway, to the program. We use two functions to determine the derivative of each term. To get the terms, we use the following code snippet.

Code
cout << "Var a: "; cin >> p1; if(p1 == "0") { p1 = "(1x^1)-1"; a1 = ""; } else { a1 = p1 + " + "; }
cout << "Var b: "; cin >> p2; if(p2 == "0") { p2 = "(1x^1)-1"; a2 = ""; } else { a2 = p2 + " + "; }
cout << "Var c: "; cin >> p3; if(p3 == "0") { p3 = "(1x^1)-1"; a3 = ""; } else { a3 = p3 + " + "; }
cout << "Var d: "; cin >> p4; if(p4 == "0") { p4 = "(1x^1)-1"; a4 = ""; } else { a4 = p4 + " + "; }
cout << "Var e: "; cin >> p5; if(p5 == "0") { p5 = "(1x^1)-1"; a5 = "0"; } else { a5 = p5; } 

The if() statements are to handle zero inputs, yet provide tokens for calculation, and to make the resultant formula output look better. One area that needs improvement is the trailing ‘+’ if there are zero inputs at the end of the entries. I started to fix that with additional ‘if/then/else’ statements, but it became too cumbersome, so I dropped that idea, at least for now. It was only to make the resultant formula display prettier, and could be done away with entirely.

The two functions to calculate the derivatives are the business end of the program, and are presented as is:

Code
long long derivativeTerm(string pTerm, long long val)
{
    // Get coefficient
    string coeffStr = "";
    long unsigned int i; // type 'int i' clashes with 'pTerm.size()', but will compile.
    for (i = 0; pTerm[i] != 'x'; i++)
        coeffStr.push_back(pTerm[i]);
    long long coeff = atol(coeffStr.c_str());

    // Get Power (Skip 2 characters for x and ^)
    string powStr = "";
    for (i = i + 2; i != pTerm.size(); i++)
        powStr.push_back(pTerm[i]);
    long long power = atol(powStr.c_str());

    // For ax^n, we return anx^(n-1) 
    return coeff * power * pow(val, power - 1);
}

long long derivativeVal(string& poly, int val)
{
    long long ans = 0;

    // We use istringstream to get input in tokens
    istringstream is(poly);

    string pTerm;
    while (is >> pTerm) {

        // If the token is equal to '+' then
        // continue with the string
        if (pTerm == "+")
            continue;
      
        // Otherwise find the derivative of that
        // particular term
        else
            ans = (ans + derivativeTerm(pTerm, val));
    }
    return ans;
}

There is ample opportunity for improvement, especially for double entries. But for now, I have other projects to consider, so that may be for a later time.

So for now, have a great day and God Bless you and yours!

© S Lazy-H 2019 -