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