;; 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)))
(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
(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)
(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)))
(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))))
(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