Guest User

Untitled

a guest
Feb 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. Function.prototype.diff = function(eps) {
  2. var fn = this, eps = eps || 1e-6;
  3. return function(x) {
  4. return (fn(x+eps) - fn(x)) / eps;
  5. };
  6. };
  7.  
  8. (function(x) { return x*x }).diff()(6)
  9. // -> 12
  10.  
  11. Function.prototype.int = function(n) {
  12. var fn = this, n = n || 10000;
  13. return function(a,b) {
  14. var s = 0, i, eps = (b-a)/n;
  15. for (i = 0; i < n; i ++)
  16. s += fn(a + i*eps) * eps;
  17. return s;
  18. };
  19. };
  20.  
  21. (function(x) { return x*x }).int()(0,4)
  22. // -> 21.33
Add Comment
Please, Sign In to add comment