00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <stdio.h>
00017 #include <math.h>
00018 #include <grass/gis.h>
00019
00020 #ifndef ABS
00021 # define ABS(a) ((a) > 0 ? (a) : -(a))
00022 #endif
00023
00024 #define EP .0000000001
00025
00026 double sphere_volume(double dimension)
00027 {
00028 double log_gamma, log_volume;
00029 log_gamma = lgamma(dimension/2.0 + 1);
00030 log_volume = dimension/2.0 * log(M_PI) - log_gamma;
00031 return exp(log_volume);
00032 }
00033
00034 int main()
00035 {
00036 double dim=0, delta=1;
00037 while(ABS(delta) > EP)
00038 if(sphere_volume(dim + delta) > sphere_volume(dim))
00039 dim += delta;
00040 else
00041 delta /= -2;
00042 fprintf(stdout, "max volume = %.10f at dimension %.10f\n",
00043 sphere_volume(dim), dim);
00044 return 0;
00045 }