fork download
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. class Function {
  5. private:
  6. float (*function)(float);
  7. public:
  8. Function(float method(float)) : function(method) {}
  9. float eval(float x) {
  10. return function(x);
  11. }
  12. float derive(float x, float error = 0.001) {
  13. return (function(x) - function(x + error)) / error;
  14. }
  15. float integrate(float x0, float x1, float partitions = 100) {
  16. float integral = 0;
  17. float step = (x1 - x0) / partitions;
  18. for(float i = 0; i < partitions; i += step) integral += step * function(i);
  19. return integral;
  20. }
  21. };
  22.  
  23.  
  24. float exampleFunction(float x) {
  25. return 2 * pow(x, 2) + 5;
  26. }
  27.  
  28.  
  29. int main() {
  30. Function myFunction (exampleFunction);
  31. std::cout << myFunction.eval(6) << std::endl;
  32. }
  33.  
Success #stdin #stdout 0s 4316KB
stdin
Standard input is empty
stdout
77