Page 1 of 1

Calculate ln(x) without math.h

Posted: Fri Nov 16, 2012 10:16 am
by rakeen
Write a program which will calculate $ln(x)$. You can't use math.h library. And the code should also be able to find the $ln(x)$ for $x>1$

Re: Calculate ln(x) without math.h

Posted: Sat Nov 17, 2012 7:35 pm
by Corei13
Use Maclaurin Series of ln(x).

Re: Calculate ln(x) without math.h

Posted: Sun Nov 18, 2012 12:09 pm
by rakeen
You can't plug 3 or 4 or 5 in the series since the series is for $-1<x<=1$ only

Re: Calculate ln(x) without math.h

Posted: Sun Nov 18, 2012 3:40 pm
by Corei13
Then write a function exp(x) to calculate (e^x) and use binary search.

Re: Calculate ln(x) without math.h

Posted: Sun Nov 18, 2012 8:43 pm
by *Mahi*
rakeen wrote:You can't plug 3 or 4 or 5 in the series since the series is for $-1<x<=1$ only
For $|x|>1$ determine $ln( \frac 1 x)$ and then multiply it by $-1$.

Re: Calculate ln(x) without math.h

Posted: Sun Nov 18, 2012 10:02 pm
by rakeen
Then write a function exp(x) to calculate (e^x) and use binary search.
you mean... if logx=p then $e^p=x$. then trial and error?!

However mahi's method worked :)

Re: Calculate ln(x) without math.h

Posted: Sun Nov 18, 2012 10:14 pm
by *Mahi*
rakeen wrote:you mean... if logx=p then $e^p=x$. then trial and error?!

However mahi's method worked :)
Yeah, the only difference is, binary seacrh is quite smart "trial and error" for strictly increasing functions like $\ln (x)$

Re: Calculate ln(x) without math.h

Posted: Wed Jan 09, 2013 1:46 am
by Masum
rakeen wrote:
Then write a function exp(x) to calculate (e^x) and use binary search.
you mean... if logx=p then $e^p=x$. then trial and error?!

However mahi's method worked :)
এইটা ঠিক ট্রায়াল এরর না। Newton-Raphson's Method.
If a continuous function $f(x)=0$ has $f(a)<0$ and $f(b)>0$ then $a<x<b$ must hold. So check with the mid value always and see which region it belongs to. If $f(mid)<0$ then of-course $f(x)<0$ for $a<mid$. Therefore, you can set $a=mid$ and vice-versa.
And binary search is the efficient one. Because Maclaurine series may have the convergence rate very slow, also you need to find that again if you don't remember. On the other hand, binary search gives you correct result to $6/7$ digits with at most $500$ loops.