3 ms·
He is right, you could do something roughly equivalent in 1983-era C, albeit a fair amount clunkier due to the lack of partial application or (any kind of) type
by smitherfield 9y ago
He is right, you could do something roughly equivalent in 1983-era C, albeit a fair amount clunkier due to the lack of partial application or (any kind of) type-checking:
#define DX (double)0.0001
double f_prime(f, x)
register double (*f)();
double x;
{
return ((*f)(x + DX) - (*f)(x)) / DX;
}
#define DERIV(F, X) f_prime((F), (double)(X))
double cube(x)
double x;
{
return x * x * x;
}
#define DERIV_CUBE(X) DERIV(cube,(X))
main()
{
printf("%f\n%f\n%f\n", DERIV_CUBE(2), DERIV_CUBE(3), DERIV_CUBE(4));
}
- smitherfield 9y agoAnd in >=C89: #include <stdio.h> double deriv(double (*f)(double), double x) { static const double dx = 1e-8; return (f(x + dx) - f(x)) / dx; } static double cube(double x) { return x * x * x; } double deriv_cube(double x) { return deriv(cube, x); } int main(void) { printf("%f\n%f\n%f\n", deriv_cube(2), deriv_cube(3), deriv_cube(4)); return 0; }
- deleted 9y ago[deleted]