summaryrefslogtreecommitdiffstats
path: root/examples/using_double_on_avr/test_conversions.c
diff options
context:
space:
mode:
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-09-13 12:59:31 +0300
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>2013-09-13 12:59:31 +0300
commitf47410ea4b8ae43e19facd378be4cf1073e1813b (patch)
tree53fdc4bac6615f07e830d7d538c90abbea1ead4b /examples/using_double_on_avr/test_conversions.c
parentfd9a79a06db00c6199a5dcaee22ed2cd8e3c3e9b (diff)
Move examples into subfolders, add READMEs
Diffstat (limited to 'examples/using_double_on_avr/test_conversions.c')
-rw-r--r--examples/using_double_on_avr/test_conversions.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/examples/using_double_on_avr/test_conversions.c b/examples/using_double_on_avr/test_conversions.c
new file mode 100644
index 00000000..22620a6a
--- /dev/null
+++ b/examples/using_double_on_avr/test_conversions.c
@@ -0,0 +1,56 @@
+#include "double_conversion.h"
+#include <math.h>
+#include <stdio.h>
+
+static const double testvalues[] = {
+ 0.0, -0.0, 0.1, -0.1,
+ M_PI, -M_PI, 123456.789, -123456.789,
+ INFINITY, -INFINITY, NAN, INFINITY - INFINITY,
+ 1e38, -1e38, 1e39, -1e39,
+ 1e-38, -1e-38, 1e-39, -1e-39,
+ 3.14159e-37,-3.14159e-37, 3.14159e-43, -3.14159e-43,
+ 1e-60, -1e-60, 1e-45, -1e-45,
+ 0.99999999999999, -0.99999999999999, 127.999999999999, -127.999999999999
+};
+
+#define TESTVALUES_COUNT (sizeof(testvalues)/sizeof(testvalues[0]))
+
+int main()
+{
+ int status = 0;
+ int i;
+ for (i = 0; i < TESTVALUES_COUNT; i++)
+ {
+ double orig = testvalues[i];
+ float expected_float = (float)orig;
+ double expected_double = (double)expected_float;
+
+ float got_float = double_to_float(*(uint64_t*)&orig);
+ uint64_t got_double = float_to_double(got_float);
+
+ uint32_t e1 = *(uint32_t*)&expected_float;
+ uint32_t g1 = *(uint32_t*)&got_float;
+ uint64_t e2 = *(uint64_t*)&expected_double;
+ uint64_t g2 = got_double;
+
+ if (g1 != e1)
+ {
+ printf("%3d double_to_float fail: %08x != %08x\n", i, g1, e1);
+ status = 1;
+ }
+
+ if (g2 != e2)
+ {
+ printf("%3d float_to_double fail: %016llx != %016llx\n", i,
+ (unsigned long long)g2,
+ (unsigned long long)e2);
+ status = 1;
+ }
+ }
+
+ return status;
+}
+
+
+
+