tfloatrange.nim 915 B

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