summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorFabian Keller <bluenote10@users.noreply.github.com>2017-06-20 12:11:09 +0200
committerAndreas Rumpf <rumpf_a@web.de>2017-06-20 12:11:09 +0200
commita6e0494a6fc6149a53908cf5c58da58679a539c8 (patch)
treebcb2661bfbf7b72a9f571538c6ac121d461df2e1
parent62ffac25dcf243f6b877c677bd90fe15035db2e4 (diff)
downloadNim-a6e0494a6fc6149a53908cf5c58da58679a539c8.tar.gz
fixes #5966
-rw-r--r--lib/pure/math.nim11
-rw-r--r--lib/pure/random.nim3
-rw-r--r--tests/stdlib/tmath.nim23
3 files changed, 31 insertions, 6 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim
index a1b2690a2..8037b31b0 100644
--- a/lib/pure/math.nim
+++ b/lib/pure/math.nim
@@ -235,7 +235,7 @@ when not defined(JS):
         x = x and (not (1'u64 shl (64'u64-12'u64-e) - 1'u64))
 
       result = cast[float64](x)
-    
+
     proc truncImpl(f: float32): float32 =
       const
         mask : uint32 = 0xFF
@@ -255,7 +255,7 @@ when not defined(JS):
         x = x and (not (1'u32 shl (32'u32-9'u32-e) - 1'u32))
 
       result = cast[float32](x)
-      
+
     proc trunc*(x: float64): float64 =
       if classify(x) in {fcZero, fcNegZero, fcNan, fcInf, fcNegInf}: return x
       result = truncImpl(x)
@@ -413,10 +413,13 @@ proc `mod`*[T: float32|float64](x, y: T): T =
 {.pop.}
 {.pop.}
 
-proc `^`*[T](x, y: T): T =
+proc `^`*[T](x: T, y: Natural): T =
   ## Computes ``x`` to the power ``y`. ``x`` must be non-negative, use
   ## `pow <#pow,float,float>` for negative exponents.
-  assert y >= T(0)
+  when compiles(y >= T(0)):
+    assert y >= T(0)
+  else:
+    assert T(y) >= T(0)
   var (x, y) = (x, y)
   result = 1
 
diff --git a/lib/pure/random.nim b/lib/pure/random.nim
index 1f750edcd..8a32f7d9a 100644
--- a/lib/pure/random.nim
+++ b/lib/pure/random.nim
@@ -123,7 +123,8 @@ when not defined(nimscript):
       proc getMil(t: Time): int {.importcpp: "getTime", nodecl.}
       randomize(getMil times.getTime())
     else:
-      randomize(int times.getTime())
+      let time = int(times.epochTime() * 1_000_000_000)
+      randomize(time)
 
 {.pop.}
 
diff --git a/tests/stdlib/tmath.nim b/tests/stdlib/tmath.nim
index 538582ba8..581308a7e 100644
--- a/tests/stdlib/tmath.nim
+++ b/tests/stdlib/tmath.nim
@@ -1,4 +1,15 @@
-import math, random
+discard """
+  action: run
+  output: '''[Suite] random int
+
+[Suite] random float
+
+[Suite] ^
+
+'''
+"""
+
+import math, random, os
 import unittest
 import sets
 
@@ -26,6 +37,7 @@ suite "random int":
   test "randomize() again gives new numbers":
     randomize()
     var rand1 = random(1000000)
+    os.sleep(200)
     randomize()
     var rand2 = random(1000000)
     check rand1 != rand2
@@ -55,7 +67,16 @@ suite "random float":
   test "randomize() again gives new numbers":
     randomize()
     var rand1:float = random(1000000.0)
+    os.sleep(200)
     randomize()
     var rand2:float = random(1000000.0)
     check rand1 != rand2
 
+suite "^":
+  test "compiles for valid types":
+    check: compiles(5 ^ 2)
+    check: compiles(5.5 ^ 2)
+    check: compiles(5.5 ^ 2.int8)
+    check: compiles(5.5 ^ 2.uint)
+    check: compiles(5.5 ^ 2.uint8)
+    check: not compiles(5.5 ^ 2.2)
\ No newline at end of file