• Home
  • History
  • Annotate
  • Raw
  • Download
  • only in /macosx-10.10.1/emacs-93/emacs/lisp/calc/

Lines Matching +defs:calc +defs:math

0 ;;; calc-bin.el --- binary functions for Calc
30 ;; This file is autoloaded from calc-ext.el.
32 (require 'calc-ext)
33 (require 'calc-macs)
37 (defun calc-and (n)
39 (calc-slow-wrapper
40 (calc-enter-result 2 "and"
42 (calc-top-list-n 2)
45 (defun calc-or (n)
47 (calc-slow-wrapper
48 (calc-enter-result 2 "or"
50 (calc-top-list-n 2)
53 (defun calc-xor (n)
55 (calc-slow-wrapper
56 (calc-enter-result 2 "xor"
58 (calc-top-list-n 2)
61 (defun calc-diff (n)
63 (calc-slow-wrapper
64 (calc-enter-result 2 "diff"
66 (calc-top-list-n 2)
69 (defun calc-not (n)
71 (calc-slow-wrapper
72 (calc-enter-result 1 "not"
74 (calc-top-list-n 1)
77 (defun calc-lshift-binary (n)
79 (calc-slow-wrapper
80 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
81 (calc-enter-result hyp "lsh"
83 (calc-top-list-n hyp)
86 (defun calc-rshift-binary (n)
88 (calc-slow-wrapper
89 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
90 (calc-enter-result hyp "rsh"
92 (calc-top-list-n hyp)
95 (defun calc-lshift-arith (n)
97 (calc-slow-wrapper
98 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
99 (calc-enter-result hyp "ash"
101 (calc-top-list-n hyp)
104 (defun calc-rshift-arith (n)
106 (calc-slow-wrapper
107 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
108 (calc-enter-result hyp "rash"
110 (calc-top-list-n hyp)
113 (defun calc-rotate-binary (n)
115 (calc-slow-wrapper
116 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
117 (calc-enter-result hyp "rot"
119 (calc-top-list-n hyp)
122 (defun calc-clip (n)
124 (calc-slow-wrapper
125 (calc-enter-result 1 "clip"
127 (calc-top-list-n 1)
130 (defun calc-word-size (n)
132 (calc-wrapper
134 calc-word-size))))
137 calc-word-size
142 (or (= n calc-word-size)
143 (if (> (math-abs n) 100)
144 (calc-change-mode 'calc-word-size n calc-leading-zeros)
145 (calc-change-mode '(calc-word-size calc-previous-modulo)
146 (list n (math-power-of-2 (math-abs n)))
147 calc-leading-zeros)))
158 (defun calc-radix (n)
160 (calc-wrapper
163 (calc-change-mode 'calc-number-radix n t)
165 (setq-default calc-number-radix calc-number-radix))
166 (setq n calc-number-radix))
169 (defun calc-decimal-radix ()
171 (calc-radix 10))
173 (defun calc-binary-radix ()
175 (calc-radix 2))
177 (defun calc-octal-radix ()
179 (calc-radix 8))
181 (defun calc-hex-radix ()
183 (calc-radix 16))
185 (defun calc-leading-zeros (n)
187 (calc-wrapper
188 (if (calc-change-mode 'calc-leading-zeros n t t)
190 (let* ((calc-internal-prec 6))
191 (math-compute-max-digits (math-abs calc-word-size)
192 calc-number-radix))
193 calc-number-radix)
197 (defvar math-power-of-2-cache (list 1 2 4 8 16 32 64 128 256 512 1024))
198 (defvar math-big-power-of-2-cache nil)
199 (defun math-power-of-2 (n) ; [I I] [Public]
201 (or (nth n math-power-of-2-cache)
202 (let* ((i (length math-power-of-2-cache))
203 (val (nth (1- i) math-power-of-2-cache)))
205 (setq val (math-mul val 2)
206 math-power-of-2-cache (nconc math-power-of-2-cache
210 (let ((found (assq n math-big-power-of-2-cache)))
213 (let ((po2 (math-ipow 2 n)))
214 (setq math-big-power-of-2-cache
215 (cons (cons n po2) math-big-power-of-2-cache))
218 (defun math-integer-log2 (n) ; [I I] [Public]
220 (p math-power-of-2-cache)
230 (setq val (math-mul val 2))
231 (setq math-power-of-2-cache (nconc math-power-of-2-cache
245 (calcFunc-and a b (math-trunc w)))
247 (math-reject-arg w 'fixnump))
249 (math-clip (logand a b) w))
251 (math-binary-modulo-args 'calcFunc-and a b w))
253 (math-reject-arg a 'integerp))
255 (math-reject-arg b 'integerp))
256 (t (math-clip (cons 'bigpos
257 (math-and-bignum (math-binary-arg a w)
258 (math-binary-arg b w)))
261 (defun math-binary-arg (a w)
263 (setq a (math-trunc a)))
265 (math-not-bignum (cdr (math-bignum-test (math-sub -1 a)))
266 (math-abs (if w (math-trunc w) calc-word-size)))
269 (defun math-binary-modulo-args (f a b w)
278 (math-reject-arg b "*Inconsistent modulos"))))
282 (setq mod (math-trunc mod))
284 (math-reject-arg mod 'integerp)))
285 (let ((bits (math-integer-log2 mod)))
289 (calc-record-why
292 (calc-record-why "*Warning: Modulo is not a power of 2"))
293 (math-make-mod (if b
298 (defun math-and-bignum (a b) ; [l l l]
300 (let ((qa (math-div-bignum-digit a 512))
301 (qb (math-div-bignum-digit b 512)))
302 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa))
303 (math-norm-bignum (car qb)))
309 (calcFunc-or a b (math-trunc w)))
311 (math-reject-arg w 'fixnump))
313 (math-clip (logior a b) w))
315 (math-binary-modulo-args 'calcFunc-or a b w))
317 (math-reject-arg a 'integerp))
319 (math-reject-arg b 'integerp))
320 (t (math-clip (cons 'bigpos
321 (math-or-bignum (math-binary-arg a w)
322 (math-binary-arg b w)))
325 (defun math-or-bignum (a b) ; [l l l]
327 (let ((qa (math-div-bignum-digit a 512))
328 (qb (math-div-bignum-digit b 512)))
329 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa))
330 (math-norm-bignum (car qb)))
336 (calcFunc-xor a b (math-trunc w)))
338 (math-reject-arg w 'fixnump))
340 (math-clip (logxor a b) w))
342 (math-binary-modulo-args 'calcFunc-xor a b w))
344 (math-reject-arg a 'integerp))
346 (math-reject-arg b 'integerp))
347 (t (math-clip (cons 'bigpos
348 (math-xor-bignum (math-binary-arg a w)
349 (math-binary-arg b w)))
352 (defun math-xor-bignum (a b) ; [l l l]
354 (let ((qa (math-div-bignum-digit a 512))
355 (qb (math-div-bignum-digit b 512)))
356 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa))
357 (math-norm-bignum (car qb)))
363 (calcFunc-diff a b (math-trunc w)))
365 (math-reject-arg w 'fixnump))
367 (math-clip (logand a (lognot b)) w))
369 (math-binary-modulo-args 'calcFunc-diff a b w))
371 (math-reject-arg a 'integerp))
373 (math-reject-arg b 'integerp))
374 (t (math-clip (cons 'bigpos
375 (math-diff-bignum (math-binary-arg a w)
376 (math-binary-arg b w)))
379 (defun math-diff-bignum (a b) ; [l l l]
381 (let ((qa (math-div-bignum-digit a 512))
382 (qb (math-div-bignum-digit b 512)))
383 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa))
384 (math-norm-bignum (car qb)))
390 (calcFunc-not a (math-trunc w)))
392 (math-binary-modulo-args 'calcFunc-not a nil w))
394 (math-reject-arg w 'fixnump))
396 (math-reject-arg a 'integerp))
397 ((< (or w (setq w calc-word-size)) 0)
398 (math-clip (calcFunc-not a (- w)) w))
399 (t (math-normalize
401 (math-not-bignum (math-binary-arg a w)
404 (defun math-not-bignum (a w) ; [l l]
405 (let ((q (math-div-bignum-digit a 512)))
409 (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q))
415 (setq a (math-trunc a)
416 n (if n (math-trunc n) 1))
418 (math-binary-modulo-args 'calcFunc-lsh a n w)
419 (setq w (if w (math-trunc w) calc-word-size))
421 (math-reject-arg w 'fixnump))
423 (math-reject-arg a 'integerp))
425 (math-reject-arg n 'integerp))
427 (math-clip (calcFunc-lsh a n (- w)) w)
429 (setq a (math-clip a w)))
434 (math-quotient (math-clip a w) (math-power-of-2 (- n))))
436 (math-clip (math-mul a (math-power-of-2 n)) w))))))
439 (calcFunc-lsh a (math-neg (or n 1)) w))
445 (setq a (math-trunc a)
446 n (if n (math-trunc n) 1))
448 (math-binary-modulo-args 'calcFunc-ash a n w)
449 (setq w (if w (math-trunc w) calc-word-size))
451 (math-reject-arg w 'fixnump))
453 (math-reject-arg a 'integerp))
455 (math-reject-arg n 'integerp))
457 (math-clip (calcFunc-ash a n (- w)) w)
459 (setq a (math-clip a w)))
460 (let ((two-to-sizem1 (math-power-of-2 (1- w)))
465 (math-add (math-mul two-to-sizem1 2) -1))
466 (t (let ((two-to-n (math-power-of-2 (- n))))
467 (math-add (calcFunc-lsh (math-add two-to-n -1)
472 (calcFunc-ash a (math-neg (or n 1)) w))
475 (setq a (math-trunc a)
476 n (if n (math-trunc n) 1))
478 (math-binary-modulo-args 'calcFunc-rot a n w)
479 (setq w (if w (math-trunc w) calc-word-size))
481 (math-reject-arg w 'fixnump))
483 (math-reject-arg a 'integerp))
485 (math-reject-arg n 'integerp))
487 (math-clip (calcFunc-rot a n (- w)) w)
489 (setq a (math-clip a w)))
492 (calcFunc-rot a (math-mod n w) w))
494 (math-add (calcFunc-lsh a (- n w) w)
497 (defun math-clip (a &optional w) ; [I I] [Public]
499 (math-clip a (math-trunc w)))
501 (math-binary-modulo-args 'math-clip a nil w))
503 (math-reject-arg w 'fixnump))
505 (math-reject-arg a 'integerp))
506 ((< (or w (setq w calc-word-size)) 0)
507 (setq a (math-clip a (- w)))
508 (if (Math-natnum-lessp a (math-power-of-2 (- -1 w)))
510 (math-sub a (math-power-of-2 (- w)))))
512 (math-normalize (cons 'bigpos (math-binary-arg a w))))
518 (math-normalize
520 (math-clip-bignum (cdr (math-bignum-test (math-trunc a)))
523 (defalias 'calcFunc-clip 'math-clip)
525 (defun math-clip-bignum (a w) ; [l l]
526 (let ((q (math-div-bignum-digit a 512)))
530 (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q))
535 (defvar math-max-digits-cache nil)
536 (defun math-compute-max-digits (w r)
538 (res (assq pair math-max-digits-cache)))
541 (let* ((calc-command-flags nil)
542 (digs (math-ceiling (math-div w (math-real-log2 r)))))
543 (setq math-max-digits-cache (cons (cons pair digs)
544 math-max-digits-cache))
547 (defvar math-log2-cache (list '(2 . 1)
553 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6
554 (let ((res (assq x math-log2-cache)))
557 (let* ((calc-symbolic-mode nil)
558 (calc-display-working-message nil)
560 (setq math-log2-cache (cons (cons x log) math-log2-cache))
563 (defconst math-radix-digits ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
568 (defsubst math-format-radix-digit (a) ; [X D]
569 (aref math-radix-digits a))
571 (defun math-format-radix (a) ; [X S]
572 (if (< a calc-number-radix)
574 (concat "-" (math-format-radix (- a)))
575 (math-format-radix-digit a))
578 (setq s (concat (math-format-radix-digit (% a calc-number-radix)) s)
579 a (/ a calc-number-radix)))
582 (defconst math-binary-digits ["000" "001" "010" "011"
584 (defun math-format-binary (a) ; [X S]
587 (concat "-" (math-format-binary (- a)))
588 (math-format-radix a))
591 (setq s (concat (aref math-binary-digits (% a 8)) s)
593 (concat (math-format-radix a) s))))
595 (defun math-format-bignum-radix (a) ; [X L]
598 (< (car a) calc-number-radix))
599 (math-format-radix-digit (car a)))
601 (let ((q (math-div-bignum-digit a calc-number-radix)))
602 (concat (math-format-bignum-radix (math-norm-bignum (car q)))
603 (math-format-radix-digit (cdr q)))))))
605 (defun math-format-bignum-binary (a) ; [X L]
608 (math-format-binary (car a)))
610 (let ((q (math-div-bignum-digit a 512)))
611 (concat (math-format-bignum-binary (math-norm-bignum (car q)))
612 (aref math-binary-digits (/ (cdr q) 64))
613 (aref math-binary-digits (% (/ (cdr q) 8) 8))
614 (aref math-binary-digits (% (cdr q) 8)))))))
616 (defun math-format-bignum-octal (a) ; [X L]
619 (math-format-radix (car a)))
621 (let ((q (math-div-bignum-digit a 512)))
622 (concat (math-format-bignum-octal (math-norm-bignum (car q)))
623 (math-format-radix-digit (/ (cdr q) 64))
624 (math-format-radix-digit (% (/ (cdr q) 8) 8))
625 (math-format-radix-digit (% (cdr q) 8)))))))
627 (defun math-format-bignum-hex (a) ; [X L]
630 (math-format-radix (car a)))
632 (let ((q (math-div-bignum-digit a 256)))
633 (concat (math-format-bignum-hex (math-norm-bignum (car q)))
634 (math-format-radix-digit (/ (cdr q) 16))
635 (math-format-radix-digit (% (cdr q) 16)))))))
638 ;;; on calc-internal-prec.
639 (defun math-float-parts (a need-frac) ; returns ( int frac fracdigs )
641 (list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0)
642 (let* ((d (math-numdigs (nth 1 a)))
647 (let ((qr (math-idivmod (nth 1 a) (math-scale-int 1 n))))
648 (list (car qr) (math-make-float (cdr qr) (- n)) n)))
649 (list (math-scale-rounding (nth 1 a) (nth 2 a))
652 (defun math-format-radix-float (a prec)
653 (let ((fmt (car calc-float-format))
654 (figs (nth 1 calc-float-format))
655 (point calc-point-char)
659 (let* ((afigs (math-abs figs))
660 (fp (math-float-parts a (> afigs 0)))
661 (calc-internal-prec (+ 3 (max (nth 2 fp)
662 (math-convert-radix-digits
665 (frac (math-round (math-mul (math-normalize (nth 1 fp))
666 (math-radix-float-power afigs)))))
667 (if (not (and (math-zerop frac) (math-zerop int) (< figs 0)))
668 (let ((math-radix-explicit-format nil))
669 (let ((calc-group-digits nil))
670 (setq str (if (> afigs 0) (math-format-number frac) ""))
676 int (math-add int 1))))
677 (setq str (concat (math-format-number int) point str)))
678 (when calc-group-digits
679 (setq str (math-group-float str))))
682 (let* ((prec calc-internal-prec)
686 (1- (math-convert-radix-digits
688 (math-numdigs (nth 1 a)))))))))
689 (calc-internal-prec (+ 3 (math-convert-radix-digits afigs t)))
690 (explo -1) (vlo (math-radix-float-power explo))
691 (exphi 1) (vhi (math-radix-float-power exphi))
693 (setq a (math-normalize a))
696 (if (math-lessp-float '(float 1 0) a)
697 (while (not (math-lessp-float a vhi))
699 exphi (math-mul exphi 2)
700 vhi (math-radix-float-power exphi)))
701 (while (math-lessp-float a vlo)
703 explo (math-mul explo 2)
704 vlo (math-radix-float-power explo))))
705 (while (not (eq (math-sub exphi explo) 1))
706 (setq expmid (math-div2 (math-add explo exphi))
707 vmid (math-radix-float-power expmid))
708 (if (math-lessp-float a vmid)
711 (setq a (math-div-float a vlo)))
712 (let* ((sc (math-round (math-mul a (math-radix-float-power
714 (math-radix-explicit-format nil))
715 (let ((calc-group-digits nil))
716 (setq str (math-format-number sc))))
721 (math-lessp explo (+ (if (= figs 0)
722 (1- (math-convert-radix-digits
725 calc-display-sci-high 1))
726 (math-lessp calc-display-sci-low explo))
739 (min (math-mod explo 3) (length str))
747 (when calc-group-digits
748 (setq str (math-group-float str)))
750 (let ((estr (let ((calc-number-radix 10)
751 (calc-group-digits nil))
752 (math-format-number
753 (math-sub explo eadj)))))
754 (setq str (if (or (memq calc-language '(math maple))
755 (> calc-number-radix 14))
756 (format "%s*%d.^%s" str calc-number-radix estr)
760 (defvar math-radix-digits-cache nil)
762 (defun math-convert-radix-digits (n &optional to-dec)
763 (let ((key (cons n (cons to-dec calc-number-radix))))
764 (or (cdr (assoc key math-radix-digits-cache))
765 (let* ((calc-internal-prec 6)
766 (log (math-div (math-real-log2 calc-number-radix)
768 (cdr (car (setq math-radix-digits-cache
769 (cons (cons key (math-ceiling (if to-dec
770 (math-mul n log)
771 (math-div n log))))
772 math-radix-digits-cache))))))))
774 (defvar math-radix-float-cache-tag nil)
775 (defvar math-radix-float-cache)
777 (defun math-radix-float-power (n)
780 (or (and (eq calc-number-radix (car math-radix-float-cache-tag))
781 (<= calc-internal-prec (cdr math-radix-float-cache-tag)))
782 (setq math-radix-float-cache-tag (cons calc-number-radix
783 calc-internal-prec)
784 math-radix-float-cache nil))
785 (math-normalize
786 (or (cdr (assoc n math-radix-float-cache))
787 (cdr (car (setq math-radix-float-cache
790 (let ((calc-internal-prec
791 (cdr math-radix-float-cache-tag)))
792 (if (math-negp n)
793 (math-div-float '(float 1 0)
794 (math-radix-float-power
795 (math-neg n)))
796 (math-mul-float (math-sqr-float
797 (math-radix-float-power
798 (math-div2 n)))
799 (if (math-evenp n)
801 (math-float
802 calc-number-radix))))))
803 math-radix-float-cache))))))))
805 (provide 'calc-bin)
808 ;;; calc-bin.el ends here