test-numeric.scm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ;;; Copyright (C) 2023 Robin Templeton
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. ;;; Commentary:
  15. ;;;
  16. ;;; Miscellaneous numeric tower tests, primarily for mixed-type
  17. ;;; operations.
  18. ;;;
  19. ;;; Code:
  20. (use-modules (ice-9 format)
  21. (srfi srfi-64)
  22. (test utils))
  23. (test-begin "test-numeric")
  24. ;; Flonum<->fraction comparison
  25. ;; ...with infinities and NaN
  26. (test-call "#t" (lambda (a b) (< a b)) 1/2 +inf.0)
  27. (test-call "#f" (lambda (a b) (< a b)) 1/2 -inf.0)
  28. (test-call "#f" (lambda (a b) (< a b)) +nan.0 1/2)
  29. (test-call "#f" (lambda (a b) (< a b)) +inf.0 1/2)
  30. (test-call "#t" (lambda (a b) (< a b)) -inf.0 1/2)
  31. (test-call "#f" (lambda (a b) (< a b)) 1/2 +nan.0)
  32. (test-call "#t" (lambda (a b) (<= a b)) 1/2 +inf.0)
  33. (test-call "#f" (lambda (a b) (<= a b)) 1/2 -inf.0)
  34. (test-call "#f" (lambda (a b) (<= a b)) +nan.0 1/2)
  35. (test-call "#f" (lambda (a b) (<= a b)) +inf.0 1/2)
  36. (test-call "#t" (lambda (a b) (<= a b)) -inf.0 1/2)
  37. (test-call "#f" (lambda (a b) (<= a b)) 1/2 +nan.0)
  38. (test-call "#f" (lambda (a b) (= a b)) 1/2 +inf.0)
  39. (test-call "#f" (lambda (a b) (= a b)) 1/2 -inf.0)
  40. (test-call "#f" (lambda (a b) (= a b)) +nan.0 1/2)
  41. (test-call "#f" (lambda (a b) (= a b)) +inf.0 1/2)
  42. (test-call "#f" (lambda (a b) (= a b)) -inf.0 1/2)
  43. (test-call "#f" (lambda (a b) (= a b)) 1/2 +nan.0)
  44. ;; ...with ordinary flonums
  45. (test-call "#t" (lambda (a b) (< a b)) 1/2 1.0)
  46. (test-call "#f" (lambda (a b) (< a b)) 1/2 -1.0)
  47. (test-call "#f" (lambda (a b) (< a b)) 1.0 1/2)
  48. (test-call "#t" (lambda (a b) (< a b)) -1.0 1/2)
  49. (test-call "#t" (lambda (a b) (<= a b)) 1/2 1.0)
  50. (test-call "#f" (lambda (a b) (<= a b)) 1/2 -1.0)
  51. (test-call "#f" (lambda (a b) (<= a b)) 1.0 1/2)
  52. (test-call "#t" (lambda (a b) (<= a b)) -1.0 1/2)
  53. (test-call "#f" (lambda (a b) (= a b)) 1/2 1.0)
  54. (test-call "#f" (lambda (a b) (= a b)) 1/2 -1.0)
  55. (test-call "#f" (lambda (a b) (= a b)) 1.0 1/2)
  56. (test-call "#f" (lambda (a b) (= a b)) -1.0 1/2)
  57. (test-call "#t" (lambda (a b) (= a b)) 1/2 0.5)
  58. (test-call "#t" (lambda (a b) (= a b)) 0.5 1/2)
  59. (test-call "#t" (lambda (a b) (<= a b)) 1/2 0.5)
  60. (test-call "#t" (lambda (a b) (<= a b)) 0.5 1/2)
  61. (test-call "#t" (lambda (a b) (= a b)) 0.5 1/2)
  62. (test-call "#t" (lambda (a b) (= a b)) 0.5 1/2)
  63. ;; exact
  64. (test-call "1" (lambda (a) (exact a)) 1)
  65. (test-call "1/2" (lambda (a) (exact a)) 1/2)
  66. (test-call "0" (lambda (a) (exact a)) 0.0)
  67. (test-call "0" (lambda (a) (exact a)) -0.0)
  68. (test-call "1/2" (lambda (a) (exact a)) 0.5)
  69. (test-call "-1/2" (lambda (a) (exact a)) -0.5)
  70. (test-call "9999" (lambda (a) (exact a)) 9999.0)
  71. (test-call "-9999" (lambda (a) (exact a)) -9999.0)
  72. ;; subnormal conversion
  73. (test-call (format #f "~d/~d" #xcccccccccccd (ash 2 1072))
  74. (lambda (a) (exact a))
  75. 2.2250738585072014e-309)
  76. ;; numeric equivalence
  77. (test-call "#t" (lambda (a b) (eqv? a b)) 1 1)
  78. (test-call "#f" (lambda (a b) (eqv? a b)) 1 1.0)
  79. (test-call "#t" (lambda (a b) (eqv? a (+ b b))) 1/2 1/4)
  80. (test-call "#t" (lambda (a b) (eqv? a b)) +nan.0 +nan.0)
  81. (test-call "#t" (lambda (a b) (eqv? a b)) +inf.0 +inf.0)
  82. (test-call "#t" (lambda (a b) (eqv? a b)) -inf.0 -inf.0)
  83. (test-call "#t" (lambda (a b) (equal? a b)) 1 1)
  84. (test-call "#f" (lambda (a b) (equal? a b)) 1 1.0)
  85. (test-call "#t" (lambda (a b) (equal? a (+ b b))) 1/2 1/4)
  86. (test-call "#t" (lambda (a b) (equal? a b)) +nan.0 +nan.0)
  87. (test-call "#t" (lambda (a b) (equal? a b)) +inf.0 +inf.0)
  88. (test-call "#t" (lambda (a b) (equal? a b)) -inf.0 -inf.0)
  89. ;; numerator and denominator
  90. (test-call "42" (lambda (a) (numerator a)) 42)
  91. (test-call "1" (lambda (a) (denominator a)) 42)
  92. (test-call "3" (lambda (a) (numerator a)) 6/4)
  93. (test-call "2" (lambda (a) (denominator a)) 6/4)
  94. (test-call "3.0" (lambda (a) (numerator (inexact a))) 6/4)
  95. (test-call "2.0" (lambda (a) (denominator (inexact a))) 6/4)
  96. ;; log and exp
  97. (test-call "0.0" (lambda (a) (log a)) 1)
  98. (test-call "2.0" (lambda (a b) (log a b)) 100 10)
  99. (test-call "1.0" (lambda (a) (exp (log a))) 1)
  100. (test-call "0.5" (lambda (a) (exp (log a))) 1/2)
  101. ;; expt
  102. (test-call "1" (lambda (a b) (expt a b)) 0 0)
  103. (test-call "1.0" (lambda (a b) (expt a b)) 0 0.0)
  104. (test-call "0" (lambda (a b) (expt a b)) 0 42)
  105. (test-call "0.0" (lambda (a b) (expt a b)) 0 42.0)
  106. (test-call "+nan.0" (lambda (a b) (expt a b)) 0 -1)
  107. (test-call "1.0" (lambda (a b) (expt a b)) 0.0 0)
  108. (test-call "1.0" (lambda (a b) (expt a b)) 0.0 0.0)
  109. (test-call "0.0" (lambda (a b) (expt a b)) 0.0 42)
  110. (test-call "0.0" (lambda (a b) (expt a b)) 0.0 42.0)
  111. (test-call "+nan.0" (lambda (a b) (expt a b)) 0.0 -1)
  112. (test-call "1" (lambda (a b) (expt a b)) 1 0)
  113. (test-call "1" (lambda (a b) (expt a b)) 1 1)
  114. (test-call "1" (lambda (a b) (expt a b)) 1 2)
  115. (test-call "1" (lambda (a b) (expt a b)) 1 3)
  116. (test-call "1" (lambda (a b) (expt a b)) 2 0)
  117. (test-call "2" (lambda (a b) (expt a b)) 2 1)
  118. (test-call "4" (lambda (a b) (expt a b)) 2 2)
  119. (test-call "8" (lambda (a b) (expt a b)) 2 3)
  120. (test-call "1" (lambda (a b) (expt a b)) 1/2 0)
  121. (test-call "1/2" (lambda (a b) (expt a b)) 1/2 1)
  122. (test-call "1/4" (lambda (a b) (expt a b)) 1/2 2)
  123. (test-call "1/8" (lambda (a b) (expt a b)) 1/2 3)
  124. (test-call "2.0" (lambda (a b) (expt a b)) 4 1/2)
  125. (test-call "2.0" (lambda (a b) (expt a b)) 4 0.5)
  126. (test-call "2.0" (lambda (a b) (expt a b)) 4.0 1/2)
  127. (test-call "2.0" (lambda (a b) (expt a b)) 4.0 0.5)
  128. (test-call "+inf.0" (lambda (a b) (expt a b)) +inf.0 +inf.0)
  129. (test-call "0.0" (lambda (a b) (expt a b)) +inf.0 -inf.0)
  130. ;; gcd and lcm
  131. (test-call "4" (lambda (a b) (gcd a b)) 32 -36)
  132. (test-call "0" (lambda () (gcd)))
  133. (test-call "288" (lambda (a b) (lcm a b)) 32 -36)
  134. (test-call "288.0" (lambda (a b) (lcm a b)) 32.0 -36)
  135. (test-call "1" (lambda () (lcm)))
  136. ;; rationalize
  137. (test-call "1/3" (lambda (a b) (rationalize a b)) 3/10 1/10)
  138. (test-call "1/4" (lambda (a b) (rationalize a b)) 2/10 1/10)
  139. (test-call "0.25" (lambda (a b) (rationalize a b)) 0.2 1/10)
  140. (test-call "0.25" (lambda (a b) (rationalize a b)) 2/10 0.1)
  141. ;; eta expansion
  142. (test-call "3" (lambda (f a b) (f a b)) + 1 2)
  143. (test-call "1" (lambda (f a b) (f a b)) - 2 1)
  144. (test-call "6" (lambda (f a b) (f a b)) * 2 3)
  145. (test-call "2" (lambda (f a b) (f a b)) / 6 3)
  146. ;; truncate
  147. (test-call "42" (lambda (x) (truncate x)) 42)
  148. (test-call "42.0" (lambda (x) (truncate x)) 42.3)
  149. (test-call "1" (lambda (x) (truncate x)) 43/42)
  150. (test-end* "test-numeric")