fork download
  1. import kotlin.math.abs
  2. import kotlin.math.exp
  3. import kotlin.math.ln
  4.  
  5. val EPS = 0.00001
  6.  
  7. fun f(x: Double, a: Double) : Double {
  8.  
  9. return exp(x) - a
  10. }
  11.  
  12. fun DivideEtImpera(lo: Double, hi: Double, a: Double) : Double {
  13.  
  14. if(abs(lo - hi) < EPS) {
  15.  
  16. return (lo + hi) / 2.0
  17.  
  18. } else {
  19.  
  20. val m:Double = (lo + hi) / 2.0
  21.  
  22. if(f(m,a)*f(lo,a) < 0) return DivideEtImpera(lo, m, a)
  23.  
  24. else return DivideEtImpera(m, hi, a)
  25. }
  26. }
  27.  
  28. fun _ln(a: Double) : Double {
  29.  
  30. return DivideEtImpera(0.0, a, a)
  31.  
  32. }
  33.  
  34. fun main(arr: Array<String>) {
  35.  
  36. var a: Double = 5.0
  37.  
  38. println(_ln(a))
  39. println(ln(a))
  40. }
Success #stdin #stdout 0.06s 33880KB
stdin
Standard input is empty
stdout
1.6094350814819336
1.6094379124341003