Loading lang_c_10_maths...
#include <stdio.h>
void test_maths(void) { printf("\n~~~~ %s() ~~~~\n", __func__); }
int main(void) { test_maths(); return 0; }
<math.h>.
printf("cos(%g)=%g\n", x, cos(x));
<math.h>une macro M_PI qui décrit cette valeur ; seulement la norme du langage C ne la mentionne pas et selon les options de compilations cette macro sera rendue visible ou non✍
#ifndef M_PI # define M_PI 3.14159265358979323846 #endif
<math.h>), mais à l'édition de liens.
LDFLAGS=doit être complétée pour contenir
LDFLAGS=-lmCette variable du fichier GNUmakefile est transmise à la commande invoquant l'éditeur de liens.
void RealArray_minMax(const double *values, int count, double *out_minValue, double *out_maxValue) { double minValue=DBL_MAX, maxValue=-DBL_MAX; for(int i=0; i<count; ++i) { double value=values[i]; minValue=fmin(minValue, value); maxValue=fmax(maxValue, value); } //-- store out-parameters -- *out_minValue=minValue; *out_maxValue=maxValue; }
void test_realArray(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values[80]; int count=(int)(sizeof(values)/sizeof(values[0])); for(int i=0; i<count; ++i) { values[i]=4.0*M_PI*i/count+M_PI/4.0; } double minValue, maxValue; RealArray_minMax(values, count, &minValue, &maxValue); printf("min=%g max=%g\n", minValue, maxValue); }
void RealArray_display(const double *values, int count, double minValue, double maxValue) { double valueRange=maxValue-minValue; int first=8, last=70, range=last-first+1; int zero=first+(int)round(range*(-minValue)/valueRange); for(int i=0; i<count; ++i) { double value=values[i]; int step=first+(int)round(range*(value-minValue)/valueRange); char symbol=step>zero ? '>' : step<zero ? '<' : 'X'; for(int s=0; s<=78; ++s) { char c=' '; if(step==s) { c=symbol; } else if(s==zero) { c='|'; } else if(((step<=s)&&(s<=zero))||((zero<=s)&&(s<=step))) { c='~'; } fputc(c, stdout); } fputc('\n', stdout); } }
void RealArray_sin(double *values, int count) { for(int i=0; i<count; ++i) { values[i]=sin(values[i]); } }
{ ... printf("sin():\n"); RealArray_sin(values, count); RealArray_minMax(values, count, &minValue, &maxValue); printf("min=%g max=%g\n", minValue, maxValue); RealArray_display(values, count, minValue, maxValue); ... }
void RealArray_sqrt(double *values, int count) { for(int i=0; i<count; ++i) { values[i]=sqrt(values[i]); } }
{ ... printf("sqrt(sin()):\n"); RealArray_sqrt(values, count); RealArray_minMax(values, count, &minValue, &maxValue); printf("min=%g max=%g\n", minValue, maxValue); RealArray_display(values, count, minValue, maxValue); ... }
{ ... for(int i=0; i<count; ++i) { printf("%g ", values[i]); } printf("\n"); ... }
{ ... int step=zero; char symbol='?'; if(isfinite(value)) { step=first+(int)round(range*(value-minValue)/valueRange); symbol=step>zero ? '>' : step<zero ? '<' : 'X'; } ... }
void test_rand(void) { printf("\n~~~~ %s() ~~~~\n", __func__); for(int i=0; i<10; ++i) { printf("%d\n", rand()); } }
<stdlib.h>.
<stdlib.h>, sert ce propos.
<time.h>déclare la fonction standard time() qui fournit une valeur qui évolue à chaque seconde.
{ ... srand((unsigned int)time(NULL)); ... }
<stdlib.h>.
{ ... for(int i=0; i<100; ++i) { printf("%d ", rand()%10); } printf("\n"); ... }
{ ... int successCount=0, drawCount=1000000; for(int i=0; i<drawCount; ++i) { if((rand()%10)<3) { ++successCount; } } printf("%d successes out of %d draws\n", successCount, drawCount); ... }
double // random value in [0.0,1.0) Random_uniform(void) { return rand()/(RAND_MAX+1.0); }
void test_distribution(void) { printf("\n~~~~ %s() ~~~~\n", __func__); int drawCount=1000000; double values[40]={0.0}; int count=(int)(sizeof(values)/sizeof(values[0])); printf("uniform distribution:\n"); for(int i=0; i<drawCount; ++i) { int r=(int)(count*Random_uniform()); values[r]+=1.0/drawCount; } RealArray_display(values, count, 0.0, 1.0/count); }
double // random value with mean 0.0 and standard-deviation 1.0 Random_gaussian(void) { double r1=Random_uniform(); double r2=Random_uniform(); return sin(2.0*M_PI*r1)*sqrt(-2.0*log(1.0-r2)); // Box-Muller method }
{ ... printf("gaussian distribution:\n"); for(int i=0; i<count; ++i) { values[i]=0.0; } double mean=0.5*count; double stdDev=0.15*count; for(int i=0; i<drawCount; ++i) { int r=(int)(mean+stdDev*Random_gaussian()); if((r>=0)&&(r<count)) { values[r]+=1.0/drawCount; } } RealArray_display(values, count, 0.0, 1.0/sqrt(2.0*M_PI*stdDev*stdDev)); ... }
<math.h>et nécessite l'édition de liens explicite avec la bibliothèque mathématique standard.
<stdlib.h>.
//----------------------------------------------------------------------------
#ifndef REALARRAY_H #define REALARRAY_H
void RealArray_minMax(const double *values, int count, double *out_minValue, double *out_maxValue);
void RealArray_display(const double *values, int count, double minValue, double maxValue);
void RealArray_sin(double *values, int count);
void RealArray_sqrt(double *values, int count);
#endif // REALARRAY_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "realArray.h" #include <math.h> #include <float.h> #include <stdio.h>
void RealArray_minMax(const double *values, int count, double *out_minValue, double *out_maxValue) { double minValue=DBL_MAX, maxValue=-DBL_MAX; for(int i=0; i<count; ++i) { double value=values[i]; minValue=fmin(minValue, value); maxValue=fmax(maxValue, value); } //-- store out-parameters -- *out_minValue=minValue; *out_maxValue=maxValue; }
void RealArray_display(const double *values, int count, double minValue, double maxValue) { double valueRange=maxValue-minValue; int first=8, last=70, range=last-first+1; int zero=first+(int)round(range*(-minValue)/valueRange); for(int i=0; i<count; ++i) { double value=values[i]; #if 0 // first version int step=first+(int)round(range*(value-minValue)/valueRange); char symbol=step>zero ? '>' : step<zero ? '<' : 'X'; #else // second version int step=zero; char symbol='?'; if(isfinite(value)) { step=first+(int)round(range*(value-minValue)/valueRange); symbol=step>zero ? '>' : step<zero ? '<' : 'X'; } #endif for(int s=0; s<=78; ++s) { char c=' '; if(step==s) { c=symbol; } else if(s==zero) { c='|'; } else if(((step<=s)&&(s<=zero))||((zero<=s)&&(s<=step))) { c='~'; } fputc(c, stdout); } fputc('\n', stdout); } }
void RealArray_sin(double *values, int count) { for(int i=0; i<count; ++i) { values[i]=sin(values[i]); } }
void RealArray_sqrt(double *values, int count) { for(int i=0; i<count; ++i) { values[i]=sqrt(values[i]); } }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#ifndef RANDOM_H #define RANDOM_H
double // random value in [0.0,1.0) Random_uniform(void);
double // random value with mean 0.0 and standard-deviation 1.0 Random_gaussian(void);
#endif // RANDOM_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "random.h" #include <stdlib.h> #include <math.h>
#ifndef M_PI # define M_PI 3.14159265358979323846 #endif
double // random value in [0.0,1.0) Random_uniform(void) { return rand()/(RAND_MAX+1.0); }
double // random value with mean 0.0 and standard-deviation 1.0 Random_gaussian(void) { double r1=Random_uniform(); double r2=Random_uniform(); return sin(2.0*M_PI*r1)*sqrt(-2.0*log(1.0-r2)); // Box-Muller method }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <time.h> #include "realArray.h" #include "random.h"
#ifndef M_PI # define M_PI 3.14159265358979323846 #endif
void test_maths(double x) { printf("\n~~~~ %s() ~~~~\n", __func__); printf("cos(%g)=%g\n", x, cos(x)); printf("sqrt(2.0)=%g\n", sqrt(2.0)); printf("exp(1.0)=%g\n", exp(1.0)); printf("log10(0.01)=%g\n", log10(0.01)); printf("atan2(-1.0,1.0)=%g\n", atan2(-1.0,1.0)); }
void test_realArray(void) { printf("\n~~~~ %s() ~~~~\n", __func__); double values[80]; int count=(int)(sizeof(values)/sizeof(values[0])); for(int i=0; i<count; ++i) { values[i]=4.0*M_PI*i/count+M_PI/4.0; } double minValue, maxValue; RealArray_minMax(values, count, &minValue, &maxValue); printf("min=%g max=%g\n", minValue, maxValue); RealArray_display(values, count, minValue, maxValue); printf("sin():\n"); RealArray_sin(values, count); RealArray_minMax(values, count, &minValue, &maxValue); printf("min=%g max=%g\n", minValue, maxValue); RealArray_display(values, count, minValue, maxValue); printf("sqrt(sin()):\n"); RealArray_sqrt(values, count); RealArray_minMax(values, count, &minValue, &maxValue); printf("min=%g max=%g\n", minValue, maxValue); RealArray_display(values, count, minValue, maxValue); for(int i=0; i<count; ++i) { printf("%g ", values[i]); } printf("\n"); }
void test_rand(void) { printf("\n~~~~ %s() ~~~~\n", __func__); for(int i=0; i<10; ++i) { printf("%d\n", rand()); } for(int i=0; i<100; ++i) { printf("%d ", rand()%10); } printf("\n"); int successCount=0, drawCount=1000000; for(int i=0; i<drawCount; ++i) { if((rand()%10)<3) { ++successCount; } } printf("%d successes out of %d draws\n", successCount, drawCount); }
void test_distribution(void) { printf("\n~~~~ %s() ~~~~\n", __func__); int drawCount=1000000; double values[40]={0.0}; int count=(int)(sizeof(values)/sizeof(values[0])); printf("uniform distribution:\n"); for(int i=0; i<drawCount; ++i) { int r=(int)(count*Random_uniform()); values[r]+=1.0/drawCount; } RealArray_display(values, count, 0.0, 1.0/count); printf("gaussian distribution:\n"); for(int i=0; i<count; ++i) { values[i]=0.0; } double mean=0.5*count; double stdDev=0.15*count; for(int i=0; i<drawCount; ++i) { int r=(int)(mean+stdDev*Random_gaussian()); if((r>=0)&&(r<count)) { values[r]+=1.0/drawCount; } } RealArray_display(values, count, 0.0, 1.0/sqrt(2.0*M_PI*stdDev*stdDev)); }
int main(void) { srand((unsigned int)time(NULL)); test_maths(M_PI/3.0); test_realArray(); test_rand(); test_distribution(); return 0; }
//----------------------------------------------------------------------------