From: Tino Calancha Date: Thu, 18 May 2017 05:47:06 +0000 (+0900) Subject: Use the expression angle units while simplifying it X-Git-Tag: emacs-26.0.90~521^2~374 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=c48e539226a062d01b8d5534644fae959506a743;p=emacs.git Use the expression angle units while simplifying it Don't use the angle mode, use the angle units included in the expression instead (Bug#23889). * lisp/calc/calc-alg.el (calc-input-angle-units): New defun. (math-simplify): Use it. * lisp/calc/calc-forms.el (math-to-hms, math-from-hms): Don't use calc-angle-mode if math-simplifying-units is non-nil. * lisp/calc/calc-math.el (calcFunc-nroot, math-from-radians) (math-to-radians-2, math-from-radians-2): Don't convert angle to radians if math-simplifying-units is non-nil. * test/lisp/calc/calc-tests.el (test-calc-23889): Add test. --- diff --git a/lisp/calc/calc-alg.el b/lisp/calc/calc-alg.el index 4e63d238c78..9db901a9753 100644 --- a/lisp/calc/calc-alg.el +++ b/lisp/calc/calc-alg.el @@ -355,10 +355,19 @@ ;; math-simplify-step, which is called by math-simplify. (defvar math-top-only) +(defun calc-input-angle-units (input) + (cond ((math-expr-contains input '(var deg var-deg)) 'deg) + ((math-expr-contains input '(var rad var-rad)) 'rad) + ((math-expr-contains input '(var hms var-hms)) 'hms) + (t nil))) + ;; math-normalize-error is declared in calc.el. (defvar math-normalize-error) (defun math-simplify (top-expr) (let ((math-simplifying t) + (calc-angle-mode (if (calc-input-angle-units top-expr) + 'rad + calc-angle-mode)) (math-top-only (consp calc-simplify-mode)) (simp-rules (append (and (calc-has-rules 'var-AlgSimpRules) '((var AlgSimpRules var-AlgSimpRules))) diff --git a/lisp/calc/calc-forms.el b/lisp/calc/calc-forms.el index abf76cf07ed..6aa421ec205 100644 --- a/lisp/calc/calc-forms.el +++ b/lisp/calc/calc-forms.el @@ -317,7 +317,9 @@ (list 'calcFunc-hms a)) ((math-negp a) (math-neg (math-to-hms (math-neg a) ang))) - ((eq (or ang calc-angle-mode) 'rad) + ((eq (or ang + (and (not math-simplifying-units) calc-angle-mode)) + 'rad) (math-to-hms (math-div a (math-pi-over-180)) 'deg)) ((memq (car-safe a) '(cplx polar)) a) (t @@ -354,12 +356,16 @@ (if (eq (car-safe a) 'sdev) (math-make-sdev (math-from-hms (nth 1 a) ang) (math-from-hms (nth 2 a) ang)) - (if (eq (or ang calc-angle-mode) 'rad) + (if (eq (or ang + (and (not math-simplifying-units) calc-angle-mode)) + 'rad) (list 'calcFunc-rad a) (list 'calcFunc-deg a))))) ((math-negp a) (math-neg (math-from-hms (math-neg a) ang))) - ((eq (or ang calc-angle-mode) 'rad) + ((eq (or ang + (and (not math-simplifying-units) calc-angle-mode)) + 'rad) (math-mul (math-from-hms a 'deg) (math-pi-over-180))) (t (math-add (math-div (math-add (math-div (nth 3 a) diff --git a/lisp/calc/calc-math.el b/lisp/calc/calc-math.el index faa318d45d0..2590761d539 100644 --- a/lisp/calc/calc-math.el +++ b/lisp/calc/calc-math.el @@ -763,12 +763,14 @@ If this can't be done, return NIL." (defun math-to-radians (a) ; [N N] (cond ((eq (car-safe a) 'hms) (math-from-hms a 'rad)) - ((memq calc-angle-mode '(deg hms)) + ((and (not math-simplifying-units) + (memq calc-angle-mode '(deg hms))) (math-mul a (math-pi-over-180))) (t a))) (defun math-from-radians (a) ; [N N] - (cond ((eq calc-angle-mode 'deg) + (cond ((and (not math-simplifying-units) + (eq calc-angle-mode 'deg)) (if (math-constp a) (math-div a (math-pi-over-180)) (list 'calcFunc-deg a))) @@ -779,14 +781,16 @@ If this can't be done, return NIL." (defun math-to-radians-2 (a &optional force-symbolic) ; [N N] (cond ((eq (car-safe a) 'hms) (math-from-hms a 'rad)) - ((memq calc-angle-mode '(deg hms)) + ((and (not math-simplifying-units) + (memq calc-angle-mode '(deg hms))) (if (or calc-symbolic-mode force-symbolic) (math-div (math-mul a '(var pi var-pi)) 180) (math-mul a (math-pi-over-180)))) (t a))) (defun math-from-radians-2 (a &optional force-symbolic) ; [N N] - (cond ((memq calc-angle-mode '(deg hms)) + (cond ((and (not math-simplifying-units) + (memq calc-angle-mode '(deg hms))) (if (or calc-symbolic-mode force-symbolic) (math-div (math-mul 180 a) '(var pi var-pi)) (math-div a (math-pi-over-180)))) diff --git a/test/lisp/calc/calc-tests.el b/test/lisp/calc/calc-tests.el index 8f56d48d01d..68567dcc212 100644 --- a/test/lisp/calc/calc-tests.el +++ b/test/lisp/calc/calc-tests.el @@ -86,6 +86,50 @@ An existing calc stack is reused, otherwise a new one is created." (math-read-expr "1m") "cm") '(* -100 (var cm var-cm))))) +(ert-deftest test-calc-23889 () + "Test for http://debbugs.gnu.org/23889 and 25652." + (dolist (mode '(deg rad)) + (let ((calc-angle-mode mode)) + ;; If user inputs angle units, then should ignore `calc-angle-mode'. + (should (string= "5253" + (substring + (number-to-string + (nth 1 + (math-simplify-units + '(calcFunc-cos (* 45 (var rad var-rad)))))) + 0 4))) + (should (string= "7071" + (substring + (number-to-string + (nth 1 + (math-simplify-units + '(calcFunc-cos (* 45 (var deg var-deg)))))) + 0 4))) + (should (string= "8939" + (substring + (number-to-string + (nth 1 + (math-simplify-units + '(+ (calcFunc-sin (* 90 (var rad var-rad))) + (calcFunc-cos (* 90 (var deg var-deg))))))) + 0 4))) + (should (string= "5519" + (substring + (number-to-string + (nth 1 + (math-simplify-units + '(+ (calcFunc-sin (* 90 (var deg var-deg))) + (calcFunc-cos (* 90 (var rad var-rad))))))) + 0 4))) + ;; If user doesn't input units, then must use `calc-angle-mode'. + (should (string= (if (eq calc-angle-mode 'deg) + "9998" + "5403") + (substring + (number-to-string + (nth 1 (calcFunc-cos 1))) + 0 4)))))) + (provide 'calc-tests) ;;; calc-tests.el ends here