1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
discard """
cmd: "nim c -d:release --rangeChecks:on $file"
disabled: "windows"
output: '''StrictPositiveRange
float
range fail expected
range fail expected
'''
"""
import math, fenv
type
Positive = range[0.0..Inf]
StrictPositive = range[minimumPositiveValue(float)..Inf]
Negative32 = range[-maximumPositiveValue(float32) .. -1.0'f32]
proc myoverload(x: float) =
echo "float"
proc myoverload(x: Positive) =
echo "PositiveRange"
proc myoverload(x: StrictPositive) =
echo "StrictPositiveRange"
let x = 9.0.StrictPositive
myoverload(x)
myoverload(9.0)
doAssert(sqrt(x) == 3.0)
var z = -10.0
try:
myoverload(StrictPositive(z))
except:
echo "range fail expected"
proc strictOnlyProc(x: StrictPositive): bool =
if x > 1.0: true else: false
let x2 = 5.0.Positive
doAssert(strictOnlyProc(x2))
try:
let x4 = 0.0.Positive
discard strictOnlyProc(x4)
except:
echo "range fail expected"
|