Spelling fixes.
[bpt/emacs.git] / lisp / calc / calc-bin.el
index d70e686..44354f0 100644 (file)
@@ -1,7 +1,6 @@
 ;;; calc-bin.el --- binary functions for Calc
 
-;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 1990-1993, 2001-2011 Free Software Foundation, Inc.
 
 ;; Author: David Gillespie <daveg@synaptics.com>
 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
@@ -154,8 +153,12 @@ the size of a Calc bignum digit.")
         (calc-change-mode '(calc-word-size calc-previous-modulo)
                           (list n (math-power-of-2 (math-abs n)))
                           calc-leading-zeros)))
+   (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)
    (if (< n 0)
-       (message "Binary word size is %d bits (2's complement)" (- n))
+       (message "Binary word size is %d bits (two's complement)" (- n))
      (message "Binary word size is %d bits" n))))
 
 
@@ -164,32 +167,36 @@ the size of a Calc bignum digit.")
 
 ;;; d-prefix mode commands.
 
-(defun calc-radix (n)
+(defun calc-radix (n &optional arg)
   (interactive "NDisplay radix (2-36): ")
   (calc-wrapper
    (if (and (>= n 2) (<= n 36))
        (progn
-        (calc-change-mode 'calc-number-radix n t)
+        (calc-change-mode 
+          (list 'calc-number-radix 'calc-twos-complement-mode)
+          (list n (or arg (calc-is-option))) t)
         ;; also change global value so minibuffer sees it
         (setq-default calc-number-radix calc-number-radix))
      (setq n calc-number-radix))
-   (message "Number radix is %d" n)))
+   (if calc-twos-complement-mode
+       (message "Number radix is %d, two's complement mode is on." n)
+     (message "Number radix is %d" n))))
 
 (defun calc-decimal-radix ()
   (interactive)
   (calc-radix 10))
 
-(defun calc-binary-radix ()
-  (interactive)
-  (calc-radix 2))
+(defun calc-binary-radix (&optional arg)
+  (interactive "P")
+  (calc-radix 2 arg))
 
-(defun calc-octal-radix ()
-  (interactive)
-  (calc-radix 8))
+(defun calc-octal-radix (&optional arg)
+  (interactive "P")
+  (calc-radix 8 arg))
 
-(defun calc-hex-radix ()
-  (interactive)
-  (calc-radix 16))
+(defun calc-hex-radix (&optional arg)
+  (interactive "P")
+  (calc-radix 16 arg))
 
 (defun calc-leading-zeros (n)
   (interactive "P")
@@ -284,7 +291,7 @@ the size of a Calc bignum digit.")
          (if (eq (car-safe b) 'mod)
              (if (equal mod (nth 2 b))
                  (setq b (nth 1 b))
-               (math-reject-arg b "*Inconsistent modulos"))))
+               (math-reject-arg b "*Inconsistent modulus"))))
       (setq mod (nth 2 b)
            b (nth 1 b)))
     (if (Math-messy-integerp mod)
@@ -296,9 +303,9 @@ the size of a Calc bignum digit.")
          (if w
              (if (/= w bits)
                  (calc-record-why
-                  "*Warning: Modulo inconsistent with word size"))
+                  "*Warning: Modulus inconsistent with word size"))
            (setq w bits))
-       (calc-record-why "*Warning: Modulo is not a power of 2"))
+       (calc-record-why "*Warning: Modulus is not a power of 2"))
       (math-make-mod (if b
                         (funcall f a b w)
                       (funcall f a w))
@@ -812,7 +819,38 @@ the size of a Calc bignum digit.")
                                                       calc-number-radix))))))
                               math-radix-float-cache))))))))
 
+;;; Two's complement mode
+
+(defun math-format-twos-complement (a)
+  "Format an integer in two's complement mode."
+  (let* (;(calc-leading-zeros t)
+         (overflow nil)
+         (negative nil)
+         (num
+          (cond
+           ((or (eq a 0)
+                (and (Math-integer-posp a)))
+            (if (integerp a)
+                (math-format-radix a)
+              (math-format-bignum-radix (cdr a))))
+           ((Math-integer-negp a)
+            (let ((newa (math-add a math-2-word-size)))
+              (if (integerp newa)
+                  (math-format-radix newa)
+                (math-format-bignum-radix (cdr newa))))))))
+    (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))))
+    (when calc-group-digits
+      (setq num (math-group-float num)))
+    (concat 
+     (number-to-string calc-number-radix)
+     "##"
+     num)))
+
 (provide 'calc-bin)
 
-;; arch-tag: f6dba7bc-53b2-41ae-919c-c266ab0ca8b3
 ;;; calc-bin.el ends here