trationals.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. """
  4. import std/[rationals, math]
  5. import std/assertions
  6. template main() =
  7. var
  8. z = Rational[int](num: 0, den: 1)
  9. o = initRational(num = 1, den = 1)
  10. a = initRational(1, 2)
  11. u = 3u // 2
  12. b = -1 // -2
  13. m1 = -1 // 1
  14. tt = 10 // 2
  15. doAssert a == a
  16. doAssert a - a == z
  17. doAssert a + b == o
  18. doAssert a / b == o
  19. doAssert a * b == 1 // 4
  20. doAssert 3 / a == 6 // 1
  21. doAssert a / 3 == 1 // 6
  22. doAssert tt * z == z
  23. doAssert 10 * a == tt
  24. doAssert a * 10 == tt
  25. doAssert tt / 10 == a
  26. doAssert a - m1 == 3 // 2
  27. doAssert a + m1 == -1 // 2
  28. doAssert m1 + tt == 16 // 4
  29. doAssert m1 - tt == 6 // -1
  30. doAssert z < o
  31. doAssert z <= o
  32. doAssert z == z
  33. doAssert cmp(z, o) < 0
  34. doAssert cmp(o, z) > 0
  35. doAssert o == o
  36. doAssert o >= o
  37. doAssert not(o > o)
  38. doAssert cmp(o, o) == 0
  39. doAssert cmp(z, z) == 0
  40. doAssert hash(o) == hash(o)
  41. doAssert a == b
  42. doAssert a >= b
  43. doAssert not(b > a)
  44. doAssert cmp(a, b) == 0
  45. doAssert hash(a) == hash(b)
  46. var x = 1 // 3
  47. x *= 5 // 1
  48. doAssert x == 5 // 3
  49. x += 2 // 9
  50. doAssert x == 17 // 9
  51. x -= 9 // 18
  52. doAssert x == 25 // 18
  53. x /= 1 // 2
  54. doAssert x == 50 // 18
  55. var y = 1 // 3
  56. y *= 4
  57. doAssert y == 4 // 3
  58. y += 5
  59. doAssert y == 19 // 3
  60. y -= 2
  61. doAssert y == 13 // 3
  62. y /= 9
  63. doAssert y == 13 // 27
  64. doAssert toRational(5) == 5 // 1
  65. doAssert abs(toFloat(y) - 0.4814814814814815) < 1.0e-7
  66. doAssert toInt(z) == 0
  67. when sizeof(int) == 8:
  68. doAssert toRational(0.98765432) == 2111111029 // 2137499919
  69. doAssert toRational(PI) == 817696623 // 260280919
  70. when sizeof(int) == 4:
  71. doAssert toRational(0.98765432) == 80 // 81
  72. doAssert toRational(PI) == 355 // 113
  73. doAssert toRational(0.1) == 1 // 10
  74. doAssert toRational(0.9) == 9 // 10
  75. doAssert toRational(0.0) == 0 // 1
  76. doAssert toRational(-0.25) == 1 // -4
  77. doAssert toRational(3.2) == 16 // 5
  78. doAssert toRational(0.33) == 33 // 100
  79. doAssert toRational(0.22) == 11 // 50
  80. doAssert toRational(10.0) == 10 // 1
  81. doAssert (1 // 1) div (3 // 10) == 3
  82. doAssert (-1 // 1) div (3 // 10) == -3
  83. doAssert (3 // 10) mod (1 // 1) == 3 // 10
  84. doAssert (-3 // 10) mod (1 // 1) == -3 // 10
  85. doAssert floorDiv(1 // 1, 3 // 10) == 3
  86. doAssert floorDiv(-1 // 1, 3 // 10) == -4
  87. doAssert floorMod(3 // 10, 1 // 1) == 3 // 10
  88. doAssert floorMod(-3 // 10, 1 // 1) == 7 // 10
  89. when sizeof(int) == 8:
  90. doAssert almostEqual(PI.toRational.toFloat, PI)
  91. # unsigned
  92. doAssert u == u
  93. doAssert u + u == 3u // 1
  94. doAssert 3u.toRational - u == u
  95. doAssert u * 2 == 3u // 1
  96. static: main()
  97. main()