summary refs log tree commit diff stats
path: root/tests/template/t2do.nim
blob: b87e3328c2bff800e4716f26aee72ebcfdfef842 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
discard """
  output: "8.0"
"""

# bug #2057

proc mpf_get_d(x: int): float = float(x)
proc mpf_cmp_d(a: int; b: float): int = 0

template toFloatHelper(result: expr; tooSmall, tooLarge: stmt) {.immediate.} =
  result = mpf_get_d(a)
  if result == 0.0 and mpf_cmp_d(a,0.0) != 0:
    tooSmall
  if result == Inf:
    tooLarge

proc toFloat*(a: int): float =
  toFloatHelper(result)
    do: raise newException(ValueError, "number too small"):
        raise newException(ValueError, "number too large")

echo toFloat(8)