tmath.nim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. discard """
  2. targets: "c cpp js"
  3. matrix:"; -d:danger; --mm:refc"
  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: # divmod
  159. doAssert divmod(int.high, 1) == (int.high, 0)
  160. doAssert divmod(-1073741823, 17) == (-63161283, -12)
  161. doAssert divmod(int32.high, 1.int32) == (int32.high, 0.int32)
  162. doAssert divmod(1073741823.int32, 5.int32) == (214748364.int32, 3.int32)
  163. doAssert divmod(4611686018427387903.int64, 5.int64) == (922337203685477580.int64, 3.int64)
  164. when not defined(js) and (not compileOption("panics")) and compileOption("overflowChecks"):
  165. when nimvm:
  166. discard # cannot catch OverflowDefect here
  167. else:
  168. doAssertRaises(OverflowDefect, (discard divmod(cint.low, -1.cint)))
  169. doAssertRaises(OverflowDefect, (discard divmod(clong.low, -1.clong)))
  170. doAssertRaises(OverflowDefect, (discard divmod(clonglong.low, -1.clonglong)))
  171. doAssertRaises(DivByZeroDefect, (discard divmod(1, 0)))
  172. block: # log
  173. doAssert log(4.0, 3.0) ==~ ln(4.0) / ln(3.0)
  174. doAssert log2(8.0'f64) == 3.0'f64
  175. doAssert log2(4.0'f64) == 2.0'f64
  176. doAssert log2(2.0'f64) == 1.0'f64
  177. doAssert log2(1.0'f64) == 0.0'f64
  178. doAssert classify(log2(0.0'f64)) == fcNegInf
  179. doAssert log2(8.0'f32) == 3.0'f32
  180. doAssert log2(4.0'f32) == 2.0'f32
  181. doAssert log2(2.0'f32) == 1.0'f32
  182. doAssert log2(1.0'f32) == 0.0'f32
  183. doAssert classify(log2(0.0'f32)) == fcNegInf
  184. block: # cumsum
  185. block: # cumsum int seq return
  186. let counts = [1, 2, 3, 4]
  187. doAssert counts.cumsummed == @[1, 3, 6, 10]
  188. let empty: seq[int] = @[]
  189. doAssert empty.cumsummed == @[]
  190. block: # cumsum float seq return
  191. let counts = [1.0, 2.0, 3.0, 4.0]
  192. doAssert counts.cumsummed == @[1.0, 3.0, 6.0, 10.0]
  193. let empty: seq[float] = @[]
  194. doAssert empty.cumsummed == @[]
  195. block: # cumsum int in-place
  196. var counts = [1, 2, 3, 4]
  197. counts.cumsum
  198. doAssert counts == [1, 3, 6, 10]
  199. var empty: seq[int] = @[]
  200. empty.cumsum
  201. doAssert empty == @[]
  202. block: # cumsum float in-place
  203. var counts = [1.0, 2.0, 3.0, 4.0]
  204. counts.cumsum
  205. doAssert counts == [1.0, 3.0, 6.0, 10.0]
  206. var empty: seq[float] = @[]
  207. empty.cumsum
  208. doAssert empty == @[]
  209. block: # ^ compiles for valid types
  210. doAssert: compiles(5 ^ 2)
  211. doAssert: compiles(5.5 ^ 2)
  212. doAssert: compiles(5.5 ^ 2.int8)
  213. doAssert: compiles(5.5 ^ 2.uint)
  214. doAssert: compiles(5.5 ^ 2.uint8)
  215. doAssert: not compiles(5.5 ^ 2.2)
  216. block: # isNaN
  217. doAssert NaN.isNaN
  218. doAssert not Inf.isNaN
  219. doAssert isNaN(Inf - Inf)
  220. doAssert not isNaN(0.0)
  221. doAssert not isNaN(3.1415926)
  222. doAssert not isNaN(0'f32)
  223. block: # signbit
  224. doAssert not signbit(0.0)
  225. doAssert signbit(-0.0)
  226. doAssert signbit(-0.1)
  227. doAssert not signbit(0.1)
  228. doAssert not signbit(Inf)
  229. doAssert signbit(-Inf)
  230. doAssert not signbit(NaN)
  231. let x1 = NaN
  232. let x2 = -NaN
  233. let x3 = -x1
  234. doAssert isNaN(x1)
  235. doAssert isNaN(x2)
  236. doAssert isNaN(x3)
  237. doAssert not signbit(x1)
  238. doAssert signbit(x2)
  239. doAssert signbit(x3)
  240. block: # copySign
  241. doAssert copySign(10.0, 1.0) == 10.0
  242. doAssert copySign(10.0, -1.0) == -10.0
  243. doAssert copySign(-10.0, -1.0) == -10.0
  244. doAssert copySign(-10.0, 1.0) == 10.0
  245. doAssert copySign(float(10), -1.0) == -10.0
  246. doAssert copySign(10.0'f64, 1.0) == 10.0
  247. doAssert copySign(10.0'f64, -1.0) == -10.0
  248. doAssert copySign(-10.0'f64, -1.0) == -10.0
  249. doAssert copySign(-10.0'f64, 1.0) == 10.0
  250. doAssert copySign(10'f64, -1.0) == -10.0
  251. doAssert copySign(10.0'f32, 1.0) == 10.0
  252. doAssert copySign(10.0'f32, -1.0) == -10.0
  253. doAssert copySign(-10.0'f32, -1.0) == -10.0
  254. doAssert copySign(-10.0'f32, 1.0) == 10.0
  255. doAssert copySign(10'f32, -1.0) == -10.0
  256. doAssert copySign(Inf, -1.0) == -Inf
  257. doAssert copySign(-Inf, 1.0) == Inf
  258. doAssert copySign(Inf, 1.0) == Inf
  259. doAssert copySign(-Inf, -1.0) == -Inf
  260. doAssert copySign(Inf, 0.0) == Inf
  261. doAssert copySign(Inf, -0.0) == -Inf
  262. doAssert copySign(-Inf, 0.0) == Inf
  263. doAssert copySign(-Inf, -0.0) == -Inf
  264. doAssert copySign(1.0, -0.0) == -1.0
  265. doAssert copySign(0.0, -0.0) == -0.0
  266. doAssert copySign(-1.0, 0.0) == 1.0
  267. doAssert copySign(10.0, 0.0) == 10.0
  268. doAssert copySign(-1.0, NaN) == 1.0
  269. doAssert copySign(10.0, NaN) == 10.0
  270. doAssert copySign(NaN, NaN).isNaN
  271. doAssert copySign(-NaN, NaN).isNaN
  272. doAssert copySign(NaN, -NaN).isNaN
  273. doAssert copySign(-NaN, -NaN).isNaN
  274. doAssert copySign(NaN, 0.0).isNaN
  275. doAssert copySign(NaN, -0.0).isNaN
  276. doAssert copySign(-NaN, 0.0).isNaN
  277. doAssert copySign(-NaN, -0.0).isNaN
  278. doAssert copySign(-1.0, NaN) == 1.0
  279. doAssert copySign(-1.0, -NaN) == -1.0
  280. doAssert copySign(1.0, copySign(NaN, -1.0)) == -1.0
  281. block: # almostEqual
  282. doAssert almostEqual(3.141592653589793, 3.1415926535897936)
  283. doAssert almostEqual(1.6777215e7'f32, 1.6777216e7'f32)
  284. doAssert almostEqual(Inf, Inf)
  285. doAssert almostEqual(-Inf, -Inf)
  286. doAssert not almostEqual(Inf, -Inf)
  287. doAssert not almostEqual(-Inf, Inf)
  288. doAssert not almostEqual(Inf, NaN)
  289. doAssert not almostEqual(NaN, NaN)
  290. block: # round
  291. block: # Round to 0 decimal places
  292. doAssert round(54.652) == 55.0
  293. doAssert round(54.352) == 54.0
  294. doAssert round(-54.652) == -55.0
  295. doAssert round(-54.352) == -54.0
  296. doAssert round(0.0) == 0.0
  297. doAssert 1 / round(0.0) == Inf
  298. doAssert 1 / round(-0.0) == -Inf
  299. doAssert round(Inf) == Inf
  300. doAssert round(-Inf) == -Inf
  301. doAssert round(NaN).isNaN
  302. doAssert round(-NaN).isNaN
  303. doAssert round(-0.5) == -1.0
  304. doAssert round(0.5) == 1.0
  305. doAssert round(-1.5) == -2.0
  306. doAssert round(1.5) == 2.0
  307. doAssert round(-2.5) == -3.0
  308. doAssert round(2.5) == 3.0
  309. doAssert round(2.5'f32) == 3.0'f32
  310. doAssert round(2.5'f64) == 3.0'f64
  311. block: # func round*[T: float32|float64](x: T, places: int): T
  312. doAssert round(54.345, 0) == 54.0
  313. template fn(x) =
  314. doAssert round(x, 2).almostEqual 54.35
  315. doAssert round(x, 2).almostEqual 54.35
  316. doAssert round(x, -1).almostEqual 50.0
  317. doAssert round(x, -2).almostEqual 100.0
  318. doAssert round(x, -3).almostEqual 0.0
  319. fn(54.346)
  320. fn(54.346'f32)
  321. block: # abs
  322. doAssert 1.0 / abs(-0.0) == Inf
  323. doAssert 1.0 / abs(0.0) == Inf
  324. doAssert -1.0 / abs(-0.0) == -Inf
  325. doAssert -1.0 / abs(0.0) == -Inf
  326. doAssert abs(0.0) == 0.0
  327. doAssert abs(0.0'f32) == 0.0'f32
  328. doAssert abs(Inf) == Inf
  329. doAssert abs(-Inf) == Inf
  330. doAssert abs(NaN).isNaN
  331. doAssert abs(-NaN).isNaN
  332. block: # classify
  333. doAssert classify(0.3) == fcNormal
  334. doAssert classify(-0.3) == fcNormal
  335. doAssert classify(5.0e-324) == fcSubnormal
  336. doAssert classify(-5.0e-324) == fcSubnormal
  337. doAssert classify(0.0) == fcZero
  338. doAssert classify(-0.0) == fcNegZero
  339. doAssert classify(NaN) == fcNan
  340. doAssert classify(0.3 / 0.0) == fcInf
  341. doAssert classify(Inf) == fcInf
  342. doAssert classify(-0.3 / 0.0) == fcNegInf
  343. doAssert classify(-Inf) == fcNegInf
  344. block: # sum
  345. let empty: seq[int] = @[]
  346. doAssert sum(empty) == 0
  347. doAssert sum([1, 2, 3, 4]) == 10
  348. doAssert sum([-4, 3, 5]) == 4
  349. block: # prod
  350. let empty: seq[int] = @[]
  351. doAssert prod(empty) == 1
  352. doAssert prod([1, 2, 3, 4]) == 24
  353. doAssert prod([-4, 3, 5]) == -60
  354. doAssert almostEqual(prod([1.5, 3.4]), 5.1)
  355. let x: seq[float] = @[]
  356. doAssert prod(x) == 1.0
  357. block: # clamp range
  358. doAssert clamp(10, 1..5) == 5
  359. doAssert clamp(3, 1..5) == 3
  360. doAssert clamp(5, 1..5) == 5
  361. doAssert clamp(42.0, 1.0 .. 3.1415926535) == 3.1415926535
  362. doAssert clamp(NaN, 1.0 .. 2.0).isNaN
  363. doAssert clamp(-Inf, -Inf .. -1.0) == -Inf
  364. type A = enum a0, a1, a2, a3, a4, a5
  365. doAssert a1.clamp(a2..a4) == a2
  366. doAssert clamp((3, 0), (1, 0) .. (2, 9)) == (2, 9)
  367. block: # edge cases
  368. doAssert sqrt(-4.0).isNaN
  369. doAssert ln(0.0) == -Inf
  370. doAssert ln(-0.0) == -Inf
  371. doAssert ln(-12.0).isNaN
  372. doAssert log10(0.0) == -Inf
  373. doAssert log10(-0.0) == -Inf
  374. doAssert log10(-12.0).isNaN
  375. doAssert log2(0.0) == -Inf
  376. doAssert log2(-0.0) == -Inf
  377. doAssert log2(-12.0).isNaN
  378. when nimvm: discard
  379. else:
  380. doAssert frexp(0.0) == (0.0, 0)
  381. doAssert frexp(-0.0) == (-0.0, 0)
  382. doAssert classify(frexp(-0.0)[0]) == fcNegZero
  383. when not defined(js):
  384. doAssert gamma(0.0) == Inf
  385. doAssert gamma(-0.0) == -Inf
  386. doAssert gamma(-1.0).isNaN
  387. doAssert lgamma(0.0) == Inf
  388. doAssert lgamma(-0.0) == Inf
  389. doAssert lgamma(-1.0) == Inf
  390. static: main()
  391. main()
  392. when not defined(js) and not defined(danger):
  393. block: # bug #21792
  394. block:
  395. type Digit = 0..9
  396. var x = [Digit 4, 7]
  397. doAssertRaises(RangeDefect):
  398. discard sum(x)
  399. block:
  400. var x = [int8 124, 127]
  401. doAssertRaises(OverflowDefect):
  402. discard sum(x)