tfloatmod.nim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. discard """
  2. targets: "c c++ js"
  3. output: "ok"
  4. exitcode: "0"
  5. """
  6. # Test `mod` on float64 both at compiletime and at runtime
  7. import math
  8. # Testdata from golang
  9. const testValues: array[10, tuple[f64, expected: float64]] = [
  10. (4.9790119248836735e+00, 4.197615023265299782906368e-02),
  11. (7.7388724745781045e+00, 2.261127525421895434476482e+00),
  12. (-2.7688005719200159e-01, 3.231794108794261433104108e-02),
  13. (-5.0106036182710749e+00, 4.989396381728925078391512e+00),
  14. (9.6362937071984173e+00, 3.637062928015826201999516e-01),
  15. (2.9263772392439646e+00, 1.220868282268106064236690e+00),
  16. (5.2290834314593066e+00, 4.770916568540693347699744e+00),
  17. (2.7279399104360102e+00, 1.816180268691969246219742e+00),
  18. (1.8253080916808550e+00, 8.734595415957246977711748e-01),
  19. (-8.6859247685756013e+00, 1.314075231424398637614104e+00)]
  20. const simpleTestData = [
  21. (5.0, 3.0, 2.0),
  22. (5.0, -3.0, 2.0),
  23. (-5.0, 3.0, -2.0),
  24. (-5.0, -3.0, -2.0),
  25. (10.0, 1.0, 0.0),
  26. (10.0, 0.5, 0.0),
  27. (10.0, 1.5, 1.0),
  28. (-10.0, 1.0, -0.0),
  29. (-10.0, 0.5, -0.0),
  30. (-10.0, 1.5, -1.0),
  31. (1.5, 1.0, 0.5),
  32. (1.25, 1.0, 0.25),
  33. (1.125, 1.0, 0.125)
  34. ]
  35. const specialCases = [
  36. (-Inf, -Inf, Nan),
  37. (-Inf, -Pi, Nan),
  38. (-Inf, 0.0, Nan),
  39. (-Inf, Pi, Nan),
  40. (-Inf, Inf, Nan),
  41. (-Inf, Nan, Nan),
  42. (-PI, -Inf, -PI),
  43. (-PI, 0.0, Nan),
  44. (-PI, Inf, -PI),
  45. (-PI, Nan, Nan),
  46. (-0.0, -Inf, -0.0),
  47. (-0.0, 0.0, Nan),
  48. (-0.0, Inf, -0.0),
  49. (-0.0, Nan, Nan),
  50. (0.0, -Inf, 0.0),
  51. (0.0, 0.0, Nan),
  52. (0.0, Inf, 0.0),
  53. (0.0, Nan, Nan),
  54. (PI, -Inf, PI),
  55. (PI, 0.0, Nan),
  56. (PI, Inf, PI),
  57. (PI, Nan, Nan),
  58. (Inf, -Inf, Nan),
  59. (Inf, -PI, Nan),
  60. (Inf, 0.0, Nan),
  61. (Inf, PI, Nan),
  62. (Inf, Inf, Nan),
  63. (Inf, Nan, Nan),
  64. (Nan, -Inf, Nan),
  65. (Nan, -PI, Nan),
  66. (Nan, 0.0, Nan),
  67. (Nan, PI, Nan),
  68. (Nan, Inf, Nan),
  69. (Nan, Nan, Nan)]
  70. const extremeValues = [
  71. (5.9790119248836734e+200, 1.1258465975523544, 0.6447968302508578),
  72. (1.0e-100, 1.0e100, 1.0e-100)]
  73. proc errmsg(x, y, r, expected: float64): string =
  74. $x & " mod " & $y & " == " & $r & " but expected " & $expected
  75. proc golangtest() =
  76. let x = 10.0
  77. for tpl in testValues:
  78. let (y, expected) = tpl
  79. let r = x mod y
  80. doAssert(r == expected, errmsg(x, y, r, expected))
  81. proc simpletest() =
  82. for tpl in simpleTestData:
  83. let(x, y, expected) = tpl
  84. let r = x mod y
  85. doAssert(r == expected, errmsg(x, y, r, expected))
  86. proc testSpecialCases() =
  87. proc isnan(f: float64): bool =
  88. case classify(f)
  89. of fcNan:
  90. result = true
  91. else:
  92. result = false
  93. for tpl in specialCases:
  94. let(x, y, expected) = tpl
  95. let r = x mod y
  96. doAssert((r == expected) or (r.isnan and expected.isnan),
  97. errmsg(x, y, r, expected))
  98. proc testExtremeValues() =
  99. for tpl in extremeValues:
  100. let (x, y, expected) = tpl
  101. let r = x mod y
  102. doAssert(r == expected, errmsg(x, y, r, expected))
  103. static:
  104. # compiletime evaluation
  105. golangtest()
  106. simpletest()
  107. testSpecialCases()
  108. testExtremeValues()
  109. proc main() =
  110. # runtime evaluation
  111. golangtest()
  112. simpletest()
  113. testSpecialCases()
  114. testExtremeValues()
  115. main()
  116. echo "ok"