flonums.scm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. ;;; flonums.scm --- The R6RS flonums arithmetic library
  2. ;; Copyright (C) 2010, 2011, 2013 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (library (rnrs arithmetic flonums (6))
  18. (export flonum?
  19. real->flonum
  20. fl=? fl<? fl<=? fl>? fl>=?
  21. flinteger? flzero? flpositive? flnegative? flodd? fleven? flfinite?
  22. flinfinite? flnan?
  23. flmax flmin
  24. fl+ fl* fl- fl/
  25. flabs
  26. fldiv-and-mod
  27. fldiv
  28. flmod
  29. fldiv0-and-mod0
  30. fldiv0
  31. flmod0
  32. flnumerator
  33. fldenominator
  34. flfloor flceiling fltruncate flround
  35. flexp fllog flsin flcos fltan flacos flasin flatan
  36. flsqrt flexpt
  37. &no-infinities
  38. make-no-infinities-violation
  39. no-infinities-violation?
  40. &no-nans
  41. make-no-nans-violation
  42. no-nans-violation?
  43. fixnum->flonum)
  44. (import (ice-9 optargs)
  45. (only (guile) inf?)
  46. (rnrs arithmetic fixnums (6))
  47. (rnrs base (6))
  48. (rnrs control (6))
  49. (rnrs conditions (6))
  50. (rnrs exceptions (6))
  51. (rnrs lists (6))
  52. (rnrs r5rs (6)))
  53. (define (flonum? obj) (and (real? obj) (inexact? obj)))
  54. (define (assert-flonum . args)
  55. (or (for-all flonum? args) (raise (make-assertion-violation))))
  56. (define (assert-iflonum . args)
  57. (or (for-all (lambda (i) (and (flonum? i) (integer? i))) args)
  58. (raise (make-assertion-violation))))
  59. (define (ensure-flonum z)
  60. (cond ((real? z) z)
  61. ((zero? (imag-part z)) (real-part z))
  62. (else +nan.0)))
  63. (define (real->flonum x)
  64. (or (real? x) (raise (make-assertion-violation)))
  65. (exact->inexact x))
  66. (define (fl=? . args) (apply assert-flonum args) (apply = args))
  67. (define (fl<? . args) (apply assert-flonum args) (apply < args))
  68. (define (fl<=? . args) (apply assert-flonum args) (apply <= args))
  69. (define (fl>? . args) (apply assert-flonum args) (apply > args))
  70. (define (fl>=? . args) (apply assert-flonum args) (apply >= args))
  71. (define (flinteger? fl) (assert-flonum fl) (integer? fl))
  72. (define (flzero? fl) (assert-flonum fl) (zero? fl))
  73. (define (flpositive? fl) (assert-flonum fl) (positive? fl))
  74. (define (flnegative? fl) (assert-flonum fl) (negative? fl))
  75. (define (flodd? ifl) (assert-iflonum ifl) (odd? ifl))
  76. (define (fleven? ifl) (assert-iflonum ifl) (even? ifl))
  77. (define (flfinite? fl) (assert-flonum fl) (not (or (inf? fl) (nan? fl))))
  78. (define (flinfinite? fl) (assert-flonum fl) (inf? fl))
  79. (define (flnan? fl) (assert-flonum fl) (nan? fl))
  80. (define (flmax fl1 . args)
  81. (let ((flargs (cons fl1 args)))
  82. (apply assert-flonum flargs)
  83. (apply max flargs)))
  84. (define (flmin fl1 . args)
  85. (let ((flargs (cons fl1 args)))
  86. (apply assert-flonum flargs)
  87. (apply min flargs)))
  88. (define (fl+ . args)
  89. (apply assert-flonum args)
  90. (if (null? args) 0.0 (apply + args)))
  91. (define (fl* . args)
  92. (apply assert-flonum args)
  93. (if (null? args) 1.0 (apply * args)))
  94. (define (fl- fl1 . args)
  95. (let ((flargs (cons fl1 args)))
  96. (apply assert-flonum flargs)
  97. (apply - flargs)))
  98. (define (fl/ fl1 . args)
  99. (let ((flargs (cons fl1 args)))
  100. (apply assert-flonum flargs)
  101. (apply / flargs)))
  102. (define (flabs fl) (assert-flonum fl) (abs fl))
  103. (define (fldiv-and-mod fl1 fl2)
  104. (assert-iflonum fl1 fl2)
  105. (div-and-mod fl1 fl2))
  106. (define (fldiv fl1 fl2)
  107. (assert-iflonum fl1 fl2)
  108. (div fl1 fl2))
  109. (define (flmod fl1 fl2)
  110. (assert-iflonum fl1 fl2)
  111. (mod fl1 fl2))
  112. (define (fldiv0-and-mod0 fl1 fl2)
  113. (assert-iflonum fl1 fl2)
  114. (div0-and-mod0 fl1 fl2))
  115. (define (fldiv0 fl1 fl2)
  116. (assert-iflonum fl1 fl2)
  117. (div0 fl1 fl2))
  118. (define (flmod0 fl1 fl2)
  119. (assert-iflonum fl1 fl2)
  120. (mod0 fl1 fl2))
  121. (define (flnumerator fl) (assert-flonum fl) (numerator fl))
  122. (define (fldenominator fl) (assert-flonum fl) (denominator fl))
  123. (define (flfloor fl) (assert-flonum fl) (floor fl))
  124. (define (flceiling fl) (assert-flonum fl) (ceiling fl))
  125. (define (fltruncate fl) (assert-flonum fl) (truncate fl))
  126. (define (flround fl) (assert-flonum fl) (round fl))
  127. (define (flexp fl) (assert-flonum fl) (exp fl))
  128. (define fllog
  129. (case-lambda
  130. ((fl)
  131. (assert-flonum fl)
  132. ;; add 0.0 to fl, to change -0.0 to 0.0,
  133. ;; so that (fllog -0.0) will be -inf.0, not -inf.0+pi*i.
  134. (ensure-flonum (log (+ fl 0.0))))
  135. ((fl fl2)
  136. (assert-flonum fl fl2)
  137. (ensure-flonum (/ (log (+ fl 0.0))
  138. (log (+ fl2 0.0)))))))
  139. (define (flsin fl) (assert-flonum fl) (sin fl))
  140. (define (flcos fl) (assert-flonum fl) (cos fl))
  141. (define (fltan fl) (assert-flonum fl) (tan fl))
  142. (define (flasin fl) (assert-flonum fl) (ensure-flonum (asin fl)))
  143. (define (flacos fl) (assert-flonum fl) (ensure-flonum (acos fl)))
  144. (define flatan
  145. (case-lambda
  146. ((fl) (assert-flonum fl) (atan fl))
  147. ((fl fl2) (assert-flonum fl fl2) (atan fl fl2))))
  148. (define (flsqrt fl) (assert-flonum fl) (ensure-flonum (sqrt fl)))
  149. (define (flexpt fl1 fl2) (assert-flonum fl1 fl2) (ensure-flonum (expt fl1 fl2)))
  150. (define-condition-type &no-infinities
  151. &implementation-restriction
  152. make-no-infinities-violation
  153. no-infinities-violation?)
  154. (define-condition-type &no-nans
  155. &implementation-restriction
  156. make-no-nans-violation
  157. no-nans-violation?)
  158. (define (fixnum->flonum fx)
  159. (or (fixnum? fx) (raise (make-assertion-violation)))
  160. (exact->inexact fx))
  161. )