3 ms·
#define DX 0.0001 typedef double (*func)(double); double NthDeriv(int n, func f, double x) { if (n == 1) { return (f(x + DX) - f(x)
by QuadrupleA 8y ago
#define DX 0.0001
typedef double (*func)(double);
double NthDeriv(int n, func f, double x) {
if (n == 1) {
return (f(x + DX) - f(x)) / DX;
}
else {
return (NthDeriv(n - 1, f, x + DX) - NthDeriv(n - 1, f, x)) / DX;
}
}
double Cube(double x) { return x * x * x; }
double result = NthDeriv(3, &Cube, 5.0);
- ehaliewicz2 8y agoTouche. It's not as general as the lisp example though.