Reputation: 5444
I ve to create a pi approximation based on Archimedes equation. I ve to do so with for mode and recursive mode. In for mode i ve create sth like the above:
double Pi_approximation(double r, double L){
int i;
double fin;
double y;
for(i=1; i<4; i++){
y =sqrt(2*((r*r) - r*(sqrt(4*((r*r) - (L))))));
L = y;
printf("%f \n", L);
}
return y;
}
My problem is in second loop of for. In the first y is calculated normally but in second loop when i print y and L, it prints me their pointer!! Any idea?
Upvotes: 0
Views: 1097
Reputation: 8704
you probably corrupted the code with memory leaks or similar in some other part... The code alone seems to work.
Upvotes: 0
Reputation: 612954
It's hard to be sure what is happening, but I will take a guess anyway! Most likely the value you pass to one of the sqrt()
calls is negative. When this happens, y
(and hence L
) will be NaN
. The output then will be compiler dependent. For example on my compiler the output is:
-1.#IND00
Upvotes: 4