fix floats.h bug, fix std bug, add tests for std.
This commit is contained in:
parent
6e228dcc6b
commit
f65699a688
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,4 +1,4 @@
|
||||||
#include <double.h>
|
#include <float.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
@ -161,8 +161,7 @@ double array_std(double* array, int length)
|
||||||
double mean = array_mean(array, length);
|
double mean = array_mean(array, length);
|
||||||
double std = 0.0;
|
double std = 0.0;
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
std += (array[i] - mean);
|
std += (array[i] - mean) * (array[i] - mean);
|
||||||
std *= std;
|
|
||||||
}
|
}
|
||||||
std = sqrt(std / length);
|
std = sqrt(std / length);
|
||||||
return std;
|
return std;
|
||||||
|
|
|
@ -36,6 +36,9 @@ format: $(SRC)
|
||||||
run: $(SRC) $(OUTPUT)
|
run: $(SRC) $(OUTPUT)
|
||||||
./$(OUTPUT)
|
./$(OUTPUT)
|
||||||
|
|
||||||
|
verify: $(SRC) $(OUTPUT)
|
||||||
|
./$(OUTPUT) | grep "NOT passed" || true
|
||||||
|
|
||||||
time-linux:
|
time-linux:
|
||||||
@echo "Requires /bin/time, found on GNU/Linux systems" && echo
|
@echo "Requires /bin/time, found on GNU/Linux systems" && echo
|
||||||
|
|
||||||
|
|
31
test/test.c
31
test/test.c
|
@ -4,7 +4,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#define N 100
|
#define N 1000 * 1000
|
||||||
|
#define PERCENTAGE_TOLERANCE 1.0/1000.0
|
||||||
|
|
||||||
void test_unit_uniform(uint64_t* seed){
|
void test_unit_uniform(uint64_t* seed){
|
||||||
double* unit_uniform_array = malloc(sizeof(double) * N);
|
double* unit_uniform_array = malloc(sizeof(double) * N);
|
||||||
|
@ -24,13 +25,13 @@ void test_unit_uniform(uint64_t* seed){
|
||||||
printf("Mean of unit uniform: %f, vs expected mean: %f, delta: %f\n", mean, expected_mean, delta_mean);
|
printf("Mean of unit uniform: %f, vs expected mean: %f, delta: %f\n", mean, expected_mean, delta_mean);
|
||||||
printf("Std of unit uniform: %f, vs expected std: %f, delta: %f\n", std, expected_std, delta_std);
|
printf("Std of unit uniform: %f, vs expected std: %f, delta: %f\n", std, expected_std, delta_std);
|
||||||
|
|
||||||
if(fabs(delta_mean) > 1.0/1000.0){
|
if(fabs(delta_mean) > PERCENTAGE_TOLERANCE){
|
||||||
printf("[-] Mean test for unit uniform NOT passed.\n");
|
printf("[-] Mean test for unit uniform NOT passed.\n");
|
||||||
}else {
|
}else {
|
||||||
printf("[x] Mean test for unit uniform PASSED\n");
|
printf("[x] Mean test for unit uniform PASSED\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(fabs(delta_std) > 1.0/1000.0){
|
if(fabs(delta_std) > PERCENTAGE_TOLERANCE){
|
||||||
printf("[-] Std test for unit uniform NOT passed.\n");
|
printf("[-] Std test for unit uniform NOT passed.\n");
|
||||||
}else {
|
}else {
|
||||||
printf("[x] Std test for unit uniform PASSED\n");
|
printf("[x] Std test for unit uniform PASSED\n");
|
||||||
|
@ -56,22 +57,22 @@ void test_uniform(double start, double end, uint64_t* seed){
|
||||||
double delta_std = std - expected_std;
|
double delta_std = std - expected_std;
|
||||||
|
|
||||||
double width = fabs(end - start);
|
double width = fabs(end - start);
|
||||||
if(fabs(delta_mean) > width * 1.0/1000.0){
|
if(fabs(delta_mean) > width * PERCENTAGE_TOLERANCE){
|
||||||
printf("[-] Mean test for [%.1f, %.1f] uniform NOT passed.\n", start, end);
|
printf("[-] Mean test for [%.1f, %.1f] uniform NOT passed.\n", start, end);
|
||||||
printf("Mean of [%.1f, %.1f] uniform: %f, vs expected mean: %f, delta: %f\n", start, end, mean, expected_mean, mean - expected_mean);
|
printf("Mean of [%.1f, %.1f] uniform: %f, vs expected mean: %f, delta: %f\n", start, end, mean, expected_mean, mean - expected_mean);
|
||||||
}else {
|
}else {
|
||||||
printf("[x] Mean test for unit uniform PASSED\n");
|
printf("[x] Mean test for [%.1f, %.1f] uniform PASSED\n", start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(fabs(delta_std) > width * 1.0/1000.0){
|
if(fabs(delta_std) > width * PERCENTAGE_TOLERANCE){
|
||||||
printf("[-] Std test for [%.1f, %.1f] uniform NOT passed.\n", start, end);
|
printf("[-] Std test for [%.1f, %.1f] uniform NOT passed.\n", start, end);
|
||||||
printf("Std of [%.1f, %.1f] uniform: %f, vs expected std: %f, delta: %f\n", start, end, std, expected_std, std - expected_std);
|
printf("Std of [%.1f, %.1f] uniform: %f, vs expected std: %f, delta: %f\n", start, end, std, expected_std, std - expected_std);
|
||||||
for(int i=0; i<N; i++){
|
for(int i=0; i<10; i++){
|
||||||
|
|
||||||
printf("%.1f, ", uniform_array[i]);
|
printf("%.1f, ", uniform_array[i]);
|
||||||
}
|
}
|
||||||
}else {
|
}else {
|
||||||
printf("[x] Std test for unit uniform PASSED\n");
|
printf("[x] Std test for [%.1f, %.1f] uniform PASSED\n", start, end);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
@ -82,15 +83,27 @@ int main(){
|
||||||
uint64_t* seed = malloc(sizeof(uint64_t));
|
uint64_t* seed = malloc(sizeof(uint64_t));
|
||||||
*seed = 1000; // xorshift can't start with a seed of 0
|
*seed = 1000; // xorshift can't start with a seed of 0
|
||||||
|
|
||||||
|
printf("Testing unit uniform\n");
|
||||||
test_unit_uniform(seed);
|
test_unit_uniform(seed);
|
||||||
|
|
||||||
for(int i=0; i<1; i++){
|
printf("Testing small uniforms\n");
|
||||||
|
for(int i=0; i<100; i++){
|
||||||
double start = sample_uniform(-10, 10, seed);
|
double start = sample_uniform(-10, 10, seed);
|
||||||
double end = sample_uniform(-10, 10, seed);
|
double end = sample_uniform(-10, 10, seed);
|
||||||
if ( end > start){
|
if ( end > start){
|
||||||
test_uniform(start, end, seed);
|
test_uniform(start, end, seed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("Testing wide uniforms\n");
|
||||||
|
for(int i=0; i<100; i++){
|
||||||
|
double start = sample_uniform(-1000 * 1000, 1000 * 1000, seed);
|
||||||
|
double end = sample_uniform(-1000 * 1000, 1000 * 1000, seed);
|
||||||
|
if ( end > start){
|
||||||
|
test_uniform(start, end, seed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
free(seed);
|
free(seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user