arithmetic.sls 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!r6rs
  2. ;;; Copyright © 2016 Federico Beffa
  3. ;;;
  4. ;;; This program is free software; you can redistribute it and/or modify it
  5. ;;; under the terms of the GNU General Public License as published by
  6. ;;; the Free Software Foundation; either version 3 of the License, or (at
  7. ;;; your option) any later version.
  8. ;;;
  9. ;;; This program is distributed in the hope that it will be useful, but
  10. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;;; GNU General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU General Public License
  15. ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Code
  17. ;; R6RS doesn't allow 1+, 1-, ...
  18. #!chezscheme
  19. (library (mit arithmetic)
  20. (export fix:+ fix:- fix:= fix:> fix:< fix:remainder fix:quotient
  21. fix:xor index-fixnum?
  22. fix:<= fix:>= fix:* largest-fixnum fix:zero? fix:negative?
  23. fix:1+ fix:-1+
  24. fix:fixnum? fix:lsh
  25. int:+ int:- int:* int:quotient int:< int:> int:= int:zero?
  26. int:negate int:->flonum
  27. flo:= flo:< flo:> flo:- flo:+ flo:* flo:/ ;flo:flonum?
  28. flo:atan2 flo:log flo:abs flo:exp flo:sin flo:cos flo:tan
  29. flo:asin flo:acos flo:atan flo:sqrt flo:expt flo:floor flo:zero?
  30. flo:random-unit flo:negate flo:truncate->exact
  31. -1+ 1+ 1-
  32. ceiling->exact floor->exact round->exact truncate->exact
  33. integer-divide integer-divide-quotient integer-divide-remainder
  34. conjugate
  35. rationalize->exact
  36. real:*)
  37. (import (rnrs)
  38. (rnrs r5rs)
  39. (only (chezscheme) 1+ 1- random))
  40. (define fix:+ fx+)
  41. (define fix:- fx-)
  42. (define fix:= fx=?)
  43. (define fix:< fx<?)
  44. (define fix:<= fx<=?)
  45. (define fix:> fx>?)
  46. (define fix:>= fx>=?)
  47. (define fix:* fx*)
  48. (define fix:quotient quotient)
  49. (define fix:remainder fxmod)
  50. (define fix:xor fxxor)
  51. (define fix:zero? fxzero?)
  52. (define fix:negative? fxnegative?)
  53. (define (fix:-1+ x) (fix:+ -1 x))
  54. ;;(define fix:1+ 1+)
  55. (define (fix:1+ x) (fix:+ 1 x))
  56. (define fix:fixnum? fixnum?)
  57. (define index-fixnum? fixnum?)
  58. (define largest-fixnum greatest-fixnum)
  59. (define fix:lsh fxarithmetic-shift)
  60. (define int:+ fx+)
  61. (define int:- fx-)
  62. (define int:* fx*)
  63. (define int:quotient fxdiv)
  64. (define int:< fx<?)
  65. (define int:> fx>?)
  66. (define int:= fx=?)
  67. (define int:zero? fxzero?)
  68. (define (int:negate x) (int:- x))
  69. (define int:->flonum inexact)
  70. (define flo:= fl=?)
  71. (define flo:< fl<?)
  72. (define flo:> fl>?)
  73. (define flo:- fl-)
  74. (define flo:+ fl+)
  75. (define flo:* fl*)
  76. (define flo:/ fl/)
  77. (define (flo:negate x) (fl- x))
  78. (define (flo:truncate->exact x) (exact (truncate x)))
  79. ;;(define (flo:flonum? obj) (or (flonum? obj) (flo:vector? obj)))
  80. (define flo:atan2 flatan)
  81. (define flo:log fllog)
  82. (define flo:abs flabs)
  83. (define flo:exp flexp)
  84. (define flo:sin flsin)
  85. (define flo:cos flcos)
  86. (define flo:tan fltan)
  87. (define flo:asin flasin)
  88. (define flo:acos flacos)
  89. (define flo:atan flatan)
  90. (define flo:sqrt flsqrt)
  91. (define flo:expt flexpt)
  92. (define flo:floor flfloor)
  93. (define flo:zero? flzero?)
  94. (define (flo:random-unit state)
  95. (random 1.0))
  96. (define (-1+ num) (+ -1 num))
  97. (define (ceiling->exact number)
  98. (inexact->exact (ceiling number)))
  99. (define (floor->exact number)
  100. (inexact->exact (floor number)))
  101. (define (round->exact number)
  102. (inexact->exact (round number)))
  103. (define (truncate->exact number)
  104. (inexact->exact (truncate number)))
  105. (define (integer-divide x y)
  106. (cons (quotient x y)
  107. (remainder x y)))
  108. (define integer-divide-quotient car)
  109. (define integer-divide-remainder cdr)
  110. (define (conjugate z)
  111. (cond ((complex? z)
  112. (make-rectangular (real-part z)
  113. (- (imag-part z))))
  114. ((real? z)
  115. z)
  116. (else
  117. (error 'CONJUGATE "wrong-type-argument" z #f))))
  118. (define (rationalize->exact r1 r2)
  119. (rationalize (exact r1) (exact r2)))
  120. (define (real:* x y)
  121. (unless (and (real? x) (real? y))
  122. (error 'real:* "Invalid arguments" x y))
  123. (* x y))
  124. )