emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master a87840f: Fix calc number formatting with digit grou


From: Mattias Engdegård
Subject: [Emacs-diffs] master a87840f: Fix calc number formatting with digit grouping (bug#36689)
Date: Tue, 16 Jul 2019 11:37:56 -0400 (EDT)

branch: master
commit a87840fffbf471d53eba17ea683728125d2d4767
Author: Mattias Engdegård <address@hidden>
Commit: Mattias Engdegård <address@hidden>

    Fix calc number formatting with digit grouping (bug#36689)
    
    The functions math-format-hex and math-format-octal were not
    implemented, yet called, leading to a crash when using hex or octal
    radix with digit grouping.
    
    * test/lisp/calc/calc-tests.el (calc-test-format-radix): New test.
    * lisp/calc/calc-ext.el: Don't declare non-existing functions.
    (math--format-integer-fancy): Don't call non-existing functions.
    * lisp/calc/calc-bin.el (math-format-binary, math-binary-digits):
    Simplify, fixing 0-padding bug.
---
 lisp/calc/calc-bin.el        | 13 ++-----------
 lisp/calc/calc-ext.el        | 14 +++-----------
 test/lisp/calc/calc-tests.el | 26 ++++++++++++++++++++++++++
 3 files changed, 31 insertions(+), 22 deletions(-)

diff --git a/lisp/calc/calc-bin.el b/lisp/calc/calc-bin.el
index b4371bd..558e309 100644
--- a/lisp/calc/calc-bin.el
+++ b/lisp/calc/calc-bin.el
@@ -506,18 +506,9 @@
              a (/ a calc-number-radix)))
       s)))
 
-(defconst math-binary-digits ["000" "001" "010" "011"
-                             "100" "101" "110" "111"])
 (defun math-format-binary (a)   ; [X S]
-  (if (< a 8)
-      (if (< a 0)
-         (concat "-" (math-format-binary (- a)))
-       (aref math-binary-digits a))
-    (let ((s ""))
-      (while (> a 7)
-       (setq s (concat (aref math-binary-digits (% a 8)) s)
-             a (/ a 8)))
-      (concat (math-format-binary a) s))))
+  (let ((calc-number-radix 2))
+    (math-format-radix a)))
 
 ;;; Decompose into integer and fractional parts, without depending
 ;;; on calc-internal-prec.
diff --git a/lisp/calc/calc-ext.el b/lisp/calc/calc-ext.el
index 2036258..0b3c489 100644
--- a/lisp/calc/calc-ext.el
+++ b/lisp/calc/calc-ext.el
@@ -64,8 +64,6 @@
 (declare-function math-compose-expr "calccomp" (a prec &optional div))
 (declare-function math-abs "calc-arith" (a))
 (declare-function math-format-binary "calc-bin" (a))
-(declare-function math-format-octal "calc-bin" (a))
-(declare-function math-format-hex "calc-bin" (a))
 (declare-function math-format-radix "calc-bin" (a))
 (declare-function math-compute-max-digits "calc-bin" (w r))
 (declare-function math-map-vec "calc-vec" (f a))
@@ -3402,15 +3400,9 @@ If X is not an error form, return 1."
     a))
 
 (defun math--format-integer-fancy (a)   ; [I]
-  (let ((str (cond ((= calc-number-radix 10)
-                   (number-to-string a))
-                  ((= calc-number-radix 2)
-                   (math-format-binary a))
-                  ((= calc-number-radix 8)
-                   (math-format-octal a))
-                  ((= calc-number-radix 16)
-                   (math-format-hex a))
-                  (t (math-format-radix a)))))
+  (let ((str (if (= calc-number-radix 10)
+                (number-to-string a)
+              (math-format-radix a))))
     (if calc-leading-zeros
        (let* ((calc-internal-prec 6)
               (digs (math-compute-max-digits (math-abs calc-word-size)
diff --git a/test/lisp/calc/calc-tests.el b/test/lisp/calc/calc-tests.el
index 92f7497..77d939e 100644
--- a/test/lisp/calc/calc-tests.el
+++ b/test/lisp/calc/calc-tests.el
@@ -168,6 +168,32 @@ An existing calc stack is reused, otherwise a new one is 
created."
       (should (equal (math-simplify '(calcFunc-cot (/ (var pi var-pi) 3)))
                      '(calcFunc-cot (/ (var pi var-pi) 3)))))))
 
+(ert-deftest calc-test-format-radix ()
+  "Test integer formatting (bug#36689)."
+  (let ((calc-group-digits nil))
+    (let ((calc-number-radix 10))
+      (should (equal (math-format-number 12345678901) "12345678901")))
+    (let ((calc-number-radix 2))
+      (should (equal (math-format-number 12345) "2#11000000111001")))
+    (let ((calc-number-radix 8))
+      (should (equal (math-format-number 12345678901) "8#133767016065")))
+    (let ((calc-number-radix 16))
+      (should (equal (math-format-number 12345678901) "16#2DFDC1C35")))
+    (let ((calc-number-radix 36))
+      (should (equal (math-format-number 12345678901) "36#5O6AQT1"))))
+  (let ((calc-group-digits t))
+    (let ((calc-number-radix 10))
+      (should (equal (math-format-number 12345678901) "12,345,678,901")))
+    (let ((calc-number-radix 2))
+      (should (equal (math-format-number 12345) "2#11,0000,0011,1001")))
+    (let ((calc-number-radix 8))
+      (should (equal (math-format-number 12345678901) "8#133,767,016,065")))
+    (let ((calc-number-radix 16))
+      (should (equal (math-format-number 12345678901) "16#2,DFDC,1C35")))
+    (let ((calc-number-radix 36))
+      (should (equal (math-format-number 12345678901) "36#5,O6A,QT1")))))
+
+
 (provide 'calc-tests)
 ;;; calc-tests.el ends here
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]