sci-num-arith.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. (import
  2. (scheme base)
  3. (scheme inexact)
  4. (macduffie helpful)
  5. (macduffie sci-num))
  6. (define (print-test-table printer test rows cols expected)
  7. (for-each
  8. (lambda (arg-row expect-row)
  9. (for-each
  10. (lambda (arg-col expect-col)
  11. (printer arg-row arg-col expect-col (test arg-row arg-col expect-col)))
  12. cols
  13. expect-row))
  14. rows
  15. expected))
  16. (define (print-test-row printer test row expected)
  17. (for-each
  18. (lambda (arg-row expected)
  19. (printer arg-row expected (test arg-row expected)))
  20. row
  21. expected))
  22. (define (arithmetic-binary-result-printer arg-row arg-col expect-col success?)
  23. (unless success?
  24. (print "Test failed for: "
  25. arg-row
  26. ", "
  27. arg-col
  28. ", "
  29. expect-col)))
  30. (define (arithmetic-unary-result-printer arg expect success?)
  31. (unless success?
  32. (print "Test failed for:"
  33. arg
  34. ", "
  35. expect)))
  36. (define (sci-num-match? a b)
  37. (define real-a (sci-num->number a))
  38. (define real-b (sci-num->number b))
  39. (cond
  40. ((finite? real-a)
  41. (and (finite? real-b)
  42. (equal? (to-fixed real-a 13)
  43. (to-fixed real-b 13))))
  44. ((nan? real-a)
  45. (nan? real-b))
  46. (else
  47. (= real-a real-b))))
  48. (define add-arguments
  49. '(+inf.0 -inf.0 +nan.0 0.0 1.0 -1.0 3.148 -5.143))
  50. (define mult-arguments add-arguments)
  51. (define expt-arguments mult-arguments)
  52. (define add-results
  53. '((+inf.0 +nan.0 +nan.0 +inf.0 +inf.0 +inf.0 +inf.0 +inf.0)
  54. (+nan.0 -inf.0 +nan.0 -inf.0 -inf.0 -inf.0 -inf.0 -inf.0)
  55. (+nan.0 +nan.0 +nan.0 +nan.0 +nan.0 +nan.0 +nan.0 +nan.0)
  56. (+inf.0 -inf.0 +nan.0 0.0 1.0 -1.0 3.148 -5.143)
  57. (+inf.0 -inf.0 +nan.0 1.0 2.0 0.0 4.148 -4.143)
  58. (+inf.0 -inf.0 +nan.0 -1.0 0.0 -2.0 2.148 -6.143)
  59. (+inf.0 -inf.0 +nan.0 3.148 4.148 2.148 6.296 -1.995)
  60. (+inf.0 -inf.0 +nan.0 -5.143 -4.143 -6.143 -1.995 -10.286)))
  61. (define mult-results
  62. '((+inf.0 -inf.0 +nan.0 +nan.0 +inf.0 -inf.0 +inf.0 -inf.0)
  63. (-inf.0 +inf.0 +nan.0 +nan.0 -inf.0 +inf.0 -inf.0 +inf.0)
  64. (+nan.0 +nan.0 +nan.0 +nan.0 +nan.0 +nan.0 +nan.0 +nan.0)
  65. (+nan.0 +nan.0 +nan.0 0.0 0.0 -0.0 0.0 -0.0)
  66. (+inf.0 -inf.0 +nan.0 0.0 1.0 -1.0 3.148 -5.143)
  67. (-inf.0 +inf.0 +nan.0 0.0 -1.0 1.0 -3.148 5.143)
  68. (+inf.0 -inf.0 +nan.0 0.0 3.148 -3.148 9.909904 -16.190164)
  69. (-inf.0 +inf.0 +nan.0 0.0 -5.143 5.143 -16.190164 26.450449)))
  70. (define expt-results
  71. '((+inf.0 0.0 +nan.0 1.0 +inf.0 0.0 +inf.0 0.0)
  72. (+nan.0 +nan.0 +nan.0 1.0 -inf.0 -0.0 +nan.0 -0.0)
  73. (+nan.0 +nan.0 +nan.0 1.0 +nan.0 +nan.0 +nan.0 +nan.0)
  74. (0.0 +inf.0 +nan.0 1.0 0.0 +inf.0 0.0 +inf.0)
  75. (1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0)
  76. (+nan.0 +nan.0 +nan.0 1.0 -1.0 -1.0 +nan.0 +nan.0)
  77. (+inf.0 0.0 +nan.0 1.0 3.148 0.3176620076238882 36.96692361914167 0.0027454112948776116)
  78. (+nan.0 +nan.0 +nan.0 1.0 -5.143 -0.19443904335990667 +nan.0 +nan.0)))
  79. (define sqrt-arguments mult-arguments)
  80. (define sqrt-results '(+inf.0 +nan.0 +nan.0 0.0 1 +nan.0))
  81. (print "Testing sci+")
  82. (print "--------------------")
  83. (print-test-table arithmetic-binary-result-printer
  84. (lambda (a b c)
  85. (sci-num-match? (sci+ a b) c))
  86. add-arguments
  87. add-arguments
  88. add-results)
  89. (print "Testing sci*")
  90. (print "--------------------")
  91. (print-test-table arithmetic-binary-result-printer
  92. (lambda (a b c)
  93. (sci-num-match? (sci* a b) c))
  94. mult-arguments
  95. mult-arguments
  96. mult-results)
  97. (print "Testing sci-expt")
  98. (print "--------------------")
  99. (print-test-table arithmetic-binary-result-printer
  100. (lambda (a b c)
  101. (sci-num-match? (sci-expt a b) c))
  102. expt-arguments
  103. expt-arguments
  104. expt-results)
  105. (print "Testing sci-sqrt")
  106. (print "--------------------")
  107. (print-test-row arithmetic-unary-result-printer
  108. (lambda (a b)
  109. (sci-num-match? (sci-sqrt a) b))
  110. sqrt-arguments
  111. sqrt-results)