fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define F(t) (5 + 2*(t) + 0.6*(t)*exp(-0.3*(t)*(t)))
  6.  
  7. double binsearch(const double t0, const double r, const double dec) {
  8. const double eps = pow(10, -dec);
  9.  
  10. double a = t0 - r;
  11. double b = t0 + r;
  12. double c = t0;
  13.  
  14. do {
  15. double F_a = F(a);
  16. double F_b = F(b);
  17. double F_c = F(c);
  18.  
  19. if (fabs(F_c) < eps) {
  20. return c;
  21. }
  22.  
  23. if (signbit(F_a) ^ signbit(F_c)) {
  24. b = c;
  25. } else if (signbit(F_c) ^ signbit(F_b)) {
  26. a = c;
  27. } else {
  28. return NAN;
  29. }
  30.  
  31. c = (a + b) / 2;
  32. } while (1);
  33. }
  34.  
  35. main() {
  36. printf("found: %1.10f\n", binsearch(-2.5, 0.5, 10));
  37. return 0;
  38. }
Success #stdin #stdout 0s 2008KB
stdin
Standard input is empty
stdout
found: -2.3678784467