changeset 106035:0cbe709e0f55

(math-2-word-size, math-half-2-word-size, calc-complement-signed-mode): new variables. (calc-set-mode-line): Add indicator for twos-complements. (math-format-number): Format twos-complement notation.
author Jay Belanger <jay.p.belanger@gmail.com>
date Sun, 15 Nov 2009 23:55:53 +0000
parents 4ff5c9cf147f
children 20558a815b94
files lisp/calc/calc.el
diffstat 1 files changed, 31 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/lisp/calc/calc.el	Sun Nov 15 23:54:21 2009 +0000
+++ b/lisp/calc/calc.el	Sun Nov 15 23:55:53 2009 +0000
@@ -153,6 +153,7 @@
 (declare-function calcFunc-unixtime "calc-forms" (date &optional zone))
 (declare-function math-parse-date "calc-forms" (math-pd-str))
 (declare-function math-lessp "calc-ext" (a b))
+(declare-function math-compare "calc-ext" (a b))
 (declare-function calc-embedded-finish-command "calc-embed" ())
 (declare-function calc-embedded-select-buffer "calc-embed" ())
 (declare-function calc-embedded-mode-line-change "calc-embed" ())
@@ -207,6 +208,7 @@
 (declare-function math-adjust-fraction "calc-ext" (a))
 (declare-function math-format-binary "calc-bin" (a))
 (declare-function math-format-radix "calc-bin" (a))
+(declare-function math-format-complement-signed "calc-bin" (a))
 (declare-function math-group-float "calc-ext" (str))
 (declare-function math-mod "calc-misc" (a b))
 (declare-function math-format-number-fancy "calc-ext" (a prec))
@@ -688,6 +690,10 @@
 If `scalar', variables are assumed to be scalar-valued.
 If nil, symbolic math routines make no assumptions about variables.")
 
+(defcalcmodevar calc-complement-signed-mode nil
+  "If non-nil, display integers in complement signed mode.")
+
+
 (defcalcmodevar calc-shift-prefix nil
   "If non-nil, shifted letter keys are prefix keys rather than normal meanings.")
 
@@ -1704,6 +1710,7 @@
 			   ((= calc-number-radix 8) "Oct ")
 			   ((= calc-number-radix 16) "Hex ")
 			   (t (format "Radix%d " calc-number-radix)))
+                     (if calc-complement-signed-mode "CompSign " "")
 		     (if calc-leading-zeros "Zero " "")
 		     (cond ((null calc-language) "")
                            ((get calc-language 'math-lang-name)
@@ -2350,7 +2357,7 @@
 	  (insert "mod "))))
      (t
       (insert (char-to-string last-command-event))
-      (if (or (and (calc-minibuffer-contains "[-+]?\\(.*\\+/- *\\|.*mod *\\)?\\([0-9][0-9]?\\)#[0-9a-zA-Z]*\\(:[0-9a-zA-Z]*\\(:[0-9a-zA-Z]*\\)?\\|.[0-9a-zA-Z]*\\(e[-+]?[0-9]*\\)?\\)?\\'")
+      (if (or (and (calc-minibuffer-contains "[-+]?\\(.*\\+/- *\\|.*mod *\\)?\\([0-9][0-9]?\\)#[#&]?[0-9a-zA-Z]*\\(:[0-9a-zA-Z]*\\(:[0-9a-zA-Z]*\\)?\\|.[0-9a-zA-Z]*\\(e[-+]?[0-9]*\\)?\\)?\\'")
 		   (let ((radix (string-to-number
 				 (buffer-substring
 				  (match-beginning 2) (match-end 2)))))
@@ -3382,9 +3389,24 @@
 
 
 ;;; Format a number as a string.
+(defvar math-half-2-word-size)
 (defun math-format-number (a &optional prec)   ; [X N]   [Public]
   (cond
    ((eq calc-display-raw t) (format "%s" a))
+   ((and calc-complement-signed-mode
+         math-radix-explicit-format
+         (Math-integerp a)
+         (or (eq a 0)
+             (and (Math-integer-posp a)
+                  (Math-lessp a math-half-2-word-size))
+             (and (Math-integer-negp a)
+                  (require 'calc-ext)
+                  (let ((comparison 
+                         (math-compare (Math-integer-neg a) math-half-2-word-size)))
+                    (or (= comparison 0)
+                        (= comparison -1))))))
+    (require 'calc-bin)
+    (math-format-complement-signed a))
    ((and (nth 1 calc-frac-format) (Math-integerp a))
     (require 'calc-ext)
     (math-format-number (math-adjust-fraction a)))
@@ -3767,6 +3789,14 @@
       (setq unread-command-event nil)
     (setq unread-command-events nil)))
 
+(defcalcmodevar math-2-word-size 
+  (math-read-number-simple "4294967296")
+  "Two to the power of `calc-word-size'.")
+
+(defcalcmodevar math-half-2-word-size
+  (math-read-number-simple "2147483648")
+  "One-half of two to the power of `calc-word-size'.")
+
 (when calc-always-load-extensions
   (require 'calc-ext)
   (calc-load-everything))