emacs/lisp/calc/calc-macs.el
Stefan Monnier 1bc1672f77 * lisp/calc/calc.el: Take advantage of native bignums.
Remove redundant :group args.

(calc-trail-mode): Use inhibit-read-only.
(math-bignum-digit-length, math-bignum-digit-size)
(math-small-integer-size): Delete constants.
(math-normalize): Use native bignums.
(math-bignum, math-bignum-big): Delete functions.
(math-make-float): The mantissa can't be a calc bignum any more.
(math-neg, math-scale-left, math-scale-right, math-scale-rounding)
(math-add, math-sub, math-mul, math-idivmod, math-quotient)
(math-format-number, math-read-number, math-read-number-simple):
Don't bother handling calc bignums.
(math-div10-bignum, math-scale-left-bignum, math-scale-right-bignum)
(math-add-bignum, math-sub-bignum, math-mul-bignum, math-mul-bignum-digit)
(math-div-bignum, math-div-bignum-digit, math-div-bignum-big)
(math-div-bignum-part, math-div-bignum-try, math-format-bignum)
(math-format-bignum-decimal, math-read-bignum): Delete functions.
(math-numdigs): Don't presume that native ints are small enough to use
a slow algorithm.

* lisp/calc/calc-aent.el (calc-do-quick-calc):
* lisp/calc/calc-vec.el (calcFunc-vunpack):
* lisp/calc/calc-alg.el (math-beforep): Don't bother handling calc bignums.

* lisp/calc/calc-bin.el (math-bignum-logb-digit-size)
(math-bignum-digit-power-of-two): Remove constants.
(calcFunc-and, math-binary-arg, calcFunc-or, calcFunc-xor)
(calcFunc-diff, calcFunc-not, math-clip, math-format-twos-complement):
Use Emacs's builtin bignums.
(math-and-bignum, math-or-bignum, math-xor-bignum, math-diff-bignum)
(math-not-bignum, math-clip-bignum)
(math-format-bignum-radix, math-format-bignum-binary)
(math-format-bignum-octal, math-format-bignum-hex): Delete functions.
(math-format-binary): Fix old copy&paste error.

* lisp/calc/calc-comb.el (calc-prime-factors): Adjust for unused arg.
(math-prime-test): math-fixnum is now the identity.

* lisp/calc/calc-ext.el: Require cl-lib.
(math-oddp): Use cl-oddp.  Don't bother with calc bignums.
(math-integerp, math-natnump, math-ratp, math-realp, math-anglep)
(math-numberp, math-scalarp, math-vectorp, math-objvecp, math-primp)
(math-num-natnump, math-objectp, math-check-integer, math-compare):
Don't bother handling calc bignums.
(math-check-fixnum): Use fixnump.
(math-fixnum, math-fixnum-big, math-bignum-test): Remove functions.
(math--format-integer-fancy): Rename from math-format-bignum-fancy.
Adjust for internal bignums.

* lisp/calc/calc-funcs.el (calcFunc-besJ): Use cl-isqrt.

* lisp/calc/calc-macs.el (Math-zerop, Math-integer-negp)
(Math-integer-posp, Math-negp, Math-posp, Math-integerp)
(Math-natnump, Math-ratp, Math-realp, Math-anglep, Math-numberp)
(Math-scalarp, Math-vectorp, Math-objectp, Math-objvecp)
(Math-integer-neg, Math-primp, Math-num-integerp):
Don't bother handling calc bignums.
(Math-bignum-test): Delete function.

* lisp/calc/calc-math.el (math-use-emacs-fn): Remove unused `fx`.
(math-isqrt, math-sqrt): Use cl-isqrt.  Don't bother handling calc bignums.
(math-isqrt-bignum, math-isqrt-bignum-iter, math-isqrt-small):
Delete function.

* lisp/calc/calc-misc.el (math-fixnump, math-fixnatnump): Use fixnump.
(math-evenp): Use cl-evenp.
(math-zerop, math-negp, math-posp, math-div2): Don't bother handling
calc bignums.
(math-div2-bignum): Delete function.
2019-06-25 23:05:11 -04:00

184 lines
4.7 KiB
EmacsLisp

;;; calc-macs.el --- important macros for Calc -*- lexical-binding:t -*-
;; Copyright (C) 1990-1993, 2001-2019 Free Software Foundation, Inc.
;; Author: David Gillespie <daveg@synaptics.com>
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
;; Declare functions which are defined elsewhere.
(declare-function math-zerop "calc-misc" (a))
(declare-function math-negp "calc-misc" (a))
(declare-function math-looks-negp "calc-misc" (a))
(declare-function math-posp "calc-misc" (a))
(declare-function math-compare "calc-ext" (a b))
(declare-function math-compare-bignum "calc-ext" (a b))
(defmacro calc-wrapper (&rest body)
`(calc-do (function (lambda ()
,@body))))
(defmacro calc-slow-wrapper (&rest body)
`(calc-do
(function (lambda () ,@body)) (point)))
(defmacro math-showing-full-precision (form)
`(let ((calc-float-format calc-full-float-format))
,form))
(defmacro math-with-extra-prec (delta &rest body)
`(math-normalize
(let ((calc-internal-prec (+ calc-internal-prec ,delta)))
,@body)))
(defmacro math-working (msg arg) ; [Public]
`(if (eq calc-display-working-message 'lots)
(math-do-working ,msg ,arg)))
(defmacro calc-with-default-simplification (&rest body)
`(let ((calc-simplify-mode (and (not (memq calc-simplify-mode '(none num)))
calc-simplify-mode)))
,@body))
(defmacro calc-with-trail-buffer (&rest body)
`(let ((save-buf (current-buffer))
(calc-command-flags nil))
(with-current-buffer (calc-trail-display t)
(progn
(goto-char calc-trail-pointer)
,@body))))
;;; Faster in-line version zerop, normalized values only.
(defsubst Math-zerop (a) ; [P N]
(if (consp a)
(if (eq (car a) 'float)
(eq (nth 1 a) 0)
(math-zerop a))
(eq a 0)))
(defsubst Math-integer-negp (a)
(and (integerp a) (< a 0)))
(defsubst Math-integer-posp (a)
(and (integerp a) (> a 0)))
(defsubst Math-negp (a)
(if (consp a)
(if (memq (car a) '(frac float))
(Math-integer-negp (nth 1 a))
(math-negp a))
(< a 0)))
(defsubst Math-looks-negp (a) ; [P x] [Public]
(or (Math-negp a)
(and (consp a) (or (eq (car a) 'neg)
(and (memq (car a) '(* /))
(or (math-looks-negp (nth 1 a))
(math-looks-negp (nth 2 a))))))))
(defsubst Math-posp (a)
(if (consp a)
(if (memq (car a) '(frac float))
(Math-integer-posp (nth 1 a))
(math-posp a))
(> a 0)))
(defalias 'Math-integerp #'integerp)
(defsubst Math-natnump (a)
(and (integerp a) (>= a 0)))
(defsubst Math-ratp (a)
(or (not (consp a))
(eq (car a) 'frac)))
(defsubst Math-realp (a)
(or (not (consp a))
(memq (car a) '(frac float))))
(defsubst Math-anglep (a)
(or (not (consp a))
(memq (car a) '(frac float hms))))
(defsubst Math-numberp (a)
(or (not (consp a))
(memq (car a) '(frac float cplx polar))))
(defsubst Math-scalarp (a)
(or (not (consp a))
(memq (car a) '(frac float cplx polar hms))))
(defsubst Math-vectorp (a)
(eq (car-safe a) 'vec))
(defsubst Math-messy-integerp (a)
(and (consp a)
(eq (car a) 'float)
(>= (nth 2 a) 0)))
(defsubst Math-objectp (a) ; [Public]
(or (not (consp a))
(memq (car a)
'(frac float cplx polar hms date sdev intv mod))))
(defsubst Math-objvecp (a) ; [Public]
(or (not (consp a))
(memq (car a)
'(frac float cplx polar hms date
sdev intv mod vec))))
;;; Compute the negative of A. [O O; o o] [Public]
(defsubst Math-integer-neg (a)
(- a))
(defsubst Math-equal (a b)
(= (math-compare a b) 0))
(defsubst Math-lessp (a b)
(= (math-compare a b) -1))
(defsubst Math-primp (a)
(or (not (consp a))
(memq (car a) '(frac float cplx polar
hms date mod var))))
(defsubst Math-num-integerp (a)
(or (not (consp a))
(and (eq (car a) 'float)
(>= (nth 2 a) 0))))
(defsubst Math-equal-int (a b)
(or (eq a b)
(and (consp a)
(eq (car a) 'float)
(eq (nth 1 a) b)
(= (nth 2 a) 0))))
(defsubst Math-natnum-lessp (a b)
(if (consp a)
(and (consp b)
(= (math-compare-bignum (cdr a) (cdr b)) -1))
(or (consp b)
(< a b))))
(provide 'calc-macs)
;;; calc-macs.el ends here