2020-10-10 16:00:51 -04:00
|
|
|
;;; calc-bin.el --- binary functions for Calc -*- lexical-binding:t -*-
|
2001-11-19 07:44:56 +00:00
|
|
|
|
2021-01-01 01:13:56 -08:00
|
|
|
;; Copyright (C) 1990-1993, 2001-2021 Free Software Foundation, Inc.
|
2001-11-19 07:44:56 +00:00
|
|
|
|
|
|
|
;; Author: David Gillespie <daveg@synaptics.com>
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2008-05-06 03:16:00 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2007-03-19 20:59:53 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 03:16:00 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
2007-03-19 20:59:53 +00:00
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2007-03-19 20:59:53 +00:00
|
|
|
;; 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
|
2017-09-13 15:52:52 -07:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2001-11-19 07:44:56 +00:00
|
|
|
;;; Commentary:
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2001-11-19 07:44:56 +00:00
|
|
|
;;; Code:
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;; This file is autoloaded from calc-ext.el.
|
|
|
|
|
2004-11-30 17:02:05 +00:00
|
|
|
(require 'calc-ext)
|
2001-11-06 18:59:06 +00:00
|
|
|
(require 'calc-macs)
|
|
|
|
|
|
|
|
;;; b-prefix binary commands.
|
|
|
|
|
|
|
|
(defun calc-and (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(calc-enter-result 2 "and"
|
|
|
|
(append '(calcFunc-and)
|
|
|
|
(calc-top-list-n 2)
|
2001-11-14 09:01:51 +00:00
|
|
|
(and n (list (prefix-numeric-value n)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-or (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(calc-enter-result 2 "or"
|
|
|
|
(append '(calcFunc-or)
|
|
|
|
(calc-top-list-n 2)
|
2001-11-14 09:01:51 +00:00
|
|
|
(and n (list (prefix-numeric-value n)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-xor (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(calc-enter-result 2 "xor"
|
|
|
|
(append '(calcFunc-xor)
|
|
|
|
(calc-top-list-n 2)
|
2001-11-14 09:01:51 +00:00
|
|
|
(and n (list (prefix-numeric-value n)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-diff (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(calc-enter-result 2 "diff"
|
|
|
|
(append '(calcFunc-diff)
|
|
|
|
(calc-top-list-n 2)
|
2001-11-14 09:01:51 +00:00
|
|
|
(and n (list (prefix-numeric-value n)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-not (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(calc-enter-result 1 "not"
|
|
|
|
(append '(calcFunc-not)
|
|
|
|
(calc-top-list-n 1)
|
2001-11-14 09:01:51 +00:00
|
|
|
(and n (list (prefix-numeric-value n)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-lshift-binary (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(let ((hyp (if (calc-is-hyperbolic) 2 1)))
|
|
|
|
(calc-enter-result hyp "lsh"
|
|
|
|
(append '(calcFunc-lsh)
|
|
|
|
(calc-top-list-n hyp)
|
2001-11-14 09:01:51 +00:00
|
|
|
(and n (list (prefix-numeric-value n))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-rshift-binary (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(let ((hyp (if (calc-is-hyperbolic) 2 1)))
|
|
|
|
(calc-enter-result hyp "rsh"
|
|
|
|
(append '(calcFunc-rsh)
|
|
|
|
(calc-top-list-n hyp)
|
2001-11-14 09:01:51 +00:00
|
|
|
(and n (list (prefix-numeric-value n))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-lshift-arith (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(let ((hyp (if (calc-is-hyperbolic) 2 1)))
|
|
|
|
(calc-enter-result hyp "ash"
|
|
|
|
(append '(calcFunc-ash)
|
|
|
|
(calc-top-list-n hyp)
|
2001-11-14 09:01:51 +00:00
|
|
|
(and n (list (prefix-numeric-value n))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-rshift-arith (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(let ((hyp (if (calc-is-hyperbolic) 2 1)))
|
|
|
|
(calc-enter-result hyp "rash"
|
|
|
|
(append '(calcFunc-rash)
|
|
|
|
(calc-top-list-n hyp)
|
2001-11-14 09:01:51 +00:00
|
|
|
(and n (list (prefix-numeric-value n))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-rotate-binary (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(let ((hyp (if (calc-is-hyperbolic) 2 1)))
|
|
|
|
(calc-enter-result hyp "rot"
|
|
|
|
(append '(calcFunc-rot)
|
|
|
|
(calc-top-list-n hyp)
|
2001-11-14 09:01:51 +00:00
|
|
|
(and n (list (prefix-numeric-value n))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-clip (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-slow-wrapper
|
|
|
|
(calc-enter-result 1 "clip"
|
|
|
|
(append '(calcFunc-clip)
|
|
|
|
(calc-top-list-n 1)
|
2001-11-14 09:01:51 +00:00
|
|
|
(and n (list (prefix-numeric-value n)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-word-size (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-wrapper
|
Use `format-prompt' when prompting with default values
* lisp/woman.el (woman-file-name):
* lisp/wid-edit.el (widget-file-prompt-value)
(widget-coding-system-prompt-value):
* lisp/w32-fns.el (w32-set-system-coding-system):
* lisp/vc/vc.el (vc-print-root-log):
* lisp/vc/vc-annotate.el (vc-annotate):
* lisp/vc/emerge.el (emerge-read-file-name):
* lisp/vc/ediff.el (ediff-directories)
(ediff-directory-revisions, ediff-directories3)
(ediff-merge-directories, )
(ediff-merge-directories-with-ancestor)
(ediff-merge-directory-revisions)
(ediff-merge-directory-revisions-with-ancestor)
(ediff-merge-revisions, ediff-merge-revisions-with-ancestor)
(ediff-revision):
* lisp/vc/ediff-util.el (ediff-toggle-regexp-match):
* lisp/vc/ediff-mult.el (ediff-filegroup-action):
* lisp/vc/add-log.el (prompt-for-change-log-name):
* lisp/textmodes/table.el (table-insert-row-column)
(table-span-cell, table-split-cell-horizontally)
(table-split-cell, table-justify, table-generate-source)
(table-insert-sequence, table-capture)
(table--read-from-minibuffer, table--query-justification):
* lisp/textmodes/sgml-mode.el (sgml-tag, sgml-tag-help):
* lisp/textmodes/reftex-ref.el (reftex-goto-label):
* lisp/textmodes/refer.el (refer-get-bib-files):
* lisp/textmodes/css-mode.el (css-lookup-symbol):
* lisp/term.el (serial-read-name, serial-read-speed):
* lisp/speedbar.el (speedbar-change-initial-expansion-list):
* lisp/simple.el (previous-matching-history-element)
(set-variable):
* lisp/ses.el (ses-read-cell, ses-set-column-width):
* lisp/replace.el (query-replace-read-from)
(occur-read-primary-args):
* lisp/rect.el (string-rectangle, string-insert-rectangle):
* lisp/progmodes/tcl.el (tcl-help-on-word):
* lisp/progmodes/sh-script.el (sh-set-shell):
* lisp/progmodes/python.el (python-eldoc-at-point):
* lisp/progmodes/octave.el (octave-completing-read)
(octave-update-function-file-comment, octave-insert-defun):
* lisp/progmodes/inf-lisp.el (lisp-symprompt):
* lisp/progmodes/cperl-mode.el (cperl-info-on-command)
(cperl-perldoc):
* lisp/progmodes/compile.el (compilation-find-file):
* lisp/net/rcirc.el (rcirc-prompt-for-encryption):
* lisp/net/eww.el (eww):
* lisp/net/browse-url.el (browse-url-with-browser-kind):
* lisp/man.el (man):
* lisp/mail/sendmail.el (sendmail-query-user-about-smtp):
* lisp/mail/mailalias.el (build-mail-aliases):
* lisp/mail/mailabbrev.el (merge-mail-abbrevs)
(rebuild-mail-abbrevs):
* lisp/locate.el (locate-prompt-for-search-string):
* lisp/isearch.el (isearch-occur):
* lisp/international/ogonek.el (ogonek-read-encoding)
(ogonek-read-prefix):
* lisp/international/mule.el (read-buffer-file-coding-system)
(set-terminal-coding-system, set-keyboard-coding-system)
(set-next-selection-coding-system, recode-region):
* lisp/international/mule-cmds.el ()
(universal-coding-system-argument, search-unencodable-char)
(select-safe-coding-system-interactively):
* lisp/info.el (Info-search, Info-search-backward, Info-menu):
* lisp/info-look.el (info-lookup-interactive-arguments):
* lisp/imenu.el (imenu--completion-buffer):
* lisp/ibuf-ext.el (mode, used-mode, ibuffer-mark-by-mode):
* lisp/hi-lock.el (hi-lock-unface-buffer)
(hi-lock-read-face-name):
* lisp/help.el (view-emacs-news, where-is):
* lisp/help-fns.el (describe-variable, describe-symbol)
(describe-keymap):
* lisp/gnus/mm-decode.el (mm-save-part):
* lisp/gnus/gnus-sum.el (gnus-summary-browse-url):
* lisp/gnus/gnus-group.el (gnus-group--read-bug-ids)
(gnus-group-set-current-level):
* lisp/frame.el (make-frame-on-monitor)
(close-display-connection, select-frame-by-name):
* lisp/format.el (format-encode-buffer, format-encode-region):
* lisp/files.el (recode-file-name):
* lisp/files-x.el (read-file-local-variable)
(read-file-local-variable-value, )
(read-file-local-variable-mode):
* lisp/ffap.el (ffap-menu-ask):
* lisp/faces.el (face-read-string):
* lisp/facemenu.el (facemenu-set-charset):
* lisp/erc/erc-dcc.el (erc-dcc-do-GET-command):
* lisp/emulation/edt-mapper.el (edt-mapper):
* lisp/emacs-lisp/trace.el (trace--read-args)
(trace-function-foreground, trace-function-background):
* lisp/emacs-lisp/smie.el (smie-config-set-indent):
* lisp/emacs-lisp/re-builder.el (reb-change-syntax):
* lisp/emacs-lisp/package.el (describe-package):
* lisp/emacs-lisp/find-func.el (read-library-name)
(find-function-read):
* lisp/emacs-lisp/ert.el (ert-read-test-name)
(ert-run-tests-interactively):
* lisp/emacs-lisp/disass.el (disassemble):
* lisp/emacs-lisp/debug.el (debug-on-entry)
(debug-on-variable-change):
* lisp/emacs-lisp/advice.el (ad-read-advised-function)
(ad-read-advice-class, ad-read-advice-name, ad-read-regexp):
* lisp/dired-x.el (dired--mark-suffix-interactive-spec):
* lisp/dired-aux.el (dired-diff):
* lisp/cus-edit.el (custom-variable-prompt, customize-mode)
(customize-changed-options):
* lisp/completion.el (interactive-completion-string-reader):
* lisp/calendar/timeclock.el (timeclock-ask-for-project):
* lisp/calc/calcalg3.el (calc-get-fit-variables):
* lisp/calc/calc-store.el (calc-edit-variable):
* lisp/calc/calc-bin.el (calc-word-size):
* lisp/bookmark.el (bookmark-set-internal):
* lisp/abbrev.el (read-abbrev-file): Use `format-prompt' for
prompting (bug#12443).
2020-09-06 16:56:44 +02:00
|
|
|
(or n (setq n (read-string (format-prompt "Binary word size"
|
|
|
|
calc-word-size))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(setq n (if (stringp n)
|
|
|
|
(if (equal n "")
|
|
|
|
calc-word-size
|
|
|
|
(if (string-match "\\`[-+]?[0-9]+\\'" n)
|
2005-05-02 19:41:27 +00:00
|
|
|
(string-to-number n)
|
2001-11-06 18:59:06 +00:00
|
|
|
(error "Expected an integer")))
|
|
|
|
(prefix-numeric-value n)))
|
|
|
|
(or (= n calc-word-size)
|
|
|
|
(if (> (math-abs n) 100)
|
|
|
|
(calc-change-mode 'calc-word-size n calc-leading-zeros)
|
|
|
|
(calc-change-mode '(calc-word-size calc-previous-modulo)
|
|
|
|
(list n (math-power-of-2 (math-abs n)))
|
|
|
|
calc-leading-zeros)))
|
2009-11-16 00:00:22 +00:00
|
|
|
(setq math-2-word-size (math-power-of-2 (math-abs n)))
|
|
|
|
(setq math-half-2-word-size (math-power-of-2 (1- (math-abs n))))
|
|
|
|
(calc-do-refresh)
|
|
|
|
(calc-refresh-evaltos)
|
Calc: allow infinite binary word size (bug#43764)
Setting the word size ("b w") to 0 removes the word size clipping for
all bit operations (effectively as if a word size of -∞ had been set).
Rotation is disallowed; logical and arithmetic shifts behave
identically.
After a suggestion by Vincent Belaïche.
* lisp/calc/calc-bin.el (calc-word-size, math-binary-arg)
(math-binary-modulo-args, calcFunc-lsh, calcFunc-ash, calcFunc-rot)
(math-clip, math-format-twos-complement): Allow a word size of 0,
meaning -∞.
* test/lisp/calc/calc-tests.el
(calc-tests--not, calc-tests--and, calc-tests--or, calc-tests--xor)
(calc-tests--diff): New functions.
(calc-tests--clip, calc-tests--rot, calc-shift-binary): Extend to
cover word size 0.
(calc-bit-ops): New test.
* doc/misc/calc.texi (Binary Functions): Update manual.
* etc/NEWS: Announce the change.
2020-10-10 18:02:49 +02:00
|
|
|
(cond
|
|
|
|
((< n 0) (message "Binary word size is %d bits (two's complement)" (- n)))
|
|
|
|
((> n 0) (message "Binary word size is %d bits" n))
|
|
|
|
(t (message "No fixed binary word size")))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; d-prefix mode commands.
|
|
|
|
|
2009-11-16 00:00:22 +00:00
|
|
|
(defun calc-radix (n &optional arg)
|
2001-11-06 18:59:06 +00:00
|
|
|
(interactive "NDisplay radix (2-36): ")
|
|
|
|
(calc-wrapper
|
|
|
|
(if (and (>= n 2) (<= n 36))
|
|
|
|
(progn
|
2017-09-13 15:52:52 -07:00
|
|
|
(calc-change-mode
|
2009-11-20 00:55:53 +00:00
|
|
|
(list 'calc-number-radix 'calc-twos-complement-mode)
|
2010-04-17 23:13:09 -05:00
|
|
|
(list n (or arg (calc-is-option))) t)
|
2001-11-06 18:59:06 +00:00
|
|
|
;; also change global value so minibuffer sees it
|
|
|
|
(setq-default calc-number-radix calc-number-radix))
|
|
|
|
(setq n calc-number-radix))
|
2009-11-20 00:55:53 +00:00
|
|
|
(if calc-twos-complement-mode
|
|
|
|
(message "Number radix is %d, two's complement mode is on." n)
|
2009-11-16 00:00:22 +00:00
|
|
|
(message "Number radix is %d" n))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-decimal-radix ()
|
|
|
|
(interactive)
|
2001-11-14 09:01:51 +00:00
|
|
|
(calc-radix 10))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2009-11-16 00:00:22 +00:00
|
|
|
(defun calc-binary-radix (&optional arg)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-radix 2 arg))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2009-11-20 00:55:53 +00:00
|
|
|
(defun calc-octal-radix (&optional arg)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-radix 8 arg))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2009-11-20 00:55:53 +00:00
|
|
|
(defun calc-hex-radix (&optional arg)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-radix 16 arg))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calc-leading-zeros (n)
|
|
|
|
(interactive "P")
|
|
|
|
(calc-wrapper
|
|
|
|
(if (calc-change-mode 'calc-leading-zeros n t t)
|
2001-11-19 07:44:56 +00:00
|
|
|
(message "Zero-padding integers to %d digits (assuming radix %d)"
|
2001-11-06 18:59:06 +00:00
|
|
|
(let* ((calc-internal-prec 6))
|
|
|
|
(math-compute-max-digits (math-abs calc-word-size)
|
|
|
|
calc-number-radix))
|
|
|
|
calc-number-radix)
|
2001-11-19 07:44:56 +00:00
|
|
|
(message "Omitting leading zeros on integers"))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
(defun math-power-of-2 (n) ; [I I] [Public]
|
2020-12-07 10:15:59 +01:00
|
|
|
(if (natnump n)
|
|
|
|
(ash 1 n)
|
2021-09-14 08:43:18 +02:00
|
|
|
(error "Argument must be a natural number")))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-integer-log2 (n) ; [I I] [Public]
|
2020-12-07 10:15:59 +01:00
|
|
|
(and (natnump n)
|
|
|
|
(not (zerop n))
|
|
|
|
(zerop (logand n (1- n)))
|
|
|
|
(logb n)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
;;; Bitwise operations.
|
|
|
|
|
|
|
|
(defun calcFunc-and (a b &optional w) ; [I I I] [Public]
|
|
|
|
(cond ((Math-messy-integerp w)
|
|
|
|
(calcFunc-and a b (math-trunc w)))
|
|
|
|
((and w (not (integerp w)))
|
|
|
|
(math-reject-arg w 'fixnump))
|
|
|
|
((and (integerp a) (integerp b))
|
|
|
|
(math-clip (logand a b) w))
|
|
|
|
((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
|
|
|
|
(math-binary-modulo-args 'calcFunc-and a b w))
|
|
|
|
((not (Math-num-integerp a))
|
|
|
|
(math-reject-arg a 'integerp))
|
|
|
|
((not (Math-num-integerp b))
|
|
|
|
(math-reject-arg b 'integerp))
|
* 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
|
|
|
(t (math-clip (logand (math-binary-arg a w) (math-binary-arg b w)) w))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-binary-arg (a w)
|
|
|
|
(if (not (Math-integerp a))
|
|
|
|
(setq a (math-trunc a)))
|
Calc: allow infinite binary word size (bug#43764)
Setting the word size ("b w") to 0 removes the word size clipping for
all bit operations (effectively as if a word size of -∞ had been set).
Rotation is disallowed; logical and arithmetic shifts behave
identically.
After a suggestion by Vincent Belaïche.
* lisp/calc/calc-bin.el (calc-word-size, math-binary-arg)
(math-binary-modulo-args, calcFunc-lsh, calcFunc-ash, calcFunc-rot)
(math-clip, math-format-twos-complement): Allow a word size of 0,
meaning -∞.
* test/lisp/calc/calc-tests.el
(calc-tests--not, calc-tests--and, calc-tests--or, calc-tests--xor)
(calc-tests--diff): New functions.
(calc-tests--clip, calc-tests--rot, calc-shift-binary): Extend to
cover word size 0.
(calc-bit-ops): New test.
* doc/misc/calc.texi (Binary Functions): Update manual.
* etc/NEWS: Announce the change.
2020-10-10 18:02:49 +02:00
|
|
|
(let ((w (if w (math-trunc w) calc-word-size)))
|
|
|
|
(if (and (< a 0) (not (zerop w)))
|
|
|
|
(logand a (1- (ash 1 w)))
|
|
|
|
a)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-binary-modulo-args (f a b w)
|
|
|
|
(let (mod)
|
|
|
|
(if (eq (car-safe a) 'mod)
|
|
|
|
(progn
|
|
|
|
(setq mod (nth 2 a)
|
|
|
|
a (nth 1 a))
|
|
|
|
(if (eq (car-safe b) 'mod)
|
|
|
|
(if (equal mod (nth 2 b))
|
|
|
|
(setq b (nth 1 b))
|
2011-11-19 18:29:42 -08:00
|
|
|
(math-reject-arg b "*Inconsistent modulus"))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(setq mod (nth 2 b)
|
|
|
|
b (nth 1 b)))
|
|
|
|
(if (Math-messy-integerp mod)
|
|
|
|
(setq mod (math-trunc mod))
|
|
|
|
(or (Math-integerp mod)
|
|
|
|
(math-reject-arg mod 'integerp)))
|
|
|
|
(let ((bits (math-integer-log2 mod)))
|
|
|
|
(if bits
|
|
|
|
(if w
|
Calc: allow infinite binary word size (bug#43764)
Setting the word size ("b w") to 0 removes the word size clipping for
all bit operations (effectively as if a word size of -∞ had been set).
Rotation is disallowed; logical and arithmetic shifts behave
identically.
After a suggestion by Vincent Belaïche.
* lisp/calc/calc-bin.el (calc-word-size, math-binary-arg)
(math-binary-modulo-args, calcFunc-lsh, calcFunc-ash, calcFunc-rot)
(math-clip, math-format-twos-complement): Allow a word size of 0,
meaning -∞.
* test/lisp/calc/calc-tests.el
(calc-tests--not, calc-tests--and, calc-tests--or, calc-tests--xor)
(calc-tests--diff): New functions.
(calc-tests--clip, calc-tests--rot, calc-shift-binary): Extend to
cover word size 0.
(calc-bit-ops): New test.
* doc/misc/calc.texi (Binary Functions): Update manual.
* etc/NEWS: Announce the change.
2020-10-10 18:02:49 +02:00
|
|
|
(if (and (/= w bits) (not (zerop w)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(calc-record-why
|
2011-11-19 18:29:42 -08:00
|
|
|
"*Warning: Modulus inconsistent with word size"))
|
2001-11-06 18:59:06 +00:00
|
|
|
(setq w bits))
|
2011-11-19 18:29:42 -08:00
|
|
|
(calc-record-why "*Warning: Modulus is not a power of 2"))
|
2001-11-06 18:59:06 +00:00
|
|
|
(math-make-mod (if b
|
|
|
|
(funcall f a b w)
|
|
|
|
(funcall f a w))
|
2001-11-14 09:01:51 +00:00
|
|
|
mod))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-or (a b &optional w) ; [I I I] [Public]
|
|
|
|
(cond ((Math-messy-integerp w)
|
|
|
|
(calcFunc-or a b (math-trunc w)))
|
|
|
|
((and w (not (integerp w)))
|
|
|
|
(math-reject-arg w 'fixnump))
|
|
|
|
((and (integerp a) (integerp b))
|
|
|
|
(math-clip (logior a b) w))
|
|
|
|
((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
|
|
|
|
(math-binary-modulo-args 'calcFunc-or a b w))
|
|
|
|
((not (Math-num-integerp a))
|
|
|
|
(math-reject-arg a 'integerp))
|
|
|
|
((not (Math-num-integerp b))
|
|
|
|
(math-reject-arg b 'integerp))
|
* 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
|
|
|
(t (math-clip (logior (math-binary-arg a w) (math-binary-arg b w)) w))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-xor (a b &optional w) ; [I I I] [Public]
|
|
|
|
(cond ((Math-messy-integerp w)
|
|
|
|
(calcFunc-xor a b (math-trunc w)))
|
|
|
|
((and w (not (integerp w)))
|
|
|
|
(math-reject-arg w 'fixnump))
|
|
|
|
((and (integerp a) (integerp b))
|
|
|
|
(math-clip (logxor a b) w))
|
|
|
|
((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
|
|
|
|
(math-binary-modulo-args 'calcFunc-xor a b w))
|
|
|
|
((not (Math-num-integerp a))
|
|
|
|
(math-reject-arg a 'integerp))
|
|
|
|
((not (Math-num-integerp b))
|
|
|
|
(math-reject-arg b 'integerp))
|
* 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
|
|
|
(t (math-clip (logxor (math-binary-arg a w) (math-binary-arg b w)) w))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-diff (a b &optional w) ; [I I I] [Public]
|
|
|
|
(cond ((Math-messy-integerp w)
|
|
|
|
(calcFunc-diff a b (math-trunc w)))
|
|
|
|
((and w (not (integerp w)))
|
|
|
|
(math-reject-arg w 'fixnump))
|
|
|
|
((and (integerp a) (integerp b))
|
|
|
|
(math-clip (logand a (lognot b)) w))
|
|
|
|
((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
|
|
|
|
(math-binary-modulo-args 'calcFunc-diff a b w))
|
|
|
|
((not (Math-num-integerp a))
|
|
|
|
(math-reject-arg a 'integerp))
|
|
|
|
((not (Math-num-integerp b))
|
|
|
|
(math-reject-arg b 'integerp))
|
* 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
|
|
|
(t (math-clip (logand (math-binary-arg a w)
|
|
|
|
(lognot (math-binary-arg b w)))
|
|
|
|
w))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-not (a &optional w) ; [I I] [Public]
|
|
|
|
(cond ((Math-messy-integerp w)
|
|
|
|
(calcFunc-not a (math-trunc w)))
|
|
|
|
((eq (car-safe a) 'mod)
|
|
|
|
(math-binary-modulo-args 'calcFunc-not a nil w))
|
|
|
|
((and w (not (integerp w)))
|
|
|
|
(math-reject-arg w 'fixnump))
|
|
|
|
((not (Math-num-integerp a))
|
|
|
|
(math-reject-arg a 'integerp))
|
|
|
|
((< (or w (setq w calc-word-size)) 0)
|
|
|
|
(math-clip (calcFunc-not a (- w)) w))
|
* 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
|
|
|
(t (math-clip (lognot (math-binary-arg a w)) w))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-lsh (a &optional n w) ; [I I] [Public]
|
|
|
|
(setq a (math-trunc a)
|
|
|
|
n (if n (math-trunc n) 1))
|
|
|
|
(if (eq (car-safe a) 'mod)
|
|
|
|
(math-binary-modulo-args 'calcFunc-lsh a n w)
|
|
|
|
(setq w (if w (math-trunc w) calc-word-size))
|
|
|
|
(or (integerp w)
|
|
|
|
(math-reject-arg w 'fixnump))
|
|
|
|
(or (Math-integerp a)
|
|
|
|
(math-reject-arg a 'integerp))
|
|
|
|
(or (Math-integerp n)
|
|
|
|
(math-reject-arg n 'integerp))
|
|
|
|
(if (< w 0)
|
|
|
|
(math-clip (calcFunc-lsh a n (- w)) w)
|
|
|
|
(if (Math-integer-negp a)
|
|
|
|
(setq a (math-clip a w)))
|
Calc: allow infinite binary word size (bug#43764)
Setting the word size ("b w") to 0 removes the word size clipping for
all bit operations (effectively as if a word size of -∞ had been set).
Rotation is disallowed; logical and arithmetic shifts behave
identically.
After a suggestion by Vincent Belaïche.
* lisp/calc/calc-bin.el (calc-word-size, math-binary-arg)
(math-binary-modulo-args, calcFunc-lsh, calcFunc-ash, calcFunc-rot)
(math-clip, math-format-twos-complement): Allow a word size of 0,
meaning -∞.
* test/lisp/calc/calc-tests.el
(calc-tests--not, calc-tests--and, calc-tests--or, calc-tests--xor)
(calc-tests--diff): New functions.
(calc-tests--clip, calc-tests--rot, calc-shift-binary): Extend to
cover word size 0.
(calc-bit-ops): New test.
* doc/misc/calc.texi (Binary Functions): Update manual.
* etc/NEWS: Announce the change.
2020-10-10 18:02:49 +02:00
|
|
|
(cond ((and (or (Math-lessp n (- w))
|
|
|
|
(Math-lessp w n))
|
|
|
|
(not (zerop w)))
|
2001-11-06 18:59:06 +00:00
|
|
|
0)
|
|
|
|
((< n 0)
|
Calc: allow infinite binary word size (bug#43764)
Setting the word size ("b w") to 0 removes the word size clipping for
all bit operations (effectively as if a word size of -∞ had been set).
Rotation is disallowed; logical and arithmetic shifts behave
identically.
After a suggestion by Vincent Belaïche.
* lisp/calc/calc-bin.el (calc-word-size, math-binary-arg)
(math-binary-modulo-args, calcFunc-lsh, calcFunc-ash, calcFunc-rot)
(math-clip, math-format-twos-complement): Allow a word size of 0,
meaning -∞.
* test/lisp/calc/calc-tests.el
(calc-tests--not, calc-tests--and, calc-tests--or, calc-tests--xor)
(calc-tests--diff): New functions.
(calc-tests--clip, calc-tests--rot, calc-shift-binary): Extend to
cover word size 0.
(calc-bit-ops): New test.
* doc/misc/calc.texi (Binary Functions): Update manual.
* etc/NEWS: Announce the change.
2020-10-10 18:02:49 +02:00
|
|
|
(ash (math-clip a w) n))
|
2001-11-06 18:59:06 +00:00
|
|
|
(t
|
2001-11-14 09:01:51 +00:00
|
|
|
(math-clip (math-mul a (math-power-of-2 n)) w))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-rsh (a &optional n w) ; [I I] [Public]
|
2001-11-14 09:01:51 +00:00
|
|
|
(calcFunc-lsh a (math-neg (or n 1)) w))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-ash (a &optional n w) ; [I I] [Public]
|
|
|
|
(if (or (null n)
|
|
|
|
(not (Math-negp n)))
|
|
|
|
(calcFunc-lsh a n w)
|
|
|
|
(setq a (math-trunc a)
|
|
|
|
n (if n (math-trunc n) 1))
|
|
|
|
(if (eq (car-safe a) 'mod)
|
|
|
|
(math-binary-modulo-args 'calcFunc-ash a n w)
|
|
|
|
(setq w (if w (math-trunc w) calc-word-size))
|
|
|
|
(or (integerp w)
|
|
|
|
(math-reject-arg w 'fixnump))
|
|
|
|
(or (Math-integerp a)
|
|
|
|
(math-reject-arg a 'integerp))
|
|
|
|
(or (Math-integerp n)
|
|
|
|
(math-reject-arg n 'integerp))
|
|
|
|
(if (< w 0)
|
|
|
|
(math-clip (calcFunc-ash a n (- w)) w)
|
|
|
|
(if (Math-integer-negp a)
|
|
|
|
(setq a (math-clip a w)))
|
2020-12-07 10:15:59 +01:00
|
|
|
(let ((two-to-sizem1 (and (not (zerop w)) (math-power-of-2 (1- w))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(sh (calcFunc-lsh a n w)))
|
Calc: allow infinite binary word size (bug#43764)
Setting the word size ("b w") to 0 removes the word size clipping for
all bit operations (effectively as if a word size of -∞ had been set).
Rotation is disallowed; logical and arithmetic shifts behave
identically.
After a suggestion by Vincent Belaïche.
* lisp/calc/calc-bin.el (calc-word-size, math-binary-arg)
(math-binary-modulo-args, calcFunc-lsh, calcFunc-ash, calcFunc-rot)
(math-clip, math-format-twos-complement): Allow a word size of 0,
meaning -∞.
* test/lisp/calc/calc-tests.el
(calc-tests--not, calc-tests--and, calc-tests--or, calc-tests--xor)
(calc-tests--diff): New functions.
(calc-tests--clip, calc-tests--rot, calc-shift-binary): Extend to
cover word size 0.
(calc-bit-ops): New test.
* doc/misc/calc.texi (Binary Functions): Update manual.
* etc/NEWS: Announce the change.
2020-10-10 18:02:49 +02:00
|
|
|
(cond ((or (zerop w)
|
|
|
|
(zerop (logand a two-to-sizem1)))
|
2001-11-06 18:59:06 +00:00
|
|
|
sh)
|
|
|
|
((Math-lessp n (- 1 w))
|
|
|
|
(math-add (math-mul two-to-sizem1 2) -1))
|
|
|
|
(t (let ((two-to-n (math-power-of-2 (- n))))
|
|
|
|
(math-add (calcFunc-lsh (math-add two-to-n -1)
|
|
|
|
(+ w n) w)
|
2001-11-14 09:01:51 +00:00
|
|
|
sh)))))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-rash (a &optional n w) ; [I I] [Public]
|
2001-11-14 09:01:51 +00:00
|
|
|
(calcFunc-ash a (math-neg (or n 1)) w))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun calcFunc-rot (a &optional n w) ; [I I] [Public]
|
|
|
|
(setq a (math-trunc a)
|
|
|
|
n (if n (math-trunc n) 1))
|
|
|
|
(if (eq (car-safe a) 'mod)
|
|
|
|
(math-binary-modulo-args 'calcFunc-rot a n w)
|
|
|
|
(setq w (if w (math-trunc w) calc-word-size))
|
Calc: allow infinite binary word size (bug#43764)
Setting the word size ("b w") to 0 removes the word size clipping for
all bit operations (effectively as if a word size of -∞ had been set).
Rotation is disallowed; logical and arithmetic shifts behave
identically.
After a suggestion by Vincent Belaïche.
* lisp/calc/calc-bin.el (calc-word-size, math-binary-arg)
(math-binary-modulo-args, calcFunc-lsh, calcFunc-ash, calcFunc-rot)
(math-clip, math-format-twos-complement): Allow a word size of 0,
meaning -∞.
* test/lisp/calc/calc-tests.el
(calc-tests--not, calc-tests--and, calc-tests--or, calc-tests--xor)
(calc-tests--diff): New functions.
(calc-tests--clip, calc-tests--rot, calc-shift-binary): Extend to
cover word size 0.
(calc-bit-ops): New test.
* doc/misc/calc.texi (Binary Functions): Update manual.
* etc/NEWS: Announce the change.
2020-10-10 18:02:49 +02:00
|
|
|
(when (zerop w)
|
|
|
|
(error "Rotation requires a nonzero word size"))
|
2001-11-06 18:59:06 +00:00
|
|
|
(or (integerp w)
|
|
|
|
(math-reject-arg w 'fixnump))
|
|
|
|
(or (Math-integerp a)
|
|
|
|
(math-reject-arg a 'integerp))
|
|
|
|
(or (Math-integerp n)
|
|
|
|
(math-reject-arg n 'integerp))
|
|
|
|
(if (< w 0)
|
|
|
|
(math-clip (calcFunc-rot a n (- w)) w)
|
|
|
|
(if (Math-integer-negp a)
|
|
|
|
(setq a (math-clip a w)))
|
|
|
|
(cond ((or (Math-integer-negp n)
|
2020-12-06 23:35:09 +01:00
|
|
|
(>= n w))
|
2001-11-06 18:59:06 +00:00
|
|
|
(calcFunc-rot a (math-mod n w) w))
|
|
|
|
(t
|
|
|
|
(math-add (calcFunc-lsh a (- n w) w)
|
2001-11-14 09:01:51 +00:00
|
|
|
(calcFunc-lsh a n w)))))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-clip (a &optional w) ; [I I] [Public]
|
|
|
|
(cond ((Math-messy-integerp w)
|
|
|
|
(math-clip a (math-trunc w)))
|
|
|
|
((eq (car-safe a) 'mod)
|
|
|
|
(math-binary-modulo-args 'math-clip a nil w))
|
|
|
|
((and w (not (integerp w)))
|
|
|
|
(math-reject-arg w 'fixnump))
|
|
|
|
((not (Math-num-integerp a))
|
|
|
|
(math-reject-arg a 'integerp))
|
|
|
|
((< (or w (setq w calc-word-size)) 0)
|
|
|
|
(setq a (math-clip a (- w)))
|
2020-12-06 23:35:09 +01:00
|
|
|
(if (< a (math-power-of-2 (- -1 w)))
|
2001-11-06 18:59:06 +00:00
|
|
|
a
|
|
|
|
(math-sub a (math-power-of-2 (- w)))))
|
Calc: allow infinite binary word size (bug#43764)
Setting the word size ("b w") to 0 removes the word size clipping for
all bit operations (effectively as if a word size of -∞ had been set).
Rotation is disallowed; logical and arithmetic shifts behave
identically.
After a suggestion by Vincent Belaïche.
* lisp/calc/calc-bin.el (calc-word-size, math-binary-arg)
(math-binary-modulo-args, calcFunc-lsh, calcFunc-ash, calcFunc-rot)
(math-clip, math-format-twos-complement): Allow a word size of 0,
meaning -∞.
* test/lisp/calc/calc-tests.el
(calc-tests--not, calc-tests--and, calc-tests--or, calc-tests--xor)
(calc-tests--diff): New functions.
(calc-tests--clip, calc-tests--rot, calc-shift-binary): Extend to
cover word size 0.
(calc-bit-ops): New test.
* doc/misc/calc.texi (Binary Functions): Update manual.
* etc/NEWS: Announce the change.
2020-10-10 18:02:49 +02:00
|
|
|
((math-zerop w)
|
|
|
|
a)
|
2001-11-06 18:59:06 +00:00
|
|
|
((Math-negp a)
|
* 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
|
|
|
(math-binary-arg a w))
|
|
|
|
((integerp a)
|
|
|
|
(logand a (1- (ash 1 w))))))
|
2001-11-14 09:01:51 +00:00
|
|
|
|
|
|
|
(defalias 'calcFunc-clip 'math-clip)
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defvar math-max-digits-cache nil)
|
|
|
|
(defun math-compute-max-digits (w r)
|
|
|
|
(let* ((pair (+ (* r 100000) w))
|
|
|
|
(res (assq pair math-max-digits-cache)))
|
|
|
|
(if res
|
|
|
|
(cdr res)
|
|
|
|
(let* ((calc-command-flags nil)
|
|
|
|
(digs (math-ceiling (math-div w (math-real-log2 r)))))
|
|
|
|
(setq math-max-digits-cache (cons (cons pair digs)
|
|
|
|
math-max-digits-cache))
|
2001-11-14 09:01:51 +00:00
|
|
|
digs))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defvar math-log2-cache (list '(2 . 1)
|
|
|
|
'(4 . 2)
|
|
|
|
'(8 . 3)
|
|
|
|
'(10 . (float 332193 -5))
|
|
|
|
'(16 . 4)
|
|
|
|
'(32 . 5)))
|
|
|
|
(defun math-real-log2 (x) ;;; calc-internal-prec must be 6
|
|
|
|
(let ((res (assq x math-log2-cache)))
|
|
|
|
(if res
|
|
|
|
(cdr res)
|
|
|
|
(let* ((calc-symbolic-mode nil)
|
|
|
|
(calc-display-working-message nil)
|
|
|
|
(log (calcFunc-log x 2)))
|
|
|
|
(setq math-log2-cache (cons (cons x log) math-log2-cache))
|
2001-11-14 09:01:51 +00:00
|
|
|
log))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defconst math-radix-digits ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
|
|
|
|
"A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
|
|
|
|
"K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
|
|
|
|
"U" "V" "W" "X" "Y" "Z"])
|
|
|
|
|
2001-11-19 07:44:56 +00:00
|
|
|
(defsubst math-format-radix-digit (a) ; [X D]
|
|
|
|
(aref math-radix-digits a))
|
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun math-format-radix (a) ; [X S]
|
|
|
|
(if (< a calc-number-radix)
|
|
|
|
(if (< a 0)
|
|
|
|
(concat "-" (math-format-radix (- a)))
|
|
|
|
(math-format-radix-digit a))
|
|
|
|
(let ((s ""))
|
|
|
|
(while (> a 0)
|
|
|
|
(setq s (concat (math-format-radix-digit (% a calc-number-radix)) s)
|
|
|
|
a (/ a calc-number-radix)))
|
2001-11-14 09:01:51 +00:00
|
|
|
s)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-format-binary (a) ; [X S]
|
2019-07-16 17:18:32 +02:00
|
|
|
(let ((calc-number-radix 2))
|
|
|
|
(math-format-radix a)))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
;;; Decompose into integer and fractional parts, without depending
|
|
|
|
;;; on calc-internal-prec.
|
|
|
|
(defun math-float-parts (a need-frac) ; returns ( int frac fracdigs )
|
|
|
|
(if (>= (nth 2 a) 0)
|
|
|
|
(list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0)
|
|
|
|
(let* ((d (math-numdigs (nth 1 a)))
|
|
|
|
(n (- (nth 2 a))))
|
|
|
|
(if need-frac
|
|
|
|
(if (>= n d)
|
|
|
|
(list 0 a n)
|
|
|
|
(let ((qr (math-idivmod (nth 1 a) (math-scale-int 1 n))))
|
|
|
|
(list (car qr) (math-make-float (cdr qr) (- n)) n)))
|
|
|
|
(list (math-scale-rounding (nth 1 a) (nth 2 a))
|
2001-11-14 09:01:51 +00:00
|
|
|
'(float 0 0) 0)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
* 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
|
|
|
(defun math-format-radix-float (a _prec)
|
2001-11-06 18:59:06 +00:00
|
|
|
(let ((fmt (car calc-float-format))
|
|
|
|
(figs (nth 1 calc-float-format))
|
|
|
|
(point calc-point-char)
|
2004-11-26 22:39:01 +00:00
|
|
|
(str nil)
|
|
|
|
pos)
|
2001-11-06 18:59:06 +00:00
|
|
|
(if (eq fmt 'fix)
|
|
|
|
(let* ((afigs (math-abs figs))
|
|
|
|
(fp (math-float-parts a (> afigs 0)))
|
|
|
|
(calc-internal-prec (+ 3 (max (nth 2 fp)
|
|
|
|
(math-convert-radix-digits
|
|
|
|
afigs t))))
|
|
|
|
(int (car fp))
|
|
|
|
(frac (math-round (math-mul (math-normalize (nth 1 fp))
|
|
|
|
(math-radix-float-power afigs)))))
|
|
|
|
(if (not (and (math-zerop frac) (math-zerop int) (< figs 0)))
|
|
|
|
(let ((math-radix-explicit-format nil))
|
|
|
|
(let ((calc-group-digits nil))
|
|
|
|
(setq str (if (> afigs 0) (math-format-number frac) ""))
|
|
|
|
(if (< (length str) afigs)
|
|
|
|
(setq str (concat (make-string (- afigs (length str)) ?0)
|
|
|
|
str))
|
|
|
|
(if (> (length str) afigs)
|
|
|
|
(setq str (substring str 1)
|
|
|
|
int (math-add int 1))))
|
|
|
|
(setq str (concat (math-format-number int) point str)))
|
2002-04-14 01:51:15 +00:00
|
|
|
(when calc-group-digits
|
|
|
|
(setq str (math-group-float str))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(setq figs 0))))
|
|
|
|
(or str
|
|
|
|
(let* ((prec calc-internal-prec)
|
|
|
|
(afigs (if (> figs 0)
|
|
|
|
figs
|
|
|
|
(max 1 (+ figs
|
|
|
|
(1- (math-convert-radix-digits
|
|
|
|
(max prec
|
|
|
|
(math-numdigs (nth 1 a)))))))))
|
|
|
|
(calc-internal-prec (+ 3 (math-convert-radix-digits afigs t)))
|
|
|
|
(explo -1) (vlo (math-radix-float-power explo))
|
|
|
|
(exphi 1) (vhi (math-radix-float-power exphi))
|
|
|
|
expmid vmid eadj)
|
|
|
|
(setq a (math-normalize a))
|
|
|
|
(if (Math-zerop a)
|
|
|
|
(setq explo 0)
|
|
|
|
(if (math-lessp-float '(float 1 0) a)
|
|
|
|
(while (not (math-lessp-float a vhi))
|
|
|
|
(setq explo exphi vlo vhi
|
|
|
|
exphi (math-mul exphi 2)
|
|
|
|
vhi (math-radix-float-power exphi)))
|
|
|
|
(while (math-lessp-float a vlo)
|
|
|
|
(setq exphi explo vhi vlo
|
|
|
|
explo (math-mul explo 2)
|
|
|
|
vlo (math-radix-float-power explo))))
|
|
|
|
(while (not (eq (math-sub exphi explo) 1))
|
|
|
|
(setq expmid (math-div2 (math-add explo exphi))
|
|
|
|
vmid (math-radix-float-power expmid))
|
|
|
|
(if (math-lessp-float a vmid)
|
|
|
|
(setq exphi expmid vhi vmid)
|
|
|
|
(setq explo expmid vlo vmid)))
|
|
|
|
(setq a (math-div-float a vlo)))
|
|
|
|
(let* ((sc (math-round (math-mul a (math-radix-float-power
|
|
|
|
(1- afigs)))))
|
|
|
|
(math-radix-explicit-format nil))
|
|
|
|
(let ((calc-group-digits nil))
|
|
|
|
(setq str (math-format-number sc))))
|
|
|
|
(if (> (length str) afigs)
|
|
|
|
(setq str (substring str 0 -1)
|
|
|
|
explo (1+ explo)))
|
|
|
|
(if (and (eq fmt 'float)
|
|
|
|
(math-lessp explo (+ (if (= figs 0)
|
|
|
|
(1- (math-convert-radix-digits
|
|
|
|
prec))
|
|
|
|
afigs)
|
|
|
|
calc-display-sci-high 1))
|
|
|
|
(math-lessp calc-display-sci-low explo))
|
|
|
|
(let ((dpos (1+ explo)))
|
|
|
|
(cond ((<= dpos 0)
|
|
|
|
(setq str (concat "0" point (make-string (- dpos) ?0)
|
|
|
|
str)))
|
|
|
|
((> dpos (length str))
|
|
|
|
(setq str (concat str (make-string (- dpos (length str))
|
|
|
|
?0) point)))
|
|
|
|
(t
|
|
|
|
(setq str (concat (substring str 0 dpos) point
|
|
|
|
(substring str dpos)))))
|
|
|
|
(setq explo nil))
|
|
|
|
(setq eadj (if (eq fmt 'eng)
|
|
|
|
(min (math-mod explo 3) (length str))
|
|
|
|
0)
|
|
|
|
str (concat (substring str 0 (1+ eadj)) point
|
|
|
|
(substring str (1+ eadj)))))
|
|
|
|
(setq pos (length str))
|
|
|
|
(while (eq (aref str (1- pos)) ?0) (setq pos (1- pos)))
|
|
|
|
(and explo (eq (aref str (1- pos)) ?.) (setq pos (1- pos)))
|
|
|
|
(setq str (substring str 0 pos))
|
2002-04-14 01:51:15 +00:00
|
|
|
(when calc-group-digits
|
|
|
|
(setq str (math-group-float str)))
|
2001-11-06 18:59:06 +00:00
|
|
|
(if explo
|
|
|
|
(let ((estr (let ((calc-number-radix 10)
|
|
|
|
(calc-group-digits nil))
|
2004-11-26 22:39:01 +00:00
|
|
|
(math-format-number
|
|
|
|
(math-sub explo eadj)))))
|
2001-11-06 18:59:06 +00:00
|
|
|
(setq str (if (or (memq calc-language '(math maple))
|
|
|
|
(> calc-number-radix 14))
|
|
|
|
(format "%s*%d.^%s" str calc-number-radix estr)
|
|
|
|
(format "%se%s" str estr)))))))
|
2001-11-14 09:01:51 +00:00
|
|
|
str))
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2001-11-19 07:44:56 +00:00
|
|
|
(defvar math-radix-digits-cache nil)
|
|
|
|
|
2001-11-06 18:59:06 +00:00
|
|
|
(defun math-convert-radix-digits (n &optional to-dec)
|
|
|
|
(let ((key (cons n (cons to-dec calc-number-radix))))
|
|
|
|
(or (cdr (assoc key math-radix-digits-cache))
|
|
|
|
(let* ((calc-internal-prec 6)
|
|
|
|
(log (math-div (math-real-log2 calc-number-radix)
|
|
|
|
'(float 332193 -5))))
|
|
|
|
(cdr (car (setq math-radix-digits-cache
|
|
|
|
(cons (cons key (math-ceiling (if to-dec
|
|
|
|
(math-mul n log)
|
|
|
|
(math-div n log))))
|
2001-11-14 09:01:51 +00:00
|
|
|
math-radix-digits-cache))))))))
|
|
|
|
|
2001-11-19 07:44:56 +00:00
|
|
|
(defvar math-radix-float-cache-tag nil)
|
2004-11-26 22:39:01 +00:00
|
|
|
(defvar math-radix-float-cache)
|
2001-11-06 18:59:06 +00:00
|
|
|
|
|
|
|
(defun math-radix-float-power (n)
|
|
|
|
(if (eq n 0)
|
|
|
|
'(float 1 0)
|
|
|
|
(or (and (eq calc-number-radix (car math-radix-float-cache-tag))
|
|
|
|
(<= calc-internal-prec (cdr math-radix-float-cache-tag)))
|
|
|
|
(setq math-radix-float-cache-tag (cons calc-number-radix
|
|
|
|
calc-internal-prec)
|
|
|
|
math-radix-float-cache nil))
|
|
|
|
(math-normalize
|
|
|
|
(or (cdr (assoc n math-radix-float-cache))
|
|
|
|
(cdr (car (setq math-radix-float-cache
|
|
|
|
(cons (cons
|
|
|
|
n
|
|
|
|
(let ((calc-internal-prec
|
|
|
|
(cdr math-radix-float-cache-tag)))
|
|
|
|
(if (math-negp n)
|
|
|
|
(math-div-float '(float 1 0)
|
|
|
|
(math-radix-float-power
|
|
|
|
(math-neg n)))
|
|
|
|
(math-mul-float (math-sqr-float
|
|
|
|
(math-radix-float-power
|
|
|
|
(math-div2 n)))
|
|
|
|
(if (math-evenp n)
|
|
|
|
'(float 1 0)
|
|
|
|
(math-float
|
|
|
|
calc-number-radix))))))
|
2001-11-14 09:01:51 +00:00
|
|
|
math-radix-float-cache))))))))
|
|
|
|
|
2009-11-20 00:55:53 +00:00
|
|
|
;;; Two's complement mode
|
2009-11-16 00:00:22 +00:00
|
|
|
|
2009-11-20 00:55:53 +00:00
|
|
|
(defun math-format-twos-complement (a)
|
2009-11-20 01:09:03 +00:00
|
|
|
"Format an integer in two's complement mode."
|
Calc: allow infinite binary word size (bug#43764)
Setting the word size ("b w") to 0 removes the word size clipping for
all bit operations (effectively as if a word size of -∞ had been set).
Rotation is disallowed; logical and arithmetic shifts behave
identically.
After a suggestion by Vincent Belaïche.
* lisp/calc/calc-bin.el (calc-word-size, math-binary-arg)
(math-binary-modulo-args, calcFunc-lsh, calcFunc-ash, calcFunc-rot)
(math-clip, math-format-twos-complement): Allow a word size of 0,
meaning -∞.
* test/lisp/calc/calc-tests.el
(calc-tests--not, calc-tests--and, calc-tests--or, calc-tests--xor)
(calc-tests--diff): New functions.
(calc-tests--clip, calc-tests--rot, calc-shift-binary): Extend to
cover word size 0.
(calc-bit-ops): New test.
* doc/misc/calc.texi (Binary Functions): Update manual.
* etc/NEWS: Announce the change.
2020-10-10 18:02:49 +02:00
|
|
|
(when (zerop calc-word-size)
|
|
|
|
(error "Nonzero word size required"))
|
2009-11-16 00:00:22 +00:00
|
|
|
(let* (;(calc-leading-zeros t)
|
|
|
|
(num
|
|
|
|
(cond
|
|
|
|
((or (eq a 0)
|
* 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
|
|
|
(Math-integer-posp a))
|
|
|
|
(math-format-radix a))
|
2009-11-16 00:00:22 +00:00
|
|
|
((Math-integer-negp a)
|
|
|
|
(let ((newa (math-add a math-2-word-size)))
|
* 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
|
|
|
(math-format-radix newa))))))
|
2009-11-16 00:00:22 +00:00
|
|
|
(let* ((calc-internal-prec 6)
|
|
|
|
(digs (math-compute-max-digits (math-abs calc-word-size)
|
|
|
|
calc-number-radix))
|
|
|
|
(len (length num)))
|
|
|
|
(if (< len digs)
|
|
|
|
(setq num (concat (make-string (- digs len) ?0) num))))
|
2010-05-14 18:46:11 -05:00
|
|
|
(when calc-group-digits
|
|
|
|
(setq num (math-group-float num)))
|
2017-09-13 15:52:52 -07:00
|
|
|
(concat
|
2009-11-16 00:00:22 +00:00
|
|
|
(number-to-string calc-number-radix)
|
|
|
|
"##"
|
|
|
|
num)))
|
|
|
|
|
2004-11-30 17:02:05 +00:00
|
|
|
(provide 'calc-bin)
|
2001-11-06 18:59:06 +00:00
|
|
|
|
2001-11-14 09:01:51 +00:00
|
|
|
;;; calc-bin.el ends here
|