## Calculate ln(x) without math.h

Discuss Computer Science and Programming related problems

Moderators: bristy1588, Labib

rakeen
Posts: 384
Joined: Thu Dec 09, 2010 5:21 pm
Location: Dhaka

### Calculate ln(x) without math.h

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$
r@k€€/|/

Corei13
Posts: 153
Joined: Tue Dec 07, 2010 9:10 pm
Location: Chittagong

### Re: Calculate ln(x) without math.h

Use Maclaurin Series of ln(x).
ধনঞ্জয় বিশ্বাস

rakeen
Posts: 384
Joined: Thu Dec 09, 2010 5:21 pm
Location: Dhaka

### Re: Calculate ln(x) without math.h

You can't plug 3 or 4 or 5 in the series since the series is for $-1<x<=1$ only
r@k€€/|/

Corei13
Posts: 153
Joined: Tue Dec 07, 2010 9:10 pm
Location: Chittagong

### Re: Calculate ln(x) without math.h

Then write a function exp(x) to calculate (e^x) and use binary search.
ধনঞ্জয় বিশ্বাস

*Mahi*
Posts: 1175
Joined: Wed Dec 29, 2010 12:46 pm
Location: 23.786228,90.354974
Contact:

### Re: Calculate ln(x) without math.h

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$.

Use $L^AT_EX$, It makes our work a lot easier!

rakeen
Posts: 384
Joined: Thu Dec 09, 2010 5:21 pm
Location: Dhaka

### Re: Calculate ln(x) without math.h

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
r@k€€/|/

*Mahi*
Posts: 1175
Joined: Wed Dec 29, 2010 12:46 pm
Location: 23.786228,90.354974
Contact:

### Re: Calculate ln(x) without math.h

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)$

Use $L^AT_EX$, It makes our work a lot easier!

Masum
Posts: 592
Joined: Tue Dec 07, 2010 1:12 pm
you mean... if logx=p then $e^p=x$. then trial and error?!
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.
One one thing is neutral in the universe, that is $0$.