Loading lang_c_11_fnctptr...
double Integrate_sqrt(double xMin, double xMax, int stepCount) { double dx=(xMax-xMin)/stepCount, sum=0.0, x=xMin+0.5*dx; for(int i=0; i<stepCount; ++i, x+=dx) { double value=sqrt(x); if(isfinite(value)) { sum+=value; } } return dx*sum; }
<math.h>: la variable LDFLAGS du fichier GNUmakefile devra à nouveau être complétée avec l'option -lm (voir cette section). Le programme principal sera rédigé dans le fichier prog11.c :
#include <stdio.h> #include "integrate.h"
void test_Integrate_math(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double xMin=0.0, xMax=1.0; int stepCount=1000; printf("Integrate_sqrt() in [%g;%g] --> %g\n", xMin, xMax, Integrate_sqrt(xMin, xMax, stepCount)); }
int main(void) { test_Integrate_math(); return 0; }
double nom_de_la_fonction(double)
double (*nom_de_la_variable)(double);
... { double (*math1)(double)=NULL; // la variable math1 ne désigne aucune fonction mathématique double (*math2)(double)=sqrt; // la variable math2 désigne la fonction mathématique sqrt() double (*math3)(double)=log; // la variable math3 désigne la fonction mathématique log() int (*cmp1)(const char *, const char *)=NULL; // la variable cmp1 ne désigne aucune fonction int (*cmp2)(const char *, const char *)=strcmp; // la variable cmp2 désigne la fonction strcmp() ... }
&sqrt,
&log...) mais cette notation n'apporte rien dans le cas présent.
... { ... if(math3) // si une fonction est désignée par ce pointeur, nous pouvons l'invoquer { y=math3(x); // en lui transmettant ses paramètres et obtenir son résultat } if(cmp2) // même chose avec un autre type de pointeur sur fonctions { r=cmp2(str1, str2); } ... }
... { ... y=(*math3)(x); ... r=(*cmp2)(str1, str2); ... }
double Integrate_math(double xMin, double xMax, int stepCount, double (*fnct)(double)) { double dx=(xMax-xMin)/stepCount, sum=0.0, x=xMin+0.5*dx; for(int i=0; i<stepCount; ++i, x+=dx) { double value=fnct(x); if(isfinite(value)) { sum+=value; } } return dx*sum; }
... { ... printf("Integrate_math(sqrt) in [%g;%g] --> %g\n", xMin, xMax, Integrate_math(xMin, xMax, stepCount, sqrt)); printf("Integrate_math(log) in [%g;%g] --> %g\n", xMin, xMax, Integrate_math(xMin, xMax, stepCount, log)); ... }
#ifndef M_PI # define M_PI 3.14159265358979323846 #endif
double unitGaussian(double x) { double mean=0.0, standardDeviation=1.0; x-=mean; double factor=(1.0/sqrt(2.0*M_PI))/standardDeviation; double exponent=-(x*x)/(2.0*standardDeviation*standardDeviation); return factor*exp(exponent); }
{ ... printf("Integrate_math(unitGaussian) in [%g;%g] --> %g\n", xMin, xMax, Integrate_math(xMin, xMax, stepCount, unitGaussian)); ... }
double nom_de_la_fonction(double, void *)
double (*nom_de_la_variable)(double, void *);
typedef double (*nom_du_type)(double, void *);
void une_fonction(nom_du_type fnct, void *params);
... { nom_du_type variable; ... }
typedef struct { double mean, standardDeviation; } GaussianParameters;
double gaussian(double x, void *mathParams) { const GaussianParameters gp=*(const GaussianParameters *)mathParams; x-=gp.mean; double factor=(1.0/sqrt(2.0*M_PI))/gp.standardDeviation; double exponent=-(x*x)/(2.0*gp.standardDeviation*gp.standardDeviation); return factor*exp(exponent); }
typedef double (*ParamMathFnct)(double, void *);
double Integrate_param(double xMin, double xMax, int stepCount, ParamMathFnct fnct, void *mathParams) { double dx=(xMax-xMin)/stepCount, sum=0.0, x=xMin+0.5*dx; for(int i=0; i<stepCount; ++i, x+=dx) { double value=fnct(x, mathParams); if(isfinite(value)) { sum+=value; } } return dx*sum; }
void test_Integrate_param(double mean, double standardDeviation) { printf("\n~~~~ %s(%g, %g) ~~~~\n", __func__, mean, standardDeviation); double xMin=0.0, xMax=1.0; int stepCount=1000; GaussianParameters gp={mean, standardDeviation}; printf("Integrate_param(gaussian) in [%g;%g] --> %g\n", xMin, xMax, Integrate_param(xMin, xMax, stepCount, gaussian, &gp)); }
//----------------------------------------------------------------------------
#ifndef INTEGRATE_H #define INTEGRATE_H
typedef double (*ParamMathFnct)(double, void *);
double Integrate_sqrt(double xMin, double xMax, int stepCount);
double Integrate_log(double xMin, double xMax, int stepCount);
double Integrate_math(double xMin, double xMax, int stepCount, double (*fnct)(double));
double Integrate_param(double xMin, double xMax, int nbSteps, ParamMathFnct fnct, void *mathParams);
#endif // INTEGRATE_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "integrate.h" #include <math.h>
double Integrate_sqrt(double xMin, double xMax, int stepCount) { double dx=(xMax-xMin)/stepCount, sum=0.0, x=xMin+0.5*dx; for(int i=0; i<stepCount; ++i, x+=dx) { double value=sqrt(x); if(isfinite(value)) { sum+=value; } } return dx*sum; }
double Integrate_log(double xMin, double xMax, int stepCount) { double dx=(xMax-xMin)/stepCount, sum=0.0, x=xMin+0.5*dx; for(int i=0; i<stepCount; ++i, x+=dx) { double value=log(x); if(isfinite(value)) { sum+=value; } } return dx*sum; }
double Integrate_math(double xMin, double xMax, int stepCount, double (*fnct)(double)) { double dx=(xMax-xMin)/stepCount, sum=0.0, x=xMin+0.5*dx; for(int i=0; i<stepCount; ++i, x+=dx) { double value=fnct(x); if(isfinite(value)) { sum+=value; } } return dx*sum; }
double Integrate_param(double xMin, double xMax, int stepCount, ParamMathFnct fnct, void *mathParams) { double dx=(xMax-xMin)/stepCount, sum=0.0, x=xMin+0.5*dx; for(int i=0; i<stepCount; ++i, x+=dx) { double value=fnct(x, mathParams); if(isfinite(value)) { sum+=value; } } return dx*sum; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include <stdio.h> #include <math.h> #include "integrate.h"
#ifndef M_PI # define M_PI 3.14159265358979323846 #endif
double unitGaussian(double x) { double mean=0.0, standardDeviation=1.0; x-=mean; double factor=(1.0/sqrt(2.0*M_PI))/standardDeviation; double exponent=-(x*x)/(2.0*standardDeviation*standardDeviation); return factor*exp(exponent); }
void test_Integrate_math(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double xMin=0.0, xMax=1.0; int stepCount=1000; printf("Integrate_sqrt() in [%g;%g] --> %g\n", xMin, xMax, Integrate_sqrt(xMin, xMax, stepCount)); printf("Integrate_log() in [%g;%g] --> %g\n", xMin, xMax, Integrate_log(xMin, xMax, stepCount)); printf("Integrate_math(sqrt) in [%g;%g] --> %g\n", xMin, xMax, Integrate_math(xMin, xMax, stepCount, sqrt)); printf("Integrate_math(log) in [%g;%g] --> %g\n", xMin, xMax, Integrate_math(xMin, xMax, stepCount, log)); printf("Integrate_math(unitGaussian) in [%g;%g] --> %g\n", xMin, xMax, Integrate_math(xMin, xMax, stepCount, unitGaussian)); }
typedef struct { double mean, standardDeviation; } GaussianParameters;
double gaussian(double x, void *mathParams) { const GaussianParameters gp=*(const GaussianParameters *)mathParams; x-=gp.mean; double factor=(1.0/sqrt(2.0*M_PI))/gp.standardDeviation; double exponent=-(x*x)/(2.0*gp.standardDeviation*gp.standardDeviation); return factor*exp(exponent); }
void test_Integrate_param(double mean, double standardDeviation) { printf("\n~~~~ %s(%g, %g) ~~~~\n", __func__, mean, standardDeviation); double xMin=0.0, xMax=1.0; int stepCount=1000; GaussianParameters gp={mean, standardDeviation}; printf("Integrate_param(gaussian) in [%g;%g] --> %g\n", xMin, xMax, Integrate_param(xMin, xMax, stepCount, gaussian, &gp)); }
int main(void) { test_Integrate_math(); test_Integrate_param(0.0, 1.0); test_Integrate_param(1.0, 0.5); return 0; }
//----------------------------------------------------------------------------