tfloatrange.nim 900 B

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