merge trunk
[bpt/emacs.git] / lisp / calc / calc-bin.el
index 0f21927..4ab698e 100644 (file)
@@ -1,17 +1,17 @@
 ;;; calc-bin.el --- binary functions for Calc
 
 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
-;;   2005, 2006, 2007 Free Software Foundation, Inc.
+;;   2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
 
 ;; Author: David Gillespie <daveg@synaptics.com>
 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; 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, or (at your option)
-;; any later version.
+;; 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
@@ -19,9 +19,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -156,8 +154,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))))
 
 
@@ -166,32 +168,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")
@@ -814,7 +820,39 @@ 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
+;; arch-tag: f6dba7bc-53b2-41ae-919c-c266ab0ca8b3
 ;;; calc-bin.el ends here