hi everyone!
i'm trying to write an algorithm that approximates ln(x),and i just can't come up with a decent one...can u please tell me how to do that,or how log(x) is defined in math.h...
so far i've tried the newton-raphson method,a few other formulas etc but nothing seems to work.please help!
i don't need a very specific answer,just the name of the algorithm will be great...
thx for ur time!
ok thx,so i think i stressed the wrong part in the question,the problem is
i can't implement the algorithms precisely enough,i guess because i'm using float as the return type of my functions.do you think i need to define some structures(similar to LARGE_INTEGER ) and use them as return types and parameters?
Since the function in itself is unimportant isolated, I think you should make it a templated function and allow the caller of the function decide what to use (float, double, LARGE_INTEGER, some other big number class, etc.).
where EXP_T(a) returns exp(a) using the taylor series,and LN_Newton(a) uses the Newton-Raphson method
to return ln(x).
i'd like the functions to return more precise results,for example 15 digits after the radix point.right now it returns what log(x) from math.h would return,for instance 0.999897 for ln(2.718),instead of
0.999896315729 wich is what any decent calculator returns.
how can i reach this precision?thx again for ur help!
I repeat: Make it templated and let the caller decide the precision he/she likes.
But regardless of my original advise, your problem probably relies in N. You need to make it so it takes advantage of the double data type (0.0001 is too big for double's capability). You need to be introduced to std::numeric_limits. It can tell you a bunch of information about the capabilities of the data type, BUT I really don't know much about the architecture of floating point data types to give you correct advise on how to base your calculations.
For example, numeric_limits can tell you the maximum precision of the data type via its digits10 field, but I don't know how to use this one to improve your loop exit condition. What if the calculated value is just really big? It would mean that the result is using the precision digits to show really just an approximate integer in scientific notation. Your loop then could take a very long time to exit and as a matter of fact it may never exit because you may be stuck subtracting, for example, 1.12345678901235E1100 - 1.12345678901234E1100, which is 1E1086, and this is way bigger than your current N!
So, from the above rationale, I think you should always strip the E part out of the number and then compare differences between the two numbers in terms of just the mantissa. But then again, you should only do this after ensuring the exponent part is equal in both terms!
And now comes the most difficult part: Because of the computer's binary nature, numbers cannot be exactly represented sometimes. Do I know for sure if this is the case in the mantissa part of the double data type? No, I don't know. I don't know anything about this topic as a matter of fact! But it is definitely something you need to look out for because if you make your N fall within a problematic value that cannot be accurately represented, your loop may never exit as well.
So, all in all, the problems are a few but quite challenging, IMO. I can only say: Study numeric_limits and determine a way of stripping the exponent part out of the number, then base your loop exit on the differences in the mantissa only.
wow.thx webJose, i hadn't thought that much about big numbers,now that u say it,i think the problem is not only the N,but the following:
e^x and therefore e^x-a grow very fast,and if the initial guess of x0 (to use with newton-raphson)
is too big,then the convergence is amazingly slow(i guess it's intuitively obvious,from visualizing e^x and the newtonraphson method for a really big x)
and to compute ln(x),the initial guess used in my function is x0=x,so as x grows,the convergence slows down.
so the real problem is the initial guess,and then N...
i have no idea how to make a better initial guess though,i think using bisection could help,but then,i'm somehow ashamed to admit it,i can't really figure out how to find a value m for wich e^m-a<0 for any a,without having to calculate a new ln.
so any suggestion would be very welcome...
thx again webjose...by the way,since numeric_limits<double>::digits10 returns 15,i don't quite understand how
0.0001 is too big for double...excuse my stupidity
What I meant with that is that 0.0001 is and understatement for the double data type, which can theoretically distinguish changes as small as 0.00000000000001. Since your original question was: How do I get more decimals in the answer, clearly picking up such an N was contrary to your objective.