A
asadfag
So I'm learning C and here's the first thing I'm having problems with.
The goal is to compare the real ln and ln(x) computed using the
following sequence :
http://xs135.xs.to/xs135/09033/hw623.gif
And here's my try:
#include <stdio.h>
#include <math.h>
float power(float n, float m)
{
int i;
float p;
p = 1;
for (i = 1; i <= m; i++)
p = p * n;
return p;
}
float seqsum(int n, float x)
{
int i;
float r,s;
s = (x - 1) / (x + 1);
for (i = 1; i < n; i++)
s = s + (power(x - 1, 2 * i + 1)) / 3 * (power(x + 1, 2 * i +
1));
return s;
}
main()
{
float d,e;
int i;
printf("approx\t\tvalue\n");
for (i = 1; i <= 8; i = i * 2)
{
d = seqsum(i,0.1);
e = log(0.1);
printf("%f\t%f\n", d,e);
}
}
But the output values are horribly wrong for some inputs:
For x = 0.1
approx value
-1.636364 -2.302585
-2.283230 -2.302585
-3.538600 -2.302585
-5.902889 -2.302585
For x = 0.3
approx value
-1.076923 -1.203973
-1.579304 -1.203973
-2.339832 -1.203973
-3.219005 -1.203973
For x = 0.9
approx value
-0.105263 -0.105361
-0.109836 -0.105361
-0.110007 -0.105361
-0.110007 -0.105361
What could be the cause of this?
The goal is to compare the real ln and ln(x) computed using the
following sequence :
http://xs135.xs.to/xs135/09033/hw623.gif
And here's my try:
#include <stdio.h>
#include <math.h>
float power(float n, float m)
{
int i;
float p;
p = 1;
for (i = 1; i <= m; i++)
p = p * n;
return p;
}
float seqsum(int n, float x)
{
int i;
float r,s;
s = (x - 1) / (x + 1);
for (i = 1; i < n; i++)
s = s + (power(x - 1, 2 * i + 1)) / 3 * (power(x + 1, 2 * i +
1));
return s;
}
main()
{
float d,e;
int i;
printf("approx\t\tvalue\n");
for (i = 1; i <= 8; i = i * 2)
{
d = seqsum(i,0.1);
e = log(0.1);
printf("%f\t%f\n", d,e);
}
}
But the output values are horribly wrong for some inputs:
For x = 0.1
approx value
-1.636364 -2.302585
-2.283230 -2.302585
-3.538600 -2.302585
-5.902889 -2.302585
For x = 0.3
approx value
-1.076923 -1.203973
-1.579304 -1.203973
-2.339832 -1.203973
-3.219005 -1.203973
For x = 0.9
approx value
-0.105263 -0.105361
-0.109836 -0.105361
-0.110007 -0.105361
-0.110007 -0.105361
What could be the cause of this?