fork download
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5. const float pi = 3.141592f;
  6.  
  7. template<typename T>
  8. float GetDerivative(T func, float val, float eps)
  9. {
  10. return (func(val + eps) - func(val)) / eps;
  11. }
  12.  
  13. int main()
  14. {
  15. auto sinRad = [](float rad){return sin(rad);};
  16. auto sinDeg = [](float deg){return sin(deg / 180.0f * pi);};
  17.  
  18. float eps = 1e-5f;
  19. std::cout << "dsinRad(x)/dx|0 = " << GetDerivative(sinRad, 0.0f, eps) << "\n";
  20. std::cout << "dsinDeg(x)/dx|0 = " << GetDerivative(sinDeg, 0.0f, eps) << "\n";
  21. return 0;
  22. }
Success #stdin #stdout 0s 4432KB
stdin
Standard input is empty
stdout
dsinRad(x)/dx|0 = 1
dsinDeg(x)/dx|0 = 0.0174533