This commit is contained in:
Fabian Keller 2017-06-20 12:11:09 +02:00 • committed by Andreas Rumpf
commit a6e0494a6f
3 changed files with 31 additions and 6 deletions

View file

@ -413,10 +413,13 @@ proc `mod`*[T: float32|float64](x, y: T): T =
{.pop.} {.pop.}
{.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 ## Computes ``x`` to the power ``y`. ``x`` must be non-negative, use
## `pow <#pow,float,float>` for negative exponents. ## `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) var (x, y) = (x, y)
result = 1 result = 1

View file

@ -123,7 +123,8 @@ when not defined(nimscript):
proc getMil(t: Time): int {.importcpp: "getTime", nodecl.} proc getMil(t: Time): int {.importcpp: "getTime", nodecl.}
randomize(getMil times.getTime()) randomize(getMil times.getTime())
else: else:
randomize(int times.getTime()) let time = int(times.epochTime() * 1_000_000_000)
randomize(time)
{.pop.} {.pop.}

View file

@ -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 unittest
import sets import sets
@ -26,6 +37,7 @@ suite "random int":
test "randomize() again gives new numbers": test "randomize() again gives new numbers":
randomize() randomize()
var rand1 = random(1000000) var rand1 = random(1000000)
os.sleep(200)
randomize() randomize()
var rand2 = random(1000000) var rand2 = random(1000000)
check rand1 != rand2 check rand1 != rand2
@ -55,7 +67,16 @@ suite "random float":
test "randomize() again gives new numbers": test "randomize() again gives new numbers":
randomize() randomize()
var rand1:float = random(1000000.0) var rand1:float = random(1000000.0)
os.sleep(200)
randomize() randomize()
var rand2:float = random(1000000.0) var rand2:float = random(1000000.0)
check rand1 != rand2 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)