tfloatnan.nim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. discard """
  2. output: '''
  3. Nim: nan
  4. Nim: nan (float)
  5. Nim: nan (double)
  6. '''
  7. """
  8. let f = NaN
  9. echo "Nim: ", f
  10. let f32: float32 = NaN
  11. echo "Nim: ", f32, " (float)"
  12. let f64: float64 = NaN
  13. echo "Nim: ", f64, " (double)"
  14. block: # bug #10305
  15. # with `-O3 -ffast-math`, generated C/C++ code is not nan compliant
  16. # user can pass `--passC:-ffast-math` if he doesn't care.
  17. proc fun() =
  18. # this was previously failing at compile time with a nim compiler
  19. # that was compiled with `nim cpp -d:release`
  20. let a1 = 0.0
  21. let a = 0.0/a1
  22. let b1 = a == 0.0
  23. let b2 = a == a
  24. doAssert not b1
  25. doAssert not b2
  26. proc fun2(i: int) =
  27. # this was previously failing simply with `nim cpp -d:release`; the
  28. # difference with above example is that optimization (const folding) can't
  29. # take place in this example to hide the non-compliant nan bug.
  30. let a = 0.0/(i.float)
  31. let b1 = a == 0.0
  32. let b2 = a == a
  33. doAssert not b1
  34. doAssert not b2
  35. static: fun()
  36. fun()
  37. fun2(0)
  38. template main() =
  39. # xxx move all tests under here
  40. block: # bug #16469
  41. let a1 = 0.0
  42. let a2 = -0.0
  43. let a3 = 1.0 / a1
  44. let a4 = 1.0 / a2
  45. doAssert a3 == Inf
  46. doAssert a4 == -Inf
  47. doAssert $(a1, a2, a3, a4) == "(0.0, -0.0, inf, -inf)"
  48. static: main()
  49. main()