test-flonums.scm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ;;; Copyright (C) 2023 Igalia, S.L.
  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. ;;; Floating point number tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (test utils))
  21. (test-begin "test-flonums")
  22. ;; Flonum addition
  23. (test-call "84.0" (lambda (a) (+ (inexact a) (inexact a))) 42)
  24. (test-call "84.0" (lambda (a) (+ (inexact a) a)) 42)
  25. (test-call "536870954.0" (lambda (a b c) (+ (inexact a) (+ b c))) 42 536870911 1)
  26. (test-call "84.0" (lambda (a) (+ a (inexact a))) 42)
  27. (test-call "536870954.0" (lambda (a b c) (+ (+ a b) (inexact c))) 536870911 1 42)
  28. ;; Arithmetic with a flonum constant should *not* lower to
  29. ;; add/immediate and friends.
  30. (test-call "20.0" (lambda (a) (+ a 16.0)) 4.0)
  31. ;; Flonum subtraction
  32. (test-call "0.0" (lambda (a) (- (inexact a) (inexact a))) 42)
  33. (test-call "0.0" (lambda (a) (- (inexact a) a)) 42)
  34. (test-call "-536870870.0" (lambda (a b c) (- (inexact a) (+ b c))) 42 536870911 1)
  35. (test-call "0.0" (lambda (a) (- a (inexact a))) 42)
  36. (test-call "536870870.0" (lambda (a b c) (- (+ a b) (inexact c))) 536870911 1 42)
  37. ;; Flonum multiplication
  38. (test-call "1764.0" (lambda (a) (* (inexact a) (inexact a))) 42)
  39. (test-call "1764.0" (lambda (a) (* (inexact a) a)) 42)
  40. (test-call "22548578304.0" (lambda (a b c) (* (inexact a) (+ b c))) 42 536870911 1)
  41. (test-call "1764.0" (lambda (a) (* a (inexact a))) 42)
  42. (test-call "22548578304.0" (lambda (a b c) (* (+ a b) (inexact c))) 536870911 1 42)
  43. ;; Flonum division
  44. (test-call "5.25"
  45. (lambda (a b) (/ (inexact a) (inexact b)))
  46. 42 8)
  47. (test-call "0.5" (lambda (a b) (/ (inexact a) b)) 1 2)
  48. (test-call "1.0"
  49. (lambda (a b) (/ (inexact (+ a b)) (+ a b)))
  50. 536870911 1)
  51. (test-call "0.5" (lambda (a b) (/ a (inexact b))) 1 2)
  52. (test-call "107374182.4"
  53. (lambda (a b c) (/ (+ a b) (inexact c)))
  54. 536870911 1 5)
  55. (test-call "1.0" (lambda (a) (inexact a)) 1)
  56. (test-call "536870912.0" (lambda (a b) (inexact (+ a b))) 536870911 1)
  57. (test-call "+inf.0" (lambda (a b) (/ (inexact a) (inexact b))) 1 0)
  58. (test-call "0.5" (lambda (a b) (inexact (/ a b))) 1 2)
  59. ;; Square root and trigonometry
  60. (test-call "2.0" (lambda (a) (sqrt a)) 4)
  61. (test-call "0.0" (lambda (a) (sin a)) 0)
  62. (test-call "1.0" (lambda (a) (cos a)) 0)
  63. (test-call "0.0" (lambda (a) (tan a)) 0)
  64. (test-call "0.0" (lambda (a) (asin a)) 0)
  65. (test-call "0.0" (lambda (a) (acos a)) 1)
  66. (test-call "0.0" (lambda (a) (atan a)) 0)
  67. (test-call "0.0" (lambda (a b) (atan a b)) 0 1)
  68. (test-call "#t" (lambda (a b) (= a (inexact a))) 23 23)
  69. (test-call "#t" (lambda (a b c) (<= a (inexact (/ b c)))) 23 235 10)
  70. ;; comparisons with infinities and NaNs
  71. (test-call "#t" (lambda (a b) (= a b)) +inf.0 +inf.0)
  72. (test-call "#t" (lambda (a b) (= a b)) -inf.0 -inf.0)
  73. (test-call "#f" (lambda (a b) (= a b)) +inf.0 -inf.0)
  74. (test-call "#f" (lambda (a b) (= a b)) +nan.0 +nan.0)
  75. (test-call "#f" (lambda (a b) (= a b)) +inf.0 1.0)
  76. (test-call "#f" (lambda (a b) (= a b)) -inf.0 1.0)
  77. (test-call "#f" (lambda (a b) (= a b)) +nan.0 1.0)
  78. (test-call "#f" (lambda (a b) (< a b)) +inf.0 +inf.0)
  79. (test-call "#f" (lambda (a b) (< a b)) -inf.0 -inf.0)
  80. (test-call "#f" (lambda (a b) (< a b)) +inf.0 -inf.0)
  81. (test-call "#f" (lambda (a b) (< a b)) +nan.0 +nan.0)
  82. (test-call "#f" (lambda (a b) (< a b)) +inf.0 1.0)
  83. (test-call "#t" (lambda (a b) (< a b)) -inf.0 1.0)
  84. (test-call "#f" (lambda (a b) (< a b)) +nan.0 1.0)
  85. (test-call "#f" (lambda (a b) (< a b)) +inf.0 +inf.0)
  86. (test-call "#f" (lambda (a b) (< a b)) -inf.0 -inf.0)
  87. (test-call "#t" (lambda (a b) (< a b)) -inf.0 +inf.0)
  88. (test-call "#f" (lambda (a b) (< a b)) +nan.0 +nan.0)
  89. (test-call "#t" (lambda (a b) (< a b)) 1.0 +inf.0)
  90. (test-call "#f" (lambda (a b) (< a b)) 1.0 -inf.0)
  91. (test-call "#f" (lambda (a b) (< a b)) 1.0 +nan.0)
  92. (test-call "#t" (lambda (a b) (<= a b)) +inf.0 +inf.0)
  93. (test-call "#t" (lambda (a b) (<= a b)) -inf.0 -inf.0)
  94. (test-call "#f" (lambda (a b) (<= a b)) +inf.0 -inf.0)
  95. (test-call "#f" (lambda (a b) (<= a b)) +nan.0 +nan.0)
  96. (test-call "#f" (lambda (a b) (<= a b)) +inf.0 1.0)
  97. (test-call "#t" (lambda (a b) (<= a b)) -inf.0 1.0)
  98. (test-call "#f" (lambda (a b) (<= a b)) +nan.0 1.0)
  99. (test-call "#t" (lambda (a b) (<= a b)) +inf.0 +inf.0)
  100. (test-call "#t" (lambda (a b) (<= a b)) -inf.0 -inf.0)
  101. (test-call "#t" (lambda (a b) (<= a b)) -inf.0 +inf.0)
  102. (test-call "#f" (lambda (a b) (<= a b)) +nan.0 +nan.0)
  103. (test-call "#t" (lambda (a b) (<= a b)) 1.0 +inf.0)
  104. (test-call "#f" (lambda (a b) (<= a b)) 1.0 -inf.0)
  105. (test-call "#f" (lambda (a b) (<= a b)) 1.0 +nan.0)
  106. ;;; abs, floor and ceiling
  107. (test-call "0.5" (lambda (a b) (abs (inexact (/ a b)))) 1 2)
  108. (test-call "0.5" (lambda (a b) (abs (inexact (/ a b)))) -1 2)
  109. (test-call "0.0" (lambda (a b) (floor (inexact (/ a b)))) 1 2)
  110. (test-call "-1.0" (lambda (a b) (floor (inexact (/ a b)))) -1 2)
  111. (test-call "1.0" (lambda (a b) (ceiling (inexact (/ a b)))) 1 2)
  112. (test-call "-0.0" (lambda (a b) (ceiling (inexact (/ a b)))) -1 2)
  113. (test-end* "test-flonums")