aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias EngdegÄrd2019-07-16 17:18:32 +0200
committerMattias EngdegÄrd2019-07-16 17:37:46 +0200
commita87840fffbf471d53eba17ea683728125d2d4767 (patch)
tree3529d052a679c420f27a7d44bdbb8d80d9f7779d
parent0a2461be9edb218bf9ca56156d8966a2421f13a7 (diff)
downloademacs-a87840fffbf471d53eba17ea683728125d2d4767.tar.gz
emacs-a87840fffbf471d53eba17ea683728125d2d4767.zip
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.
-rw-r--r--lisp/calc/calc-bin.el13
-rw-r--r--lisp/calc/calc-ext.el14
-rw-r--r--test/lisp/calc/calc-tests.el26
3 files changed, 31 insertions, 22 deletions
diff --git a/lisp/calc/calc-bin.el b/lisp/calc/calc-bin.el
index b4371bdaf98..558e309e472 100644
--- a/lisp/calc/calc-bin.el
+++ b/lisp/calc/calc-bin.el
@@ -506,18 +506,9 @@
506 a (/ a calc-number-radix))) 506 a (/ a calc-number-radix)))
507 s))) 507 s)))
508 508
509(defconst math-binary-digits ["000" "001" "010" "011"
510 "100" "101" "110" "111"])
511(defun math-format-binary (a) ; [X S] 509(defun math-format-binary (a) ; [X S]
512 (if (< a 8) 510 (let ((calc-number-radix 2))
513 (if (< a 0) 511 (math-format-radix a)))
514 (concat "-" (math-format-binary (- a)))
515 (aref math-binary-digits a))
516 (let ((s ""))
517 (while (> a 7)
518 (setq s (concat (aref math-binary-digits (% a 8)) s)
519 a (/ a 8)))
520 (concat (math-format-binary a) s))))
521 512
522;;; Decompose into integer and fractional parts, without depending 513;;; Decompose into integer and fractional parts, without depending
523;;; on calc-internal-prec. 514;;; on calc-internal-prec.
diff --git a/lisp/calc/calc-ext.el b/lisp/calc/calc-ext.el
index 203625873a5..0b3c489d453 100644
--- a/lisp/calc/calc-ext.el
+++ b/lisp/calc/calc-ext.el
@@ -64,8 +64,6 @@
64(declare-function math-compose-expr "calccomp" (a prec &optional div)) 64(declare-function math-compose-expr "calccomp" (a prec &optional div))
65(declare-function math-abs "calc-arith" (a)) 65(declare-function math-abs "calc-arith" (a))
66(declare-function math-format-binary "calc-bin" (a)) 66(declare-function math-format-binary "calc-bin" (a))
67(declare-function math-format-octal "calc-bin" (a))
68(declare-function math-format-hex "calc-bin" (a))
69(declare-function math-format-radix "calc-bin" (a)) 67(declare-function math-format-radix "calc-bin" (a))
70(declare-function math-compute-max-digits "calc-bin" (w r)) 68(declare-function math-compute-max-digits "calc-bin" (w r))
71(declare-function math-map-vec "calc-vec" (f a)) 69(declare-function math-map-vec "calc-vec" (f a))
@@ -3402,15 +3400,9 @@ If X is not an error form, return 1."
3402 a)) 3400 a))
3403 3401
3404(defun math--format-integer-fancy (a) ; [I] 3402(defun math--format-integer-fancy (a) ; [I]
3405 (let ((str (cond ((= calc-number-radix 10) 3403 (let ((str (if (= calc-number-radix 10)
3406 (number-to-string a)) 3404 (number-to-string a)
3407 ((= calc-number-radix 2) 3405 (math-format-radix a))))
3408 (math-format-binary a))
3409 ((= calc-number-radix 8)
3410 (math-format-octal a))
3411 ((= calc-number-radix 16)
3412 (math-format-hex a))
3413 (t (math-format-radix a)))))
3414 (if calc-leading-zeros 3406 (if calc-leading-zeros
3415 (let* ((calc-internal-prec 6) 3407 (let* ((calc-internal-prec 6)
3416 (digs (math-compute-max-digits (math-abs calc-word-size) 3408 (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 92f74976b00..77d939eb406 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."
168 (should (equal (math-simplify '(calcFunc-cot (/ (var pi var-pi) 3))) 168 (should (equal (math-simplify '(calcFunc-cot (/ (var pi var-pi) 3)))
169 '(calcFunc-cot (/ (var pi var-pi) 3))))))) 169 '(calcFunc-cot (/ (var pi var-pi) 3)))))))
170 170
171(ert-deftest calc-test-format-radix ()
172 "Test integer formatting (bug#36689)."
173 (let ((calc-group-digits nil))
174 (let ((calc-number-radix 10))
175 (should (equal (math-format-number 12345678901) "12345678901")))
176 (let ((calc-number-radix 2))
177 (should (equal (math-format-number 12345) "2#11000000111001")))
178 (let ((calc-number-radix 8))
179 (should (equal (math-format-number 12345678901) "8#133767016065")))
180 (let ((calc-number-radix 16))
181 (should (equal (math-format-number 12345678901) "16#2DFDC1C35")))
182 (let ((calc-number-radix 36))
183 (should (equal (math-format-number 12345678901) "36#5O6AQT1"))))
184 (let ((calc-group-digits t))
185 (let ((calc-number-radix 10))
186 (should (equal (math-format-number 12345678901) "12,345,678,901")))
187 (let ((calc-number-radix 2))
188 (should (equal (math-format-number 12345) "2#11,0000,0011,1001")))
189 (let ((calc-number-radix 8))
190 (should (equal (math-format-number 12345678901) "8#133,767,016,065")))
191 (let ((calc-number-radix 16))
192 (should (equal (math-format-number 12345678901) "16#2,DFDC,1C35")))
193 (let ((calc-number-radix 36))
194 (should (equal (math-format-number 12345678901) "36#5,O6A,QT1")))))
195
196
171(provide 'calc-tests) 197(provide 'calc-tests)
172;;; calc-tests.el ends here 198;;; calc-tests.el ends here
173 199