C/C++ programming
derivative implementation
numerical methods
calculus in programming
coding tutorials

Implementing the derivative in C/C

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In C or C++, derivatives are usually implemented numerically unless you are building symbolic math software or using an automatic differentiation library. The standard approach is finite differences, where you sample a function near a point and estimate the slope from those nearby values.

Start with Finite Difference Methods

The three classic approximations are:

  • Forward difference.
  • Backward difference.
  • Central difference.

Central difference is often the best default because it is usually more accurate than forward or backward difference for the same step size.

Here is a simple C++ example that computes all three:

cpp
1#include <cmath>
2#include <functional>
3#include <iostream>
4
5double forward_diff(const std::function<double(double)>& f, double x, double h) {
6    return (f(x + h) - f(x)) / h;
7}
8
9double backward_diff(const std::function<double(double)>& f, double x, double h) {
10    return (f(x) - f(x - h)) / h;
11}
12
13double central_diff(const std::function<double(double)>& f, double x, double h) {
14    return (f(x + h) - f(x - h)) / (2.0 * h);
15}
16
17int main() {
18    auto f = [](double x) { return x * x; };
19    double x = 3.0;
20    double h = 1e-5;
21
22    std::cout << "forward:  " << forward_diff(f, x, h) << '\n';
23    std::cout << "backward: " << backward_diff(f, x, h) << '\n';
24    std::cout << "central:  " << central_diff(f, x, h) << '\n';
25}

For f(x) = x * x at x = 3, the true derivative is 6, so this is an easy correctness check.

Why Step Size Matters

Choosing h is a real numerical decision. If h is too large, the approximation is crude. If h is too small, floating-point cancellation becomes a problem because you subtract nearly equal numbers and lose precision.

Practical rule:

  • Start with something like 1e-5 or 1e-6 for double.
  • Test against functions with known derivatives.
  • Tune based on the scale of your input values.

For example, sin(x) is a useful test function:

cpp
1#include <cmath>
2#include <functional>
3#include <iostream>
4
5double derivative(const std::function<double(double)>& f, double x, double h) {
6    return (f(x + h) - f(x - h)) / (2.0 * h);
7}
8
9int main() {
10    auto f = [](double x) { return std::sin(x); };
11    double x = 1.0;
12    double h = 1e-5;
13
14    std::cout << "approx: " << derivative(f, x, h) << '\n';
15    std::cout << "exact:  " << std::cos(x) << '\n';
16}

Comparing the approximation to cos(x) gives you a practical sense of how the chosen h behaves.

A C-Style Version with Function Pointers

If you need a plain C implementation, function pointers are enough.

c
1#include <math.h>
2#include <stdio.h>
3
4double central_diff(double (*f)(double), double x, double h) {
5    return (f(x + h) - f(x - h)) / (2.0 * h);
6}
7
8double cubic(double x) {
9    return x * x * x;
10}
11
12int main(void) {
13    double x = 2.0;
14    double h = 1e-5;
15    printf("approx derivative: %.10f\n", central_diff(cubic, x, h));
16    return 0;
17}

This style is useful in embedded or low-dependency code where you want a small, explicit interface.

Higher-Level Design Choices

Derivative helpers often end up inside optimization, root finding, or simulation code, so the interface matters:

  • Function pointer or callable input.
  • Explicit step size parameter.
  • Numeric type control such as float or double.

In modern C++, templates can reduce overhead by avoiding std::function wrapping, but for many applications the simpler interface is acceptable unless profiling proves otherwise.

Second Derivative and Extensions

Once the first derivative helper is correct, extending it to a second derivative is straightforward.

cpp
1#include <cmath>
2#include <functional>
3#include <iostream>
4
5double second_derivative(const std::function<double(double)>& f, double x, double h) {
6    return (f(x + h) - 2.0 * f(x) + f(x - h)) / (h * h);
7}
8
9int main() {
10    auto f = [](double x) { return x * x; };
11    std::cout << second_derivative(f, 3.0, 1e-5) << '\n';
12}

This kind of helper is useful in curvature analysis and in optimization routines that need local shape information.

Validate Against Known Functions

Any numerical derivative implementation should be checked against analytic results when possible. Good test functions include:

  • 'x^2, derivative 2x'
  • 'sin(x), derivative cos(x)'
  • 'exp(x), derivative exp(x)'

Those tests help you verify both the formula and the chosen step size before the code is used in a larger numeric system.

Common Pitfalls

  • Using a step size that is too large or too small without testing.
  • Assuming forward difference is accurate enough when central difference would be better.
  • Forgetting floating-point limits and expecting exact results.
  • Using numerical differentiation where symbolic or automatic differentiation is actually required.
  • Skipping validation against functions with known derivatives.

Summary

  • Numerical derivatives in C or C++ are usually implemented with finite differences.
  • Central difference is often the best default for first derivatives.
  • Step size h strongly affects accuracy and stability.
  • Both C-style function pointers and C++ callables work well for derivative helpers.
  • Always validate the implementation against known analytic derivatives before trusting production results.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.