tmath.nim 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. discard """
  2. action: run
  3. output: '''
  4. [Suite] random int
  5. [Suite] random float
  6. [Suite] cumsum
  7. [Suite] random sample
  8. [Suite] ^
  9. '''
  10. """
  11. import math, random, os
  12. import unittest
  13. import sets, tables
  14. suite "random int":
  15. test "there might be some randomness":
  16. var set = initHashSet[int](128)
  17. for i in 1..1000:
  18. incl(set, rand(high(int)))
  19. check len(set) == 1000
  20. test "single number bounds work":
  21. var rand: int
  22. for i in 1..1000:
  23. rand = rand(1000)
  24. check rand < 1000
  25. check rand > -1
  26. test "slice bounds work":
  27. var rand: int
  28. for i in 1..1000:
  29. rand = rand(100..1000)
  30. check rand < 1000
  31. check rand >= 100
  32. test " again gives new numbers":
  33. var rand1 = rand(1000000)
  34. os.sleep(200)
  35. var rand2 = rand(1000000)
  36. check rand1 != rand2
  37. suite "random float":
  38. test "there might be some randomness":
  39. var set = initHashSet[float](128)
  40. for i in 1..100:
  41. incl(set, rand(1.0))
  42. check len(set) == 100
  43. test "single number bounds work":
  44. var rand: float
  45. for i in 1..1000:
  46. rand = rand(1000.0)
  47. check rand < 1000.0
  48. check rand > -1.0
  49. test "slice bounds work":
  50. var rand: float
  51. for i in 1..1000:
  52. rand = rand(100.0..1000.0)
  53. check rand < 1000.0
  54. check rand >= 100.0
  55. test " again gives new numbers":
  56. var rand1:float = rand(1000000.0)
  57. os.sleep(200)
  58. var rand2:float = rand(1000000.0)
  59. check rand1 != rand2
  60. suite "cumsum":
  61. test "cumsum int seq return":
  62. let counts = [ 1, 2, 3, 4 ]
  63. check counts.cumsummed == [ 1, 3, 6, 10 ]
  64. test "cumsum float seq return":
  65. let counts = [ 1.0, 2.0, 3.0, 4.0 ]
  66. check counts.cumsummed == [ 1.0, 3.0, 6.0, 10.0 ]
  67. test "cumsum int in-place":
  68. var counts = [ 1, 2, 3, 4 ]
  69. counts.cumsum
  70. check counts == [ 1, 3, 6, 10 ]
  71. test "cumsum float in-place":
  72. var counts = [ 1.0, 2.0, 3.0, 4.0 ]
  73. counts.cumsum
  74. check counts == [ 1.0, 3.0, 6.0, 10.0 ]
  75. suite "random sample":
  76. test "non-uniform array sample unnormalized int CDF":
  77. let values = [ 10, 20, 30, 40, 50 ] # values
  78. let counts = [ 4, 3, 2, 1, 0 ] # weights aka unnormalized probabilities
  79. var histo = initCountTable[int]()
  80. let cdf = counts.cumsummed # unnormalized CDF
  81. for i in 0 ..< 5000:
  82. histo.inc(sample(values, cdf))
  83. check histo.len == 4 # number of non-zero in `counts`
  84. # Any one bin is a binomial random var for n samples, each with prob p of
  85. # adding a count to k; E[k]=p*n, Var k=p*(1-p)*n, approximately Normal for
  86. # big n. So, P(abs(k - p*n)/sqrt(p*(1-p)*n))>3.0) =~ 0.0027, while
  87. # P(wholeTestFails) =~ 1 - P(binPasses)^4 =~ 1 - (1-0.0027)^4 =~ 0.01.
  88. for i, c in counts:
  89. if c == 0:
  90. check values[i] notin histo
  91. continue
  92. let p = float(c) / float(cdf[^1])
  93. let n = 5000.0
  94. let expected = p * n
  95. let stdDev = sqrt(n * p * (1.0 - p))
  96. check abs(float(histo[values[i]]) - expected) <= 3.0 * stdDev
  97. test "non-uniform array sample normalized float CDF":
  98. let values = [ 10, 20, 30, 40, 50 ] # values
  99. let counts = [ 0.4, 0.3, 0.2, 0.1, 0 ] # probabilities
  100. var histo = initCountTable[int]()
  101. let cdf = counts.cumsummed # normalized CDF
  102. for i in 0 ..< 5000:
  103. histo.inc(sample(values, cdf))
  104. check histo.len == 4 # number of non-zero in ``counts``
  105. for i, c in counts:
  106. if c == 0:
  107. check values[i] notin histo
  108. continue
  109. let p = float(c) / float(cdf[^1])
  110. let n = 5000.0
  111. let expected = p * n
  112. let stdDev = sqrt(n * p * (1.0 - p))
  113. # NOTE: like unnormalized int CDF test, P(wholeTestFails) =~ 0.01.
  114. check abs(float(histo[values[i]]) - expected) <= 3.0 * stdDev
  115. suite "^":
  116. test "compiles for valid types":
  117. check: compiles(5 ^ 2)
  118. check: compiles(5.5 ^ 2)
  119. check: compiles(5.5 ^ 2.int8)
  120. check: compiles(5.5 ^ 2.uint)
  121. check: compiles(5.5 ^ 2.uint8)
  122. check: not compiles(5.5 ^ 2.2)
  123. block:
  124. when not defined(js):
  125. # Check for no side effect annotation
  126. proc mySqrt(num: float): float {.noSideEffect.} =
  127. return sqrt(num)
  128. # check gamma function
  129. assert(gamma(5.0) == 24.0) # 4!
  130. assert(lgamma(1.0) == 0.0) # ln(1.0) == 0.0
  131. assert(erf(6.0) > erf(5.0))
  132. assert(erfc(6.0) < erfc(5.0))
  133. # Function for approximate comparison of floats
  134. proc `==~`(x, y: float): bool = (abs(x-y) < 1e-9)
  135. block: # prod
  136. doAssert prod([1, 2, 3, 4]) == 24
  137. doAssert prod([1.5, 3.4]) == 5.1
  138. let x: seq[float] = @[]
  139. doAssert prod(x) == 1.0
  140. block: # round() tests
  141. # Round to 0 decimal places
  142. doAssert round(54.652) ==~ 55.0
  143. doAssert round(54.352) ==~ 54.0
  144. doAssert round(-54.652) ==~ -55.0
  145. doAssert round(-54.352) ==~ -54.0
  146. doAssert round(0.0) ==~ 0.0
  147. block: # splitDecimal() tests
  148. doAssert splitDecimal(54.674).intpart ==~ 54.0
  149. doAssert splitDecimal(54.674).floatpart ==~ 0.674
  150. doAssert splitDecimal(-693.4356).intpart ==~ -693.0
  151. doAssert splitDecimal(-693.4356).floatpart ==~ -0.4356
  152. doAssert splitDecimal(0.0).intpart ==~ 0.0
  153. doAssert splitDecimal(0.0).floatpart ==~ 0.0
  154. block: # trunc tests for vcc
  155. doAssert(trunc(-1.1) == -1)
  156. doAssert(trunc(1.1) == 1)
  157. doAssert(trunc(-0.1) == -0)
  158. doAssert(trunc(0.1) == 0)
  159. #special case
  160. doAssert(classify(trunc(1e1000000)) == fcInf)
  161. doAssert(classify(trunc(-1e1000000)) == fcNegInf)
  162. doAssert(classify(trunc(0.0/0.0)) == fcNan)
  163. doAssert(classify(trunc(0.0)) == fcZero)
  164. #trick the compiler to produce signed zero
  165. let
  166. f_neg_one = -1.0
  167. f_zero = 0.0
  168. f_nan = f_zero / f_zero
  169. doAssert(classify(trunc(f_neg_one*f_zero)) == fcNegZero)
  170. doAssert(trunc(-1.1'f32) == -1)
  171. doAssert(trunc(1.1'f32) == 1)
  172. doAssert(trunc(-0.1'f32) == -0)
  173. doAssert(trunc(0.1'f32) == 0)
  174. doAssert(classify(trunc(1e1000000'f32)) == fcInf)
  175. doAssert(classify(trunc(-1e1000000'f32)) == fcNegInf)
  176. doAssert(classify(trunc(f_nan.float32)) == fcNan)
  177. doAssert(classify(trunc(0.0'f32)) == fcZero)
  178. block: # sgn() tests
  179. assert sgn(1'i8) == 1
  180. assert sgn(1'i16) == 1
  181. assert sgn(1'i32) == 1
  182. assert sgn(1'i64) == 1
  183. assert sgn(1'u8) == 1
  184. assert sgn(1'u16) == 1
  185. assert sgn(1'u32) == 1
  186. assert sgn(1'u64) == 1
  187. assert sgn(-12342.8844'f32) == -1
  188. assert sgn(123.9834'f64) == 1
  189. assert sgn(0'i32) == 0
  190. assert sgn(0'f32) == 0
  191. assert sgn(NegInf) == -1
  192. assert sgn(Inf) == 1
  193. assert sgn(NaN) == 0
  194. block: # fac() tests
  195. try:
  196. discard fac(-1)
  197. except AssertionDefect:
  198. discard
  199. doAssert fac(0) == 1
  200. doAssert fac(1) == 1
  201. doAssert fac(2) == 2
  202. doAssert fac(3) == 6
  203. doAssert fac(4) == 24
  204. block: # floorMod/floorDiv
  205. doAssert floorDiv(8, 3) == 2
  206. doAssert floorMod(8, 3) == 2
  207. doAssert floorDiv(8, -3) == -3
  208. doAssert floorMod(8, -3) == -1
  209. doAssert floorDiv(-8, 3) == -3
  210. doAssert floorMod(-8, 3) == 1
  211. doAssert floorDiv(-8, -3) == 2
  212. doAssert floorMod(-8, -3) == -2
  213. doAssert floorMod(8.0, -3.0) ==~ -1.0
  214. doAssert floorMod(-8.5, 3.0) ==~ 0.5
  215. block: # log
  216. doAssert log(4.0, 3.0) ==~ ln(4.0) / ln(3.0)
  217. doAssert log2(8.0'f64) == 3.0'f64
  218. doAssert log2(4.0'f64) == 2.0'f64
  219. doAssert log2(2.0'f64) == 1.0'f64
  220. doAssert log2(1.0'f64) == 0.0'f64
  221. doAssert classify(log2(0.0'f64)) == fcNegInf
  222. doAssert log2(8.0'f32) == 3.0'f32
  223. doAssert log2(4.0'f32) == 2.0'f32
  224. doAssert log2(2.0'f32) == 1.0'f32
  225. doAssert log2(1.0'f32) == 0.0'f32
  226. doAssert classify(log2(0.0'f32)) == fcNegInf