Advertisement
STANAANDREY

tpa rec3

Apr 1st, 2023
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #define TOL 1e-6
  3.  
  4. double root(double a, double b, double(*f)(double)) {
  5.   double mid = (a + b) / 2;
  6.   if ((b - a)/2 <= TOL) {
  7.     return mid;
  8.   }
  9.   if (f(a) * f(mid) < 0) {
  10.     return root(a, mid, f);
  11.   }
  12.   return root(mid, b, f);
  13. }
  14.  
  15. double f(double x) {
  16.   return 2*x*x*x - 6*x - 1;
  17. }
  18.  
  19. int main() {
  20.   printf("%g\n", root(1, 2, f));
  21.   return 0;
  22. }
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement