summary refs log tree commit diff stats
path: root/tests/stdlib/tfrexp1.nim
diff options
context:
space:
mode:
authorskilchen <skilchen@users.noreply.github.com>2017-12-21 16:32:26 +0100
committerAndreas Rumpf <rumpf_a@web.de>2017-12-21 16:32:26 +0100
commita89b81eb9679d1351aceae766e5eaf1bf8faeffe (patch)
treeabd0a50e56d2fd479ee8ace883c4f9c1547261c4 /tests/stdlib/tfrexp1.nim
parent8c0e73479e0ec31fdda83751400f8b515e8e43d7 (diff)
downloadNim-a89b81eb9679d1351aceae766e5eaf1bf8faeffe.tar.gz
fixes #6353 (#6951)
Diffstat (limited to 'tests/stdlib/tfrexp1.nim')
-rw-r--r--tests/stdlib/tfrexp1.nim44
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/stdlib/tfrexp1.nim b/tests/stdlib/tfrexp1.nim
new file mode 100644
index 000000000..c6bb2b38c
--- /dev/null
+++ b/tests/stdlib/tfrexp1.nim
@@ -0,0 +1,44 @@
+discard """
+  targets: "js c c++"
+  output: '''ok'''
+"""
+
+import math
+import strformat
+
+const manualTest = false
+
+proc frexp_test(lo, hi, step: float64) =
+  var exp: int
+  var frac: float64
+
+  var eps = 1e-15.float64
+
+  var x:float64 = lo
+  while x <= hi:
+    frac = frexp(x.float, exp)
+    let rslt = pow(2.0, float(exp)) * frac
+
+    doAssert(abs(rslt - x) < eps)
+
+    when manualTest:
+      echo fmt("x: {x:10.3f} exp: {exp:4d} frac: {frac:24.20f} check: {$(abs(rslt - x) < eps):-5s} {rslt: 9.3f}")
+    x += step
+
+when manualTest:
+  var exp: int
+  var frac: float64
+
+  for flval in [1.7976931348623157e+308, -1.7976931348623157e+308, # max, min float64
+                3.4028234663852886e+38, -3.4028234663852886e+38,   # max, min float32
+                4.9406564584124654e-324, -4.9406564584124654e-324, # smallest/largest positive/negative float64
+                1.4012984643248171e-45, -1.4012984643248171e-45,   # smallest/largest positive/negative float32
+                2.2250738585072014e-308, 1.1754943508222875e-38]:  # smallest normal float64/float32
+    frac = frexp(flval, exp)
+    echo fmt("{flval:25.16e}, {exp: 6d}, {frac: .20f} {frac * pow(2.0, float(exp)): .20e}")
+
+  frexp_test(-1000.0, 1000.0, 0.0125)
+else:
+  frexp_test(-1000000.0, 1000000.0, 0.125)
+
+echo "ok"