Re: Can someone please tell me where I am going wrong??



user923005 wrote:
Nick Keighley wrote:
E-Dot wrote:

I am trying to write a program which asks the user to enter a
number in the interval [1,2], the program then gives the natural
logarithm of that number, using the series for log(x+1)...

note that's log(x + 1), not log(x)...

The series expansion for log(x+1) is:
x - x^2/2 + x^3/3 - x^4/4 ...
Handbook of Mathematical Functions, equation 4.1.24:
http://www.math.sfu.ca/~cbm/aands/page_68.htm

#include <stdlib.h>

double ln(double x) {
double lnx, lnxold, delta;
int n;

if ((x <= 0) || ((x = x - 1.0) >= 1.0)) exit(EXIT_FAILURE);
lnxold = 0; delta = x; n = 1;
while ((lnx = lnxold + delta) != lnxold) delta *= x / ++n;
return lnx;
} /* untested, known to be slow */

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews


.