fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define EPS 0.00001
  4.  
  5. double ln(double x) {
  6.  
  7. return log(x);
  8. }
  9.  
  10. double f(double x, double a) {
  11.  
  12. return exp(x) - a;
  13. }
  14.  
  15. double divideEtImpera(double lo, double hi, double a) {
  16.  
  17. if(fabs(lo - hi) < EPS) {
  18.  
  19. return (lo + hi) / 2;
  20.  
  21. } else {
  22.  
  23. double m = (lo + hi) / 2;
  24.  
  25. if( ( f(lo, a) * f(m, a) ) < 0) return divideEtImpera(lo, m, a);
  26.  
  27. else
  28. return divideEtImpera(m, hi, a);
  29.  
  30. }
  31. }
  32.  
  33. double LN(double a) {
  34.  
  35. return divideEtImpera(0, a, a);
  36. }
  37.  
  38. int main(int argc, char const *argv[])
  39. {
  40. int a;
  41. scanf("%d", &a);
  42. printf("ln(%d) = %.5f\n", a, LN(a));
  43. //built-in function
  44. printf("ln(%d) = %.5f - built-in function c", a, ln(a));
  45. return 0;
  46. }
Success #stdin #stdout 0s 4364KB
stdin
10
stdout
ln(10) = 2.30258
ln(10) = 2.30259 - built-in function c