numeric-tests.scm 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. ;; Modified from chibi/tests/numeric-tests.scm
  2. ;; these tests are only valid if chibi-scheme is compiled with full
  3. ;; numeric support (USE_BIGNUMS, USE_FLONUMS and USE_MATH)
  4. (cond-expand
  5. (kawa
  6. (define-syntax test
  7. (syntax-rules ()
  8. ((test expected expression)
  9. (test-equal expected expression)))))
  10. (modules (import (only (chibi test) test-begin test test-end)))
  11. (else #f))
  12. (test-begin "numbers")
  13. (define (integer-neighborhoods x)
  14. (list x (+ 1 x) (+ -1 x) (- x) (- 1 x) (- -1 x)))
  15. (test '(536870912 536870913 536870911 -536870912 -536870911 -536870913)
  16. (integer-neighborhoods (expt 2 29)))
  17. (test '(1073741824 1073741825 1073741823 -1073741824 -1073741823 -1073741825)
  18. (integer-neighborhoods (expt 2 30)))
  19. (test '(2147483648 2147483649 2147483647 -2147483648 -2147483647 -2147483649)
  20. (integer-neighborhoods (expt 2 31)))
  21. (test '(4294967296 4294967297 4294967295 -4294967296 -4294967295 -4294967297)
  22. (integer-neighborhoods (expt 2 32)))
  23. (test '(4611686018427387904 4611686018427387905 4611686018427387903
  24. -4611686018427387904 -4611686018427387903 -4611686018427387905)
  25. (integer-neighborhoods (expt 2 62)))
  26. (test '(9223372036854775808 9223372036854775809 9223372036854775807
  27. -9223372036854775808 -9223372036854775807 -9223372036854775809)
  28. (integer-neighborhoods (expt 2 63)))
  29. (test '(18446744073709551616 18446744073709551617 18446744073709551615
  30. -18446744073709551616 -18446744073709551615 -18446744073709551617)
  31. (integer-neighborhoods (expt 2 64)))
  32. (test '(85070591730234615865843651857942052864
  33. 85070591730234615865843651857942052865
  34. 85070591730234615865843651857942052863
  35. -85070591730234615865843651857942052864
  36. -85070591730234615865843651857942052863
  37. -85070591730234615865843651857942052865)
  38. (integer-neighborhoods (expt 2 126)))
  39. (test '(170141183460469231731687303715884105728
  40. 170141183460469231731687303715884105729
  41. 170141183460469231731687303715884105727
  42. -170141183460469231731687303715884105728
  43. -170141183460469231731687303715884105727
  44. -170141183460469231731687303715884105729)
  45. (integer-neighborhoods (expt 2 127)))
  46. (test '(340282366920938463463374607431768211456
  47. 340282366920938463463374607431768211457
  48. 340282366920938463463374607431768211455
  49. -340282366920938463463374607431768211456
  50. -340282366920938463463374607431768211455
  51. -340282366920938463463374607431768211457)
  52. (integer-neighborhoods (expt 2 128)))
  53. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  54. (define (integer-arithmetic-combinations a b)
  55. (list (+ a b) (- a b) (* a b) (quotient a b) (remainder a b)))
  56. (define (sign-combinations a b)
  57. (list (integer-arithmetic-combinations a b)
  58. (integer-arithmetic-combinations (- a) b)
  59. (integer-arithmetic-combinations a (- b))
  60. (integer-arithmetic-combinations (- a) (- b))))
  61. ;; fix x fix
  62. (test '((1 -1 0 0 0) (1 -1 0 0 0) (-1 1 0 0 0) (-1 1 0 0 0))
  63. (sign-combinations 0 1))
  64. (test '((2 0 1 1 0) (0 -2 -1 -1 0) (0 2 -1 -1 0) (-2 0 1 1 0))
  65. (sign-combinations 1 1))
  66. (test '((59 25 714 2 8) (-25 -59 -714 -2 -8)
  67. (25 59 -714 -2 8) (-59 -25 714 2 -8))
  68. (sign-combinations 42 17))
  69. ;; fix x big
  70. (test '((4294967338 -4294967254 180388626432 0 42)
  71. (4294967254 -4294967338 -180388626432 0 -42)
  72. (-4294967254 4294967338 -180388626432 0 42)
  73. (-4294967338 4294967254 180388626432 0 -42))
  74. (sign-combinations 42 (expt 2 32)))
  75. ;; big x fix
  76. (test '((4294967338 4294967254 180388626432 102261126 4)
  77. (-4294967254 -4294967338 -180388626432 -102261126 -4)
  78. (4294967254 4294967338 -180388626432 -102261126 4)
  79. (-4294967338 -4294967254 180388626432 102261126 -4))
  80. (sign-combinations (expt 2 32) 42))
  81. ;; big x bigger
  82. (test '((12884901889 -4294967297 36893488151714070528 0 4294967296)
  83. (4294967297 -12884901889 -36893488151714070528 0 -4294967296)
  84. (-4294967297 12884901889 -36893488151714070528 0 4294967296)
  85. (-12884901889 4294967297 36893488151714070528 0 -4294967296))
  86. (sign-combinations (expt 2 32) (+ 1 (expt 2 33))))
  87. (test '((18446744078004518913 -18446744069414584321 79228162514264337597838917632 0 4294967296)
  88. (18446744069414584321 -18446744078004518913 -79228162514264337597838917632 0 -4294967296)
  89. (-18446744069414584321 18446744078004518913 -79228162514264337597838917632 0 4294967296)
  90. (-18446744078004518913 18446744069414584321 79228162514264337597838917632 0 -4294967296))
  91. (sign-combinations (expt 2 32) (+ 1 (expt 2 64))))
  92. ;; bigger x big
  93. (test '((12884901889 4294967297 36893488151714070528 2 1)
  94. (-4294967297 -12884901889 -36893488151714070528 -2 -1)
  95. (4294967297 12884901889 -36893488151714070528 -2 1)
  96. (-12884901889 -4294967297 36893488151714070528 2 -1))
  97. (sign-combinations (+ 1 (expt 2 33)) (expt 2 32)))
  98. (test '((18446744078004518913 18446744069414584321 79228162514264337597838917632 4294967296 1)
  99. (-18446744069414584321 -18446744078004518913 -79228162514264337597838917632 -4294967296 -1)
  100. (18446744069414584321 18446744078004518913 -79228162514264337597838917632 -4294967296 1)
  101. (-18446744078004518913 -18446744069414584321 79228162514264337597838917632 4294967296 -1))
  102. (sign-combinations (+ 1 (expt 2 64)) (expt 2 32)))
  103. (define M7 (- (expt 2 127) 1))
  104. (test '((170141183460469231750134047789593657344
  105. 170141183460469231713240559642174554110
  106. 3138550867693340382088035895064302439764418281874191810559
  107. 9223372036854775807
  108. 9223372036854775808)
  109. (-170141183460469231713240559642174554110
  110. -170141183460469231750134047789593657344
  111. -3138550867693340382088035895064302439764418281874191810559
  112. -9223372036854775807
  113. -9223372036854775808)
  114. (170141183460469231713240559642174554110
  115. 170141183460469231750134047789593657344
  116. -3138550867693340382088035895064302439764418281874191810559
  117. -9223372036854775807
  118. 9223372036854775808)
  119. (-170141183460469231750134047789593657344
  120. -170141183460469231713240559642174554110
  121. 3138550867693340382088035895064302439764418281874191810559
  122. 9223372036854775807
  123. -9223372036854775808))
  124. (sign-combinations M7 (+ 1 (expt 2 64))))
  125. (test #f (< +nan.0 +nan.0))
  126. (test #f (<= +nan.0 +nan.0))
  127. (test #f (= +nan.0 +nan.0))
  128. (test #f (>= +nan.0 +nan.0))
  129. (test #f (> +nan.0 +nan.0))
  130. (test #f (< +inf.0 +inf.0))
  131. (test #t (<= +inf.0 +inf.0))
  132. (test #t (= +inf.0 +inf.0))
  133. (test #t (>= +inf.0 +inf.0))
  134. (test #f (> +inf.0 +inf.0))
  135. (test #f (< -inf.0 -inf.0))
  136. (test #t (<= -inf.0 -inf.0))
  137. (test #t (= -inf.0 -inf.0))
  138. (test #t (>= -inf.0 -inf.0))
  139. (test #f (> -inf.0 -inf.0))
  140. (test #t (< -inf.0 +inf.0))
  141. (test #t (<= -inf.0 +inf.0))
  142. (test #f (= -inf.0 +inf.0))
  143. (test #f (>= -inf.0 +inf.0))
  144. (test #f (> -inf.0 +inf.0))
  145. (test 88962710306127702866241727433142015
  146. (string->number "#x00112233445566778899aabbccddeeff"))
  147. (test (inexact (expt 10 154)) (inexact (sqrt (expt 10 308))))
  148. (test 36893488147419103231
  149. (- 340282366920938463463374607431768211456
  150. 340282366920938463426481119284349108225))
  151. (cond-expand
  152. (ratios
  153. (test #t (< 1/2 1.0))
  154. (test #t (< 1.0 3/2))
  155. (test #t (< 1/2 1.5))
  156. (test #t (< 1/2 2.0))
  157. (test 1.0 (max 1/2 1.0))
  158. (test 18446744073709551617 (numerator (/ 18446744073709551617 2)))
  159. (test "18446744073709551617/2" (number->string (/ 18446744073709551617 2)))
  160. (let ((a 1000000000000000000000000000000000000000)
  161. (b 31622776601683794000))
  162. (test 31622776601683792639 (quotient a b))
  163. (test 30922992657207634000 (remainder a b)))
  164. (let ((g 18446744073709551616/6148914691236517205))
  165. (test 36893488147419103231/113427455640312821148309287786019553280
  166. (- g (/ 9 g))))
  167. (let ((r (/ (expt 2 61) 3)))
  168. (test 0 (- r r))
  169. (test 2305843009213693952/3 r)))
  170. (else
  171. #f))
  172. (test-end)