emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] master c48e539 2/2: Use the expression angle units while s


From: Tino Calancha
Subject: [Emacs-diffs] master c48e539 2/2: Use the expression angle units while simplifying it
Date: Thu, 18 May 2017 01:47:57 -0400 (EDT)

branch: master
commit c48e539226a062d01b8d5534644fae959506a743
Author: Tino Calancha <address@hidden>
Commit: Tino Calancha <address@hidden>

    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.
---
 lisp/calc/calc-alg.el        |  9 +++++++++
 lisp/calc/calc-forms.el      | 12 +++++++++---
 lisp/calc/calc-math.el       | 12 ++++++++----
 test/lisp/calc/calc-tests.el | 44 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 7 deletions(-)

diff --git a/lisp/calc/calc-alg.el b/lisp/calc/calc-alg.el
index 4e63d23..9db901a 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 abf76cf..6aa421e 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 faa318d..2590761 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 8f56d48..68567dc 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
 



reply via email to

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