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