From: Sam Moore Date: Wed, 26 Mar 2014 17:42:04 +0000 (+0800) Subject: Tester to compare custom Real to IEEE float X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=commitdiff_plain;h=95dd47f77239fd252e0292dca21baac29513bc7b;ds=sidebyside Tester to compare custom Real to IEEE float Also got the endianness right this time. I think. --- diff --git a/src/main.cpp b/src/main.cpp index 339ea8d..1b2073e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,7 +15,8 @@ int main(int argc, char ** argv) else { Debug("Add random object"); - doc.Add(RECT_FILLED, Rect(Random()*0.5, Random()*0.5, Random()*0.5, Random()*0.5)); + //doc.Add(RECT_FILLED, Rect(Random()*0.5, Random()*0.5, Random()*0.5, Random()*0.5)); + doc.Add(RECT_FILLED, Rect(0.25,0.25, 0.5, 0.5)); } MainLoop(doc); return 0; diff --git a/src/real.h b/src/real.h index 1d3fb8b..7b30949 100644 --- a/src/real.h +++ b/src/real.h @@ -7,8 +7,8 @@ namespace IPDF { //#define REAL_FLOAT -#define REAL_DOUBLE -//#define REAL_HALF +//#define REAL_DOUBLE +#define REAL_HALF #ifdef REAL_SINGLE typedef float Real; @@ -26,7 +26,7 @@ namespace IPDF // mask out extra bits in exponent //1000 1111 1000 0000 0000 0011 1111 1111 // Endianness matters - a &= 0xFF3008F8;//0x8F8003FF; + a &= 0xFFC001F1; //0x8F8003FF; } diff --git a/src/tests/realtofloat.cpp b/src/tests/realtofloat.cpp index b09bd49..32ee20b 100644 --- a/src/tests/realtofloat.cpp +++ b/src/tests/realtofloat.cpp @@ -1,9 +1,26 @@ #include "main.h" -float test = 1e4; +#include +float test_min = 0; +float test_max = 1e-6; +unsigned test_count = 100; +float error_thresh = (test_max - test_min)/1e1; int main(int argc, char ** argv) { - Real r(test); - Debug("test float %.20f", test); - Debug("test real %.20f", Float(r)); + Debug("TEST STARTING - Comparing Float(Real(test)) for %u trials between %.20f and %.20f", test_count, test_min, test_max); + float error; + for (unsigned i = 0; i < test_count; ++i) + { + float test = test_min + (test_max-test_min)*((float)(rand() % (int)1e6)/1e6); + Real real(test); + float thiserror = abs(test - real.value); + error += thiserror; + Debug("#%u: |test %.20f - real %.20f| = %.20f [mean %f]", i, test, real.value, thiserror, error/(i+1)); + } + + if (error/test_count > error_thresh) + { + Fatal("TEST FAILED - Average error %.20f exceeds threshold %.20f", error/test_count, error_thresh); + } + Debug("TEST SUCCEEDED - Average error %.20f is %f percent of range", error/test_count, 1e2*(error/test_count) / (test_max - test_min)); return 0; }