tfloats.nim 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. #[
  5. xxx merge all or most float tests into this file
  6. ]#
  7. import std/[fenv, math, strutils]
  8. import stdtest/testutils
  9. proc equalsOrNaNs(a, b: float): bool =
  10. if isNaN(a): isNaN(b)
  11. elif a == 0:
  12. b == 0 and signbit(a) == signbit(b)
  13. else:
  14. a == b
  15. template reject(a) =
  16. doAssertRaises(ValueError): discard parseFloat(a)
  17. template main =
  18. block:
  19. proc test(a: string, b: float) =
  20. let a2 = a.parseFloat
  21. doAssert equalsOrNaNs(a2, b), $(a, a2, b)
  22. test "0.00_0001", 1E-6
  23. test "0.00__00_01", 1E-6
  24. test "0.0_01", 0.001
  25. test "0.00_000_1", 1E-6
  26. test "0.00000_1", 1E-6
  27. test "1_0.00_0001", 10.000001
  28. test "1__00.00_0001", 1_00.000001
  29. test "inf", Inf
  30. test "-inf", -Inf
  31. test "-Inf", -Inf
  32. test "-INF", -Inf
  33. test "NaN", NaN
  34. test "-nan", NaN
  35. test ".1", 0.1
  36. test "-.1", -0.1
  37. test "-0", -0.0
  38. test "-0", -0'f # see #18246, -0 won't work
  39. test ".1e-1", 0.1e-1
  40. test "0_1_2_3.0_1_2_3E+0_1_2", 123.0123e12
  41. test "0_1_2.e-0", 12e0
  42. test "0_1_2e-0", 12e0
  43. test "-0e0", -0.0
  44. test "-0e-0", -0.0
  45. reject "a"
  46. reject ""
  47. reject "e1"
  48. reject "infa"
  49. reject "infe1"
  50. reject "_"
  51. reject "1e"
  52. when false: # gray area; these numbers should probably be invalid
  53. reject "1_"
  54. reject "1_.0"
  55. reject "1.0_"
  56. block: # bugs mentioned in https://github.com/nim-lang/Nim/pull/18504#issuecomment-881635317
  57. block: # example 1
  58. let a = 0.1+0.2
  59. doAssert a != 0.3
  60. doAssert $a == "0.30000000000000004"
  61. block: # example 2
  62. const a = 0.1+0.2
  63. doAssert $($a, a) == """("0.30000000000000004", 0.30000000000000004)"""
  64. block: # example 3
  65. const a1 = 0.1+0.2
  66. let a2 = a1
  67. doAssert a1 != 0.3
  68. doAssert $[$a1, $a2] == """["0.30000000000000004", "0.30000000000000004"]"""
  69. when true:
  70. block: # bug #18148
  71. var a = 1.1'f32
  72. doAssert $a == "1.1", $a # was failing
  73. block: # bug #18400
  74. block:
  75. let a1 = 0.1'f32
  76. let a2 = 0.2'f32
  77. let a3 = a1 + a2
  78. var s = ""
  79. s.addFloat(a3)
  80. whenVMorJs: discard # xxx refs #12884
  81. do:
  82. doAssert a3 == 0.3'f32
  83. doAssert $a3 == "0.3"
  84. block:
  85. let a1 = 0.1
  86. let a2 = 0.2
  87. let a3 = a1 + a2
  88. var s = ""
  89. s.addFloat(a3)
  90. doAssert a3 != 0.3
  91. doAssert $a3 == "0.30000000000000004"
  92. block:
  93. var s = [-13.888888'f32]
  94. whenRuntimeJs: discard
  95. do:
  96. doAssert $s == "[-13.888888]"
  97. doAssert $s[0] == "-13.888888"
  98. block: # bug #7717
  99. proc test(f: float) =
  100. let f2 = $f
  101. let f3 = parseFloat(f2)
  102. doAssert equalsOrNaNs(f, f3), $(f, f2, f3)
  103. test 1.0 + epsilon(float64)
  104. test 1000000.0000000123
  105. test log2(100000.0)
  106. test maximumPositiveValue(float32)
  107. test maximumPositiveValue(float64)
  108. test minimumPositiveValue(float32)
  109. test minimumPositiveValue(float64)
  110. block: # bug #12884
  111. block: # example 1
  112. const x0: float32 = 1.32
  113. let x1 = 1.32
  114. let x2 = 1.32'f32
  115. var x3: float32 = 1.32
  116. doAssert $(x0, x1, x2, x3) == "(1.32, 1.32, 1.32, 1.32)"
  117. block: # example https://github.com/nim-lang/Nim/issues/12884#issuecomment-564967962
  118. let x = float(1.32'f32)
  119. when nimvm: discard # xxx prints 1.3
  120. else:
  121. when not defined(js):
  122. doAssert $x == "1.3200000524520874"
  123. doAssert $1.32 == "1.32"
  124. doAssert $1.32'f32 == "1.32"
  125. let x2 = 1.32'f32
  126. doAssert $x2 == "1.32"
  127. block:
  128. var x = 1.23456789012345'f32
  129. when nimvm:
  130. discard # xxx, refs #12884
  131. else:
  132. doAssert x == 1.2345679'f32
  133. doAssert $x == "1.2345679"
  134. static: main()
  135. main()