calc-macs.el 5.4 KB

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