From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Wolfgang Jenkner Newsgroups: gmane.emacs.bugs Subject: bug#19582: 25.0.50; [PATCH] Calc: glitches with negation of units Date: Tue, 13 Jan 2015 15:22:28 +0100 Message-ID: <851tmyah3n.fsf@iznogoud.viz> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1421159516 18602 80.91.229.3 (13 Jan 2015 14:31:56 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 13 Jan 2015 14:31:56 +0000 (UTC) To: 19582@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Tue Jan 13 15:31:49 2015 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YB2VX-0003im-HZ for geb-bug-gnu-emacs@m.gmane.org; Tue, 13 Jan 2015 15:31:47 +0100 Original-Received: from localhost ([::1]:39645 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YB2VW-0004jm-R9 for geb-bug-gnu-emacs@m.gmane.org; Tue, 13 Jan 2015 09:31:46 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60074) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YB2VF-0004No-8U for bug-gnu-emacs@gnu.org; Tue, 13 Jan 2015 09:31:30 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YB2V8-0007yX-NX for bug-gnu-emacs@gnu.org; Tue, 13 Jan 2015 09:31:28 -0500 Original-Received: from debbugs.gnu.org ([140.186.70.43]:46277) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YB2V8-0007yP-Jd for bug-gnu-emacs@gnu.org; Tue, 13 Jan 2015 09:31:22 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1YB2V6-0001X6-3s for bug-gnu-emacs@gnu.org; Tue, 13 Jan 2015 09:31:21 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Wolfgang Jenkner Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 13 Jan 2015 14:31:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 19582 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.14211594475847 (code B ref -1); Tue, 13 Jan 2015 14:31:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 13 Jan 2015 14:30:47 +0000 Original-Received: from localhost ([127.0.0.1]:55137 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1YB2UX-0001WE-M1 for submit@debbugs.gnu.org; Tue, 13 Jan 2015 09:30:46 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:57821) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1YB2UL-0001Vo-BY for submit@debbugs.gnu.org; Tue, 13 Jan 2015 09:30:35 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YB2U9-0007df-Vu for submit@debbugs.gnu.org; Tue, 13 Jan 2015 09:30:26 -0500 Original-Received: from lists.gnu.org ([2001:4830:134:3::11]:54746) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YB2U9-0007dW-RZ for submit@debbugs.gnu.org; Tue, 13 Jan 2015 09:30:21 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59420) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YB2U3-0002zI-U5 for bug-gnu-emacs@gnu.org; Tue, 13 Jan 2015 09:30:21 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YB2Ty-0007UM-4z for bug-gnu-emacs@gnu.org; Tue, 13 Jan 2015 09:30:15 -0500 Original-Received: from b2bfep14.mx.upcmail.net ([62.179.121.59]:37115) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YB2Tx-0007Qw-Ac for bug-gnu-emacs@gnu.org; Tue, 13 Jan 2015 09:30:10 -0500 Original-Received: from edge12.upcmail.net ([192.168.13.82]) by b2bfep14.mx.upcmail.net (InterMail vM.8.01.05.11 201-2260-151-128-20120928) with ESMTP id <20150113143005.EWIA11972.b2bfep14-int.chello.at@edge12.upcmail.net> for ; Tue, 13 Jan 2015 15:30:05 +0100 Original-Received: from iznogoud.viz ([85.127.90.213]) by edge12.upcmail.net with edge id fSW41p02K4cBfQ50CSW5e2; Tue, 13 Jan 2015 15:30:05 +0100 X-SourceIP: 85.127.90.213 Original-Received: from wolfgang by iznogoud.viz with local (Exim 4.84 (FreeBSD)) (envelope-from ) id 1YB2Ts-0000v0-PI; Tue, 13 Jan 2015 15:30:04 +0100 User-Agent: Gnus/5.130012 (Ma Gnus v0.12) Emacs/25.0.50 (berkeley-unix) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:98292 Archived-At: --=-=-= Content-Type: text/plain Start Calc (M-x calc). Try to convert -1m to cm. Type '-1muc Then Calc asks `(The expression is unitless when simplified) Old Units:' This is the first glitch here, but, regardless, answer the question m Now, it asks (correctly) for new units, so type cm This gives the surprising result `-100 m' on top of the stack. Now, if you do the same thing with -2 instead of -1, everything works fine. The reason is that some of the functions in calc-units.el which, in various ways, test for the presence of units in a given (simplified) expression forget to handle subexpressions of the form (neg ). I propose the attached patch, which also contains some modest additions to test/automated/cl-lib-tests.el. There's also some math-defsimplify stuff in calc-units.el which fails to handle the neg operator, but that shouldn't give rise to mathematical errors and will be done in another patch. This patch is also needed for (and, in fact, was prompted by) bug#19401. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Handle-the-neg-operator-in-some-calc-units-functions.patch Content-Description: neg operator glitches >From 3b8d9b69374036a353ddfa53733c58da2c799c1a Mon Sep 17 00:00:00 2001 From: Wolfgang Jenkner Date: Thu, 8 Jan 2015 21:13:44 +0100 Subject: [PATCH] Handle the `neg' operator in some calc-units functions. * lisp/calc/calc-units.el (math-units-in-expr-p) (math-single-units-in-expr-p, math-find-compatible-unit-rec) (math-extract-units): Handle the `neg' operator. * test/automated/calc-tests.el (calc-tests-equal, calc-tests-simple): New functions. (test-calc-remove-units, test-calc-extract-units) (test-calc-convert-units): New tests. --- lisp/ChangeLog | 6 ++++++ lisp/calc/calc-units.el | 16 ++++++++++---- test/ChangeLog | 7 +++++++ test/automated/calc-tests.el | 50 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 4 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 4985ad1..34136e4 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,9 @@ +2015-01-08 Wolfgang Jenkner + + * calc/calc-units.el (math-units-in-expr-p) + (math-single-units-in-expr-p, math-find-compatible-unit-rec) + (math-extract-units): Handle the `neg' operator. + 2015-01-12 Martin Rudalics * progmodes/xref.el (xref-marker-stack-empty-p): Add autoload diff --git a/lisp/calc/calc-units.el b/lisp/calc/calc-units.el index 26a644a..0595086 100644 --- a/lisp/calc/calc-units.el +++ b/lisp/calc/calc-units.el @@ -904,10 +904,12 @@ If COMP or STD is non-nil, put that in the units table instead." (and (consp expr) (if (eq (car expr) 'var) (math-check-unit-name expr) - (and (or sub-exprs - (memq (car expr) '(* / ^))) - (or (math-units-in-expr-p (nth 1 expr) sub-exprs) - (math-units-in-expr-p (nth 2 expr) sub-exprs)))))) + (if (eq (car expr) 'neg) + (math-units-in-expr-p (nth 1 expr) sub-exprs) + (and (or sub-exprs + (memq (car expr) '(* / ^))) + (or (math-units-in-expr-p (nth 1 expr) sub-exprs) + (math-units-in-expr-p (nth 2 expr) sub-exprs))))))) (defun math-only-units-in-expr-p (expr) (and (consp expr) @@ -924,6 +926,8 @@ If COMP or STD is non-nil, put that in the units table instead." (cond ((math-scalarp expr) nil) ((eq (car expr) 'var) (math-check-unit-name expr)) + ((eq (car expr) 'neg) + (math-single-units-in-expr-p (nth 1 expr))) ((eq (car expr) '*) (let ((u1 (math-single-units-in-expr-p (nth 1 expr))) (u2 (math-single-units-in-expr-p (nth 2 expr)))) @@ -1079,6 +1083,8 @@ If COMP or STD is non-nil, put that in the units table instead." ((eq (car-safe expr) '/) (or (math-find-compatible-unit-rec (nth 1 expr) pow) (math-find-compatible-unit-rec (nth 2 expr) (- pow)))) + ((eq (car-safe expr) 'neg) + (math-find-compatible-unit-rec (nth 1 expr) pow)) ((and (eq (car-safe expr) '^) (integerp (nth 2 expr))) (math-find-compatible-unit-rec (nth 1 expr) (* pow (nth 2 expr)))) @@ -1497,6 +1503,8 @@ If COMP or STD is non-nil, put that in the units table instead." ((memq (car-safe expr) '(* /)) (cons (car expr) (mapcar 'math-extract-units (cdr expr)))) + ((eq (car-safe expr) 'neg) + (math-extract-units (nth 1 expr))) ((eq (car-safe expr) '^) (list '^ (math-extract-units (nth 1 expr)) (nth 2 expr))) ((math-check-unit-name expr) expr) diff --git a/test/ChangeLog b/test/ChangeLog index 83bb8bf..527338b 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,3 +1,10 @@ +2015-01-08 Wolfgang Jenkner + + * automated/calc-tests.el (calc-tests-equal, calc-tests-simple): + New functions. + (test-calc-remove-units, test-calc-extract-units) + (test-calc-convert-units): New tests. + 2015-01-08 Stefan Monnier * automated/eieio-tests.el (eieio-test-23-inheritance-check): Don't use diff --git a/test/automated/calc-tests.el b/test/automated/calc-tests.el index 331e01e..d134d0a 100644 --- a/test/automated/calc-tests.el +++ b/test/automated/calc-tests.el @@ -27,6 +27,38 @@ (require 'cl-lib) (require 'ert) (require 'calc) +(require 'calc-ext) +(require 'calc-units) + +;; XXX Apparently, the representation of numbers is sometimes +;; influenced by previous results in the same calc stack. Hence this +;; function. +(defun calc-tests-equal (a b) + "Like `equal' but allow for different representations of numbers. +For example: (calc-tests-equal 10 '(float 1 1)) => t. +A and B should be calc expressions." + (cond ((math-numberp a) + (and (math-numberp b) + (math-equal a b))) + ((atom a) + (equal a b)) + ((consp b) + ;; Can't be dotted or circular. + (and (= (length a) (length b)) + (equal (car a) (car b)) + (cl-every #'calc-tests-equal (cdr a) (cdr b)))))) + +(defun calc-tests-simple (fun string &rest args) + "Push STRING on the calc stack, then call FUN and return the new top. +The result is a calc (i.e., lisp) expression, not its string representation. +Also pop the entire stack afterwards. +An existing calc stack is reused, otherwise a new one is created." + (calc-eval string 'push) + (prog1 + (ignore-errors + (apply fun args) + (calc-top-n 1)) + (calc-pop 0))) (ert-deftest test-math-bignum () ;; bug#17556 @@ -34,6 +66,24 @@ (should (math-negp n)) (should (cl-notany #'cl-minusp (cdr n))))) +(ert-deftest test-calc-remove-units () + (should (calc-tests-equal (calc-tests-simple #'calc-remove-units "-1 m") -1))) + +(ert-deftest test-calc-extract-units () + (should (calc-tests-equal (calc-tests-simple #'calc-extract-units "-1 m") + '(var m var-m))) + (should (calc-tests-equal (calc-tests-simple #'calc-extract-units "-1 m*cm") + '(* (float 1 -2) (^ (var m var-m) 2))))) + +(ert-deftest test-calc-convert-units () + ;; Used to ask for `(The expression is unitless when simplified) Old Units: '. + (should (calc-tests-equal (calc-tests-simple #'calc-convert-units "-1 m" nil "cm") + '(* -100 (var cm var-cm)))) + ;; Gave wrong result. + (should (calc-tests-equal (calc-tests-simple #'calc-convert-units "-1 m" + (math-read-expr "1m") "cm") + '(* -100 (var cm var-cm))))) + (provide 'calc-tests) ;;; calc-tests.el ends here -- 2.2.1 --=-=-=--