tmath.nim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. discard """
  2. targets: "c cpp js"
  3. matrix:"; -d:danger"
  4. """
  5. # xxx: there should be a test with `-d:nimTmathCase2 -d:danger --passc:-ffast-math`,
  6. # but it requires disabling certain lines with `when not defined(nimTmathCase2)`
  7. import std/math
  8. import std/assertions
  9. # Function for approximate comparison of floats
  10. proc `==~`(x, y: float): bool = abs(x - y) < 1e-9
  11. template main() =
  12. block:
  13. when not defined(js):
  14. # check for no side effect annotation
  15. proc mySqrt(num: float): float {.noSideEffect.} =
  16. # xxx unused
  17. sqrt(num)
  18. # check gamma function
  19. doAssert gamma(5.0) == 24.0 # 4!
  20. doAssert almostEqual(gamma(0.5), sqrt(PI))
  21. doAssert almostEqual(gamma(-0.5), -2 * sqrt(PI))
  22. doAssert lgamma(1.0) == 0.0 # ln(1.0) == 0.0
  23. doAssert almostEqual(lgamma(0.5), 0.5 * ln(PI))
  24. doAssert erf(6.0) > erf(5.0)
  25. doAssert erfc(6.0) < erfc(5.0)
  26. block: # sgn() tests
  27. doAssert sgn(1'i8) == 1
  28. doAssert sgn(1'i16) == 1
  29. doAssert sgn(1'i32) == 1
  30. doAssert sgn(1'i64) == 1
  31. doAssert sgn(1'u8) == 1
  32. doAssert sgn(1'u16) == 1
  33. doAssert sgn(1'u32) == 1
  34. doAssert sgn(1'u64) == 1
  35. doAssert sgn(-12342.8844'f32) == -1
  36. doAssert sgn(123.9834'f64) == 1
  37. doAssert sgn(0'i32) == 0
  38. doAssert sgn(0'f32) == 0
  39. doAssert sgn(-0.0'f64) == 0
  40. doAssert sgn(NegInf) == -1
  41. doAssert sgn(Inf) == 1
  42. doAssert sgn(NaN) == 0
  43. block: # fac() tests
  44. when nimvm: discard
  45. else:
  46. try:
  47. discard fac(-1)
  48. except AssertionDefect:
  49. discard
  50. doAssert fac(0) == 1
  51. doAssert fac(1) == 1
  52. doAssert fac(2) == 2
  53. doAssert fac(3) == 6
  54. doAssert fac(4) == 24
  55. doAssert fac(5) == 120
  56. block: # floorMod/floorDiv
  57. doAssert floorDiv(8, 3) == 2
  58. doAssert floorMod(8, 3) == 2
  59. doAssert floorDiv(8, -3) == -3
  60. doAssert floorMod(8, -3) == -1
  61. doAssert floorDiv(-8, 3) == -3
  62. doAssert floorMod(-8, 3) == 1
  63. doAssert floorDiv(-8, -3) == 2
  64. doAssert floorMod(-8, -3) == -2
  65. doAssert floorMod(8.0, -3.0) == -1.0
  66. doAssert floorMod(-8.5, 3.0) == 0.5
  67. block: # euclDiv/euclMod
  68. doAssert euclDiv(8, 3) == 2
  69. doAssert euclMod(8, 3) == 2
  70. doAssert euclDiv(8, -3) == -2
  71. doAssert euclMod(8, -3) == 2
  72. doAssert euclDiv(-8, 3) == -3
  73. doAssert euclMod(-8, 3) == 1
  74. doAssert euclDiv(-8, -3) == 3
  75. doAssert euclMod(-8, -3) == 1
  76. doAssert euclMod(8.0, -3.0) == 2.0
  77. doAssert euclMod(-8.5, 3.0) == 0.5
  78. doAssert euclDiv(9, 3) == 3
  79. doAssert euclMod(9, 3) == 0
  80. doAssert euclDiv(9, -3) == -3
  81. doAssert euclMod(9, -3) == 0
  82. doAssert euclDiv(-9, 3) == -3
  83. doAssert euclMod(-9, 3) == 0
  84. doAssert euclDiv(-9, -3) == 3
  85. doAssert euclMod(-9, -3) == 0
  86. block: # ceilDiv
  87. doAssert ceilDiv(8, 3) == 3
  88. doAssert ceilDiv(8, 4) == 2
  89. doAssert ceilDiv(8, 5) == 2
  90. doAssert ceilDiv(11, 3) == 4
  91. doAssert ceilDiv(12, 3) == 4
  92. doAssert ceilDiv(13, 3) == 5
  93. doAssert ceilDiv(41, 7) == 6
  94. doAssert ceilDiv(0, 1) == 0
  95. doAssert ceilDiv(1, 1) == 1
  96. doAssert ceilDiv(1, 2) == 1
  97. doAssert ceilDiv(2, 1) == 2
  98. doAssert ceilDiv(2, 2) == 1
  99. doAssert ceilDiv(0, high(int)) == 0
  100. doAssert ceilDiv(1, high(int)) == 1
  101. doAssert ceilDiv(0, high(int) - 1) == 0
  102. doAssert ceilDiv(1, high(int) - 1) == 1
  103. doAssert ceilDiv(high(int) div 2, high(int) div 2 + 1) == 1
  104. doAssert ceilDiv(high(int) div 2, high(int) div 2 + 2) == 1
  105. doAssert ceilDiv(high(int) div 2 + 1, high(int) div 2) == 2
  106. doAssert ceilDiv(high(int) div 2 + 2, high(int) div 2) == 2
  107. doAssert ceilDiv(high(int) div 2 + 1, high(int) div 2 + 1) == 1
  108. doAssert ceilDiv(high(int), 1) == high(int)
  109. doAssert ceilDiv(high(int) - 1, 1) == high(int) - 1
  110. doAssert ceilDiv(high(int) - 1, 2) == high(int) div 2
  111. doAssert ceilDiv(high(int) - 1, high(int)) == 1
  112. doAssert ceilDiv(high(int) - 1, high(int) - 1) == 1
  113. doAssert ceilDiv(high(int) - 1, high(int) - 2) == 2
  114. doAssert ceilDiv(high(int), high(int)) == 1
  115. doAssert ceilDiv(high(int), high(int) - 1) == 2
  116. doAssert ceilDiv(255'u8, 1'u8) == 255'u8
  117. doAssert ceilDiv(254'u8, 2'u8) == 127'u8
  118. when not defined(danger):
  119. doAssertRaises(AssertionDefect): discard ceilDiv(41, 0)
  120. doAssertRaises(AssertionDefect): discard ceilDiv(41, -1)
  121. doAssertRaises(AssertionDefect): discard ceilDiv(-1, 1)
  122. doAssertRaises(AssertionDefect): discard ceilDiv(-1, -1)
  123. doAssertRaises(AssertionDefect): discard ceilDiv(254'u8, 3'u8)
  124. doAssertRaises(AssertionDefect): discard ceilDiv(255'u8, 2'u8)
  125. block: # splitDecimal() tests
  126. doAssert splitDecimal(54.674).intpart == 54.0
  127. doAssert splitDecimal(54.674).floatpart ==~ 0.674
  128. doAssert splitDecimal(-693.4356).intpart == -693.0
  129. doAssert splitDecimal(-693.4356).floatpart ==~ -0.4356
  130. doAssert splitDecimal(0.0).intpart == 0.0
  131. doAssert splitDecimal(0.0).floatpart == 0.0
  132. block: # trunc tests for vcc
  133. doAssert trunc(-1.1) == -1
  134. doAssert trunc(1.1) == 1
  135. doAssert trunc(-0.1) == -0
  136. doAssert trunc(0.1) == 0
  137. # special case
  138. doAssert classify(trunc(1e1000000)) == fcInf
  139. doAssert classify(trunc(-1e1000000)) == fcNegInf
  140. when not defined(nimTmathCase2):
  141. doAssert classify(trunc(0.0/0.0)) == fcNan
  142. doAssert classify(trunc(0.0)) == fcZero
  143. # trick the compiler to produce signed zero
  144. let
  145. f_neg_one = -1.0
  146. f_zero = 0.0
  147. f_nan = f_zero / f_zero
  148. doAssert classify(trunc(f_neg_one*f_zero)) == fcNegZero
  149. doAssert trunc(-1.1'f32) == -1
  150. doAssert trunc(1.1'f32) == 1
  151. doAssert trunc(-0.1'f32) == -0
  152. doAssert trunc(0.1'f32) == 0
  153. doAssert classify(trunc(1e1000000'f32)) == fcInf
  154. doAssert classify(trunc(-1e1000000'f32)) == fcNegInf
  155. when not defined(nimTmathCase2):
  156. doAssert classify(trunc(f_nan.float32)) == fcNan
  157. doAssert classify(trunc(0.0'f32)) == fcZero
  158. block: # log
  159. doAssert log(4.0, 3.0) ==~ ln(4.0) / ln(3.0)
  160. doAssert log2(8.0'f64) == 3.0'f64
  161. doAssert log2(4.0'f64) == 2.0'f64
  162. doAssert log2(2.0'f64) == 1.0'f64
  163. doAssert log2(1.0'f64) == 0.0'f64
  164. doAssert classify(log2(0.0'f64)) == fcNegInf
  165. doAssert log2(8.0'f32) == 3.0'f32
  166. doAssert log2(4.0'f32) == 2.0'f32
  167. doAssert log2(2.0'f32) == 1.0'f32
  168. doAssert log2(1.0'f32) == 0.0'f32
  169. doAssert classify(log2(0.0'f32)) == fcNegInf
  170. block: # cumsum
  171. block: # cumsum int seq return
  172. let counts = [1, 2, 3, 4]
  173. doAssert counts.cumsummed == @[1, 3, 6, 10]
  174. let empty: seq[int] = @[]
  175. doAssert empty.cumsummed == @[]
  176. block: # cumsum float seq return
  177. let counts = [1.0, 2.0, 3.0, 4.0]
  178. doAssert counts.cumsummed == @[1.0, 3.0, 6.0, 10.0]
  179. let empty: seq[float] = @[]
  180. doAssert empty.cumsummed == @[]
  181. block: # cumsum int in-place
  182. var counts = [1, 2, 3, 4]
  183. counts.cumsum
  184. doAssert counts == [1, 3, 6, 10]
  185. var empty: seq[int] = @[]
  186. empty.cumsum
  187. doAssert empty == @[]
  188. block: # cumsum float in-place
  189. var counts = [1.0, 2.0, 3.0, 4.0]
  190. counts.cumsum
  191. doAssert counts == [1.0, 3.0, 6.0, 10.0]
  192. var empty: seq[float] = @[]
  193. empty.cumsum
  194. doAssert empty == @[]
  195. block: # ^ compiles for valid types
  196. doAssert: compiles(5 ^ 2)
  197. doAssert: compiles(5.5 ^ 2)
  198. doAssert: compiles(5.5 ^ 2.int8)
  199. doAssert: compiles(5.5 ^ 2.uint)
  200. doAssert: compiles(5.5 ^ 2.uint8)
  201. doAssert: not compiles(5.5 ^ 2.2)
  202. block: # isNaN
  203. doAssert NaN.isNaN
  204. doAssert not Inf.isNaN
  205. doAssert isNaN(Inf - Inf)
  206. doAssert not isNaN(0.0)
  207. doAssert not isNaN(3.1415926)
  208. doAssert not isNaN(0'f32)
  209. block: # signbit
  210. doAssert not signbit(0.0)
  211. doAssert signbit(-0.0)
  212. doAssert signbit(-0.1)
  213. doAssert not signbit(0.1)
  214. doAssert not signbit(Inf)
  215. doAssert signbit(-Inf)
  216. doAssert not signbit(NaN)
  217. let x1 = NaN
  218. let x2 = -NaN
  219. let x3 = -x1
  220. doAssert isNaN(x1)
  221. doAssert isNaN(x2)
  222. doAssert isNaN(x3)
  223. doAssert not signbit(x1)
  224. doAssert signbit(x2)
  225. doAssert signbit(x3)
  226. block: # copySign
  227. doAssert copySign(10.0, 1.0) == 10.0
  228. doAssert copySign(10.0, -1.0) == -10.0
  229. doAssert copySign(-10.0, -1.0) == -10.0
  230. doAssert copySign(-10.0, 1.0) == 10.0
  231. doAssert copySign(float(10), -1.0) == -10.0
  232. doAssert copySign(10.0'f64, 1.0) == 10.0
  233. doAssert copySign(10.0'f64, -1.0) == -10.0
  234. doAssert copySign(-10.0'f64, -1.0) == -10.0
  235. doAssert copySign(-10.0'f64, 1.0) == 10.0
  236. doAssert copySign(10'f64, -1.0) == -10.0
  237. doAssert copySign(10.0'f32, 1.0) == 10.0
  238. doAssert copySign(10.0'f32, -1.0) == -10.0
  239. doAssert copySign(-10.0'f32, -1.0) == -10.0
  240. doAssert copySign(-10.0'f32, 1.0) == 10.0
  241. doAssert copySign(10'f32, -1.0) == -10.0
  242. doAssert copySign(Inf, -1.0) == -Inf
  243. doAssert copySign(-Inf, 1.0) == Inf
  244. doAssert copySign(Inf, 1.0) == Inf
  245. doAssert copySign(-Inf, -1.0) == -Inf
  246. doAssert copySign(Inf, 0.0) == Inf
  247. doAssert copySign(Inf, -0.0) == -Inf
  248. doAssert copySign(-Inf, 0.0) == Inf
  249. doAssert copySign(-Inf, -0.0) == -Inf
  250. doAssert copySign(1.0, -0.0) == -1.0
  251. doAssert copySign(0.0, -0.0) == -0.0
  252. doAssert copySign(-1.0, 0.0) == 1.0
  253. doAssert copySign(10.0, 0.0) == 10.0
  254. doAssert copySign(-1.0, NaN) == 1.0
  255. doAssert copySign(10.0, NaN) == 10.0
  256. doAssert copySign(NaN, NaN).isNaN
  257. doAssert copySign(-NaN, NaN).isNaN
  258. doAssert copySign(NaN, -NaN).isNaN
  259. doAssert copySign(-NaN, -NaN).isNaN
  260. doAssert copySign(NaN, 0.0).isNaN
  261. doAssert copySign(NaN, -0.0).isNaN
  262. doAssert copySign(-NaN, 0.0).isNaN
  263. doAssert copySign(-NaN, -0.0).isNaN
  264. doAssert copySign(-1.0, NaN) == 1.0
  265. doAssert copySign(-1.0, -NaN) == -1.0
  266. doAssert copySign(1.0, copySign(NaN, -1.0)) == -1.0
  267. block: # almostEqual
  268. doAssert almostEqual(3.141592653589793, 3.1415926535897936)
  269. doAssert almostEqual(1.6777215e7'f32, 1.6777216e7'f32)
  270. doAssert almostEqual(Inf, Inf)
  271. doAssert almostEqual(-Inf, -Inf)
  272. doAssert not almostEqual(Inf, -Inf)
  273. doAssert not almostEqual(-Inf, Inf)
  274. doAssert not almostEqual(Inf, NaN)
  275. doAssert not almostEqual(NaN, NaN)
  276. block: # round
  277. block: # Round to 0 decimal places
  278. doAssert round(54.652) == 55.0
  279. doAssert round(54.352) == 54.0
  280. doAssert round(-54.652) == -55.0
  281. doAssert round(-54.352) == -54.0
  282. doAssert round(0.0) == 0.0
  283. doAssert 1 / round(0.0) == Inf
  284. doAssert 1 / round(-0.0) == -Inf
  285. doAssert round(Inf) == Inf
  286. doAssert round(-Inf) == -Inf
  287. doAssert round(NaN).isNaN
  288. doAssert round(-NaN).isNaN
  289. doAssert round(-0.5) == -1.0
  290. doAssert round(0.5) == 1.0
  291. doAssert round(-1.5) == -2.0
  292. doAssert round(1.5) == 2.0
  293. doAssert round(-2.5) == -3.0
  294. doAssert round(2.5) == 3.0
  295. doAssert round(2.5'f32) == 3.0'f32
  296. doAssert round(2.5'f64) == 3.0'f64
  297. block: # func round*[T: float32|float64](x: T, places: int): T
  298. doAssert round(54.345, 0) == 54.0
  299. template fn(x) =
  300. doAssert round(x, 2).almostEqual 54.35
  301. doAssert round(x, 2).almostEqual 54.35
  302. doAssert round(x, -1).almostEqual 50.0
  303. doAssert round(x, -2).almostEqual 100.0
  304. doAssert round(x, -3).almostEqual 0.0
  305. fn(54.346)
  306. fn(54.346'f32)
  307. block: # abs
  308. doAssert 1.0 / abs(-0.0) == Inf
  309. doAssert 1.0 / abs(0.0) == Inf
  310. doAssert -1.0 / abs(-0.0) == -Inf
  311. doAssert -1.0 / abs(0.0) == -Inf
  312. doAssert abs(0.0) == 0.0
  313. doAssert abs(0.0'f32) == 0.0'f32
  314. doAssert abs(Inf) == Inf
  315. doAssert abs(-Inf) == Inf
  316. doAssert abs(NaN).isNaN
  317. doAssert abs(-NaN).isNaN
  318. block: # classify
  319. doAssert classify(0.3) == fcNormal
  320. doAssert classify(-0.3) == fcNormal
  321. doAssert classify(5.0e-324) == fcSubnormal
  322. doAssert classify(-5.0e-324) == fcSubnormal
  323. doAssert classify(0.0) == fcZero
  324. doAssert classify(-0.0) == fcNegZero
  325. doAssert classify(NaN) == fcNan
  326. doAssert classify(0.3 / 0.0) == fcInf
  327. doAssert classify(Inf) == fcInf
  328. doAssert classify(-0.3 / 0.0) == fcNegInf
  329. doAssert classify(-Inf) == fcNegInf
  330. block: # sum
  331. let empty: seq[int] = @[]
  332. doAssert sum(empty) == 0
  333. doAssert sum([1, 2, 3, 4]) == 10
  334. doAssert sum([-4, 3, 5]) == 4
  335. block: # prod
  336. let empty: seq[int] = @[]
  337. doAssert prod(empty) == 1
  338. doAssert prod([1, 2, 3, 4]) == 24
  339. doAssert prod([-4, 3, 5]) == -60
  340. doAssert almostEqual(prod([1.5, 3.4]), 5.1)
  341. let x: seq[float] = @[]
  342. doAssert prod(x) == 1.0
  343. block: # clamp range
  344. doAssert clamp(10, 1..5) == 5
  345. doAssert clamp(3, 1..5) == 3
  346. doAssert clamp(5, 1..5) == 5
  347. doAssert clamp(42.0, 1.0 .. 3.1415926535) == 3.1415926535
  348. doAssert clamp(NaN, 1.0 .. 2.0).isNaN
  349. doAssert clamp(-Inf, -Inf .. -1.0) == -Inf
  350. type A = enum a0, a1, a2, a3, a4, a5
  351. doAssert a1.clamp(a2..a4) == a2
  352. doAssert clamp((3, 0), (1, 0) .. (2, 9)) == (2, 9)
  353. block: # edge cases
  354. doAssert sqrt(-4.0).isNaN
  355. doAssert ln(0.0) == -Inf
  356. doAssert ln(-0.0) == -Inf
  357. doAssert ln(-12.0).isNaN
  358. doAssert log10(0.0) == -Inf
  359. doAssert log10(-0.0) == -Inf
  360. doAssert log10(-12.0).isNaN
  361. doAssert log2(0.0) == -Inf
  362. doAssert log2(-0.0) == -Inf
  363. doAssert log2(-12.0).isNaN
  364. when nimvm: discard
  365. else:
  366. doAssert frexp(0.0) == (0.0, 0)
  367. doAssert frexp(-0.0) == (-0.0, 0)
  368. doAssert classify(frexp(-0.0)[0]) == fcNegZero
  369. when not defined(js):
  370. doAssert gamma(0.0) == Inf
  371. doAssert gamma(-0.0) == -Inf
  372. doAssert gamma(-1.0).isNaN
  373. doAssert lgamma(0.0) == Inf
  374. doAssert lgamma(-0.0) == Inf
  375. doAssert lgamma(-1.0) == Inf
  376. static: main()
  377. main()