Add experiment testing the effects of adding extra bits to cordic
authorMichael Nolan <mtnolan2640@gmail.com>
Tue, 14 Apr 2020 13:46:23 +0000 (09:46 -0400)
committerMichael Nolan <mtnolan2640@gmail.com>
Tue, 14 Apr 2020 13:47:05 +0000 (09:47 -0400)
src/ieee754/cordic/test/test_software.py [new file with mode: 0644]

diff --git a/src/ieee754/cordic/test/test_software.py b/src/ieee754/cordic/test/test_software.py
new file mode 100644 (file)
index 0000000..d48eefb
--- /dev/null
@@ -0,0 +1,31 @@
+import math
+import unittest
+from python_sin_cos import run_cordic
+import random
+
+class SoftwareTestCase(unittest.TestCase):
+    def test_extrabits(self):
+        fracbits = 16
+        extrabits = 18
+        M = (1 << fracbits)
+        print(M)
+        for i in range(200000):
+            f = random.uniform(-math.pi/2, math.pi/2)
+            i = int(round(f * M))
+            f = i/M
+            print(f"int: {i}, float:{f}")
+            i = i * (1<<extrabits)
+
+            expected = int(round(math.sin(f) * M))
+
+            sin, cos = run_cordic(i,
+                                  fracbits=(fracbits+extrabits),
+                                  log=False)
+            sin = int(round(sin / (1<<extrabits)))
+
+            print(f"expected: {expected}, actual: {sin}")
+            self.assertEqual(expected, sin)
+
+
+if __name__ == '__main__':
+    unittest.main()