calc-macs.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. ;;; calc-macs.el --- important macros for Calc
  2. ;; Copyright (C) 1990-1993, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: David Gillespie <daveg@synaptics.com>
  4. ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; Declare functions which are defined elsewhere.
  19. (declare-function math-zerop "calc-misc" (a))
  20. (declare-function math-negp "calc-misc" (a))
  21. (declare-function math-looks-negp "calc-misc" (a))
  22. (declare-function math-posp "calc-misc" (a))
  23. (declare-function math-compare "calc-ext" (a b))
  24. (declare-function math-bignum "calc" (a))
  25. (declare-function math-compare-bignum "calc-ext" (a b))
  26. (defmacro calc-wrapper (&rest body)
  27. `(calc-do (function (lambda ()
  28. ,@body))))
  29. (defmacro calc-slow-wrapper (&rest body)
  30. `(calc-do
  31. (function (lambda () ,@body)) (point)))
  32. (defmacro math-showing-full-precision (form)
  33. `(let ((calc-float-format calc-full-float-format))
  34. ,form))
  35. (defmacro math-with-extra-prec (delta &rest body)
  36. `(math-normalize
  37. (let ((calc-internal-prec (+ calc-internal-prec ,delta)))
  38. ,@body)))
  39. (defmacro math-working (msg arg) ; [Public]
  40. `(if (eq calc-display-working-message 'lots)
  41. (math-do-working ,msg ,arg)))
  42. (defmacro calc-with-default-simplification (&rest body)
  43. `(let ((calc-simplify-mode (and (not (memq calc-simplify-mode '(none num)))
  44. calc-simplify-mode)))
  45. ,@body))
  46. (defmacro calc-with-trail-buffer (&rest body)
  47. `(let ((save-buf (current-buffer))
  48. (calc-command-flags nil))
  49. (with-current-buffer (calc-trail-display t)
  50. (progn
  51. (goto-char calc-trail-pointer)
  52. ,@body))))
  53. ;;; Faster in-line version zerop, normalized values only.
  54. (defsubst Math-zerop (a) ; [P N]
  55. (if (consp a)
  56. (and (not (memq (car a) '(bigpos bigneg)))
  57. (if (eq (car a) 'float)
  58. (eq (nth 1 a) 0)
  59. (math-zerop a)))
  60. (eq a 0)))
  61. (defsubst Math-integer-negp (a)
  62. (if (consp a)
  63. (eq (car a) 'bigneg)
  64. (< a 0)))
  65. (defsubst Math-integer-posp (a)
  66. (if (consp a)
  67. (eq (car a) 'bigpos)
  68. (> a 0)))
  69. (defsubst Math-negp (a)
  70. (if (consp a)
  71. (or (eq (car a) 'bigneg)
  72. (and (not (eq (car a) 'bigpos))
  73. (if (memq (car a) '(frac float))
  74. (Math-integer-negp (nth 1 a))
  75. (math-negp a))))
  76. (< a 0)))
  77. (defsubst Math-looks-negp (a) ; [P x] [Public]
  78. (or (Math-negp a)
  79. (and (consp a) (or (eq (car a) 'neg)
  80. (and (memq (car a) '(* /))
  81. (or (math-looks-negp (nth 1 a))
  82. (math-looks-negp (nth 2 a))))))))
  83. (defsubst Math-posp (a)
  84. (if (consp a)
  85. (or (eq (car a) 'bigpos)
  86. (and (not (eq (car a) 'bigneg))
  87. (if (memq (car a) '(frac float))
  88. (Math-integer-posp (nth 1 a))
  89. (math-posp a))))
  90. (> a 0)))
  91. (defsubst Math-integerp (a)
  92. (or (not (consp a))
  93. (memq (car a) '(bigpos bigneg))))
  94. (defsubst Math-natnump (a)
  95. (if (consp a)
  96. (eq (car a) 'bigpos)
  97. (>= a 0)))
  98. (defsubst Math-ratp (a)
  99. (or (not (consp a))
  100. (memq (car a) '(bigpos bigneg frac))))
  101. (defsubst Math-realp (a)
  102. (or (not (consp a))
  103. (memq (car a) '(bigpos bigneg frac float))))
  104. (defsubst Math-anglep (a)
  105. (or (not (consp a))
  106. (memq (car a) '(bigpos bigneg frac float hms))))
  107. (defsubst Math-numberp (a)
  108. (or (not (consp a))
  109. (memq (car a) '(bigpos bigneg frac float cplx polar))))
  110. (defsubst Math-scalarp (a)
  111. (or (not (consp a))
  112. (memq (car a) '(bigpos bigneg frac float cplx polar hms))))
  113. (defsubst Math-vectorp (a)
  114. (and (consp a) (eq (car a) 'vec)))
  115. (defsubst Math-messy-integerp (a)
  116. (and (consp a)
  117. (eq (car a) 'float)
  118. (>= (nth 2 a) 0)))
  119. (defsubst Math-objectp (a) ; [Public]
  120. (or (not (consp a))
  121. (memq (car a)
  122. '(bigpos bigneg frac float cplx polar hms date sdev intv mod))))
  123. (defsubst Math-objvecp (a) ; [Public]
  124. (or (not (consp a))
  125. (memq (car a)
  126. '(bigpos bigneg frac float cplx polar hms date
  127. sdev intv mod vec))))
  128. ;;; Compute the negative of A. [O O; o o] [Public]
  129. (defsubst Math-integer-neg (a)
  130. (if (consp a)
  131. (if (eq (car a) 'bigpos)
  132. (cons 'bigneg (cdr a))
  133. (cons 'bigpos (cdr a)))
  134. (- a)))
  135. (defsubst Math-equal (a b)
  136. (= (math-compare a b) 0))
  137. (defsubst Math-lessp (a b)
  138. (= (math-compare a b) -1))
  139. (defsubst Math-primp (a)
  140. (or (not (consp a))
  141. (memq (car a) '(bigpos bigneg frac float cplx polar
  142. hms date mod var))))
  143. (defsubst Math-num-integerp (a)
  144. (or (not (consp a))
  145. (memq (car a) '(bigpos bigneg))
  146. (and (eq (car a) 'float)
  147. (>= (nth 2 a) 0))))
  148. (defsubst Math-bignum-test (a) ; [B N; B s; b b]
  149. (if (consp a)
  150. a
  151. (math-bignum a)))
  152. (defsubst Math-equal-int (a b)
  153. (or (eq a b)
  154. (and (consp a)
  155. (eq (car a) 'float)
  156. (eq (nth 1 a) b)
  157. (= (nth 2 a) 0))))
  158. (defsubst Math-natnum-lessp (a b)
  159. (if (consp a)
  160. (and (consp b)
  161. (= (math-compare-bignum (cdr a) (cdr b)) -1))
  162. (or (consp b)
  163. (< a b))))
  164. (provide 'calc-macs)
  165. ;;; calc-macs.el ends here