J
John Smith
The exercise is to calculate the approximate area of a circle by the
"calculus method." That is, finding the area of rectangles nearly
filling the circle. I calculate the area of rectangles in 1/4 of the
circle and multiply by 4 to get the total area.
In the code below, if the value of nrect is used in the for loop, the
value of area goes to "nan" in the last couple of iterations. When there
are slightly fewer iterations than the value of nrect, the output is OK.
This behaviour is the same for smaller values of nrect.
I'm not sure I understand nan (I know it means not-a-number). It seems
to have something to do with non-representable numbers or domain/range
errors but reading the relavent section in Harbison & Steele didn't
entirely enlighten me. Can someone explain nan and its relation to the
problem with this code?
/* Calculate the area of a circle and the value of Pi
using the "calculus method." */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
double sinx, angle, nrect, ht;
double len, area, radius, newht;
int idx;
if(argc != 2)
{
fprintf(stderr, "enter the radius of a circle\n");
exit(EXIT_FAILURE);
}
area = 0.0;
radius = strtod(argv[1], NULL);
nrect = 1000.0;
ht = radius / nrect;
newht = ht;
//for(idx = 0; idx <= nrect; idx++) /* output is nan */
for(idx = 0; idx <= 997; idx++) /* output is OK */
{
sinx = newht / radius; /* sine=opp side/hypot */
angle = tan(asin(sinx)); /* angle=tangent of inverse sine */
len = newht / angle; /* len of adj side=opp side/angle */
area += (len * ht); /* add area of rectangle to total */
printf("area: %f rects: %d\n", area, idx); /* debug */
newht += ht; /* increment length of opp side */
}
area *= 4.0;
printf("\narea of circle with radius %.2f", radius);
printf(" is approx. %.2f\n", area);
printf("approx. value of Pi = %f\n", area / (radius * radius));
return 0;
}
"calculus method." That is, finding the area of rectangles nearly
filling the circle. I calculate the area of rectangles in 1/4 of the
circle and multiply by 4 to get the total area.
In the code below, if the value of nrect is used in the for loop, the
value of area goes to "nan" in the last couple of iterations. When there
are slightly fewer iterations than the value of nrect, the output is OK.
This behaviour is the same for smaller values of nrect.
I'm not sure I understand nan (I know it means not-a-number). It seems
to have something to do with non-representable numbers or domain/range
errors but reading the relavent section in Harbison & Steele didn't
entirely enlighten me. Can someone explain nan and its relation to the
problem with this code?
/* Calculate the area of a circle and the value of Pi
using the "calculus method." */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
double sinx, angle, nrect, ht;
double len, area, radius, newht;
int idx;
if(argc != 2)
{
fprintf(stderr, "enter the radius of a circle\n");
exit(EXIT_FAILURE);
}
area = 0.0;
radius = strtod(argv[1], NULL);
nrect = 1000.0;
ht = radius / nrect;
newht = ht;
//for(idx = 0; idx <= nrect; idx++) /* output is nan */
for(idx = 0; idx <= 997; idx++) /* output is OK */
{
sinx = newht / radius; /* sine=opp side/hypot */
angle = tan(asin(sinx)); /* angle=tangent of inverse sine */
len = newht / angle; /* len of adj side=opp side/angle */
area += (len * ht); /* add area of rectangle to total */
printf("area: %f rects: %d\n", area, idx); /* debug */
newht += ht; /* increment length of opp side */
}
area *= 4.0;
printf("\narea of circle with radius %.2f", radius);
printf(" is approx. %.2f\n", area);
printf("approx. value of Pi = %f\n", area / (radius * radius));
return 0;
}