fork download
  1. #include <stdio.h>
  2.  
  3. double binEq(double left, double right, double eps, double (*f)(double))
  4. {
  5. double x = left;
  6.  
  7. if (left > right)
  8. {
  9. double t = left;
  10. left = right;
  11. right = t;
  12. }
  13.  
  14. double fl = f(left), fr = f(right);
  15.  
  16. if (fl*fr > 0)
  17. {
  18. fprintf(stderr,"Wrong range in binEq\n");
  19. return 0;
  20. }
  21.  
  22. while ( right - left > eps )
  23. {
  24. x = (left + right)/2.0;
  25. if (fl * f(x) < 0)
  26. right = x;
  27. else
  28. left = x;
  29. }
  30. return x;
  31. }
  32.  
  33. double F(double x)
  34. {
  35. return 11*x - 19;
  36. }
  37.  
  38.  
  39. int main()
  40. {
  41. printf("x = %lf\n",binEq(-100,100,1e-8,F));
  42. }
  43.  
Success #stdin #stdout 0s 4524KB
stdin
Standard input is empty
stdout
x = 1.727273