Calculate ln(x) without math.h

Discuss Computer Science and Programming related problems

Moderators:Labib, bristy1588

User avatar
rakeen
Posts:384
Joined:Thu Dec 09, 2010 5:21 pm
Location:Dhaka
Calculate ln(x) without math.h

Unread post by rakeen » Fri Nov 16, 2012 10:16 am

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

Unread post by Corei13 » Sat Nov 17, 2012 7:35 pm

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

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

Re: Calculate ln(x) without math.h

Unread post by rakeen » Sun Nov 18, 2012 12:09 pm

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

Unread post by Corei13 » Sun Nov 18, 2012 3:40 pm

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

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

Re: Calculate ln(x) without math.h

Unread post by *Mahi* » Sun Nov 18, 2012 8:43 pm

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$.
Please read Forum Guide and Rules before you post.

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

Nur Muhammad Shafiullah | Mahi

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

Re: Calculate ln(x) without math.h

Unread post by rakeen » Sun Nov 18, 2012 10:02 pm

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€€/|/

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

Re: Calculate ln(x) without math.h

Unread post by *Mahi* » Sun Nov 18, 2012 10:14 pm

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)$
Please read Forum Guide and Rules before you post.

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

Nur Muhammad Shafiullah | Mahi

User avatar
Masum
Posts:592
Joined:Tue Dec 07, 2010 1:12 pm
Location:Dhaka,Bangladesh

Re: Calculate ln(x) without math.h

Unread post by Masum » Wed Jan 09, 2013 1:46 am

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.
One one thing is neutral in the universe, that is $0$.

Post Reply