Skip to content

Instantly share code, notes, and snippets.

@andrequeiroz
Created October 8, 2017 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrequeiroz/a1e6f2c72d3b872eccaa0f172d65195f to your computer and use it in GitHub Desktop.
Save andrequeiroz/a1e6f2c72d3b872eccaa0f172d65195f to your computer and use it in GitHub Desktop.
C program that calculates the normal cumulative distribution function values
// compilation: gcc -Wall -std=c99 -lm normal_table.c -o normal_table
#include <stdio.h>
#include <time.h>
#include <math.h>
#define pi 3.14159265359
float valor(double [], double);
int main(void) {
unsigned short int a = 16807;
unsigned long int m = 2147483647, x = time(NULL), t = time(NULL);
double u[250000];
for (int i = 0; i < 250000; i++) {
x = (a * x) % m;
u[i] = (double) x / m;
}
FILE *arq = fopen("/tmp/table", "w");
for (int i = 0; i < 10; i++)
fprintf(arq, "\t%.2f", (float) i / 100);
for (int i = 0; i < 30; i++) {
fprintf(arq, "\n%.1f", (float) i / 10);
for (int j = 0; j < 10; j++)
fprintf(arq, "\t%.4f", valor(u, (float) i / 10 + (float) j / 100));
}
printf("Table generation time: %ldmin %lds\n", (time(NULL) - t) / 60,
(time(NULL) - t) % 60);
return 0;
}
float valor(double y[], double b) {
double *p, s = 0;
for (p = &y[0]; p < &y[250000]; p++)
s += exp(-pow((*p * b), 2) / 2) * b / sqrt(2 * pi);
s /= 250000;
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment