Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Created November 27, 2008 12:25
Show Gist options
  • Save jcoglan/29735 to your computer and use it in GitHub Desktop.
Save jcoglan/29735 to your computer and use it in GitHub Desktop.
Function.prototype.diff = function(eps) {
var fn = this, eps = eps || 1e-6;
return function(x) {
return (fn(x+eps) - fn(x)) / eps;
};
};
(function(x) { return x*x }).diff()(6)
// -> 12
Function.prototype.int = function(n) {
var fn = this, n = n || 10000;
return function(a,b) {
var s = 0, i, eps = (b-a)/n;
for (i = 0; i < n; i ++)
s += fn(a + i*eps) * eps;
return s;
};
};
(function(x) { return x*x }).int()(0,4)
// -> 21.33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment