From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.bugs Subject: bug#47302: 27.1; calc math-format-number formatting for floats without decimals is unusual Date: Sun, 25 Apr 2021 17:53:30 +0200 Message-ID: <0E7C5A4F-105A-4E1E-A103-BFB9C263F2BA@acm.org> References: <86k0q0nped.fsf@fsfe.org> <86v98ejr8x.fsf@fsfe.org> Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.17\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_18EF807A-717E-4D2A-8162-9126BB002BB1" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29060"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Jelle Licht , Stefan Kangas , "47302@debbugs.gnu.org" <47302@debbugs.gnu.org> To: Dave Gillespie Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Apr 25 17:54:59 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lah62-0007N1-HG for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 25 Apr 2021 17:54:58 +0200 Original-Received: from localhost ([::1]:42518 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lah61-00062X-EX for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 25 Apr 2021 11:54:57 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:45162) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lah58-00061N-Fx for bug-gnu-emacs@gnu.org; Sun, 25 Apr 2021 11:54:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:60214) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lah58-0001TH-9G for bug-gnu-emacs@gnu.org; Sun, 25 Apr 2021 11:54:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lah58-0006K9-8Q for bug-gnu-emacs@gnu.org; Sun, 25 Apr 2021 11:54:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 25 Apr 2021 15:54:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 47302 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: wontfix Original-Received: via spool by 47302-submit@debbugs.gnu.org id=B47302.161936602224271 (code B ref 47302); Sun, 25 Apr 2021 15:54:02 +0000 Original-Received: (at 47302) by debbugs.gnu.org; 25 Apr 2021 15:53:42 +0000 Original-Received: from localhost ([127.0.0.1]:43523 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lah4o-0006JP-5d for submit@debbugs.gnu.org; Sun, 25 Apr 2021 11:53:42 -0400 Original-Received: from mail1459c50.megamailservers.eu ([91.136.14.59]:48678 helo=mail267c50.megamailservers.eu) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lah4m-0006JA-DB for 47302@debbugs.gnu.org; Sun, 25 Apr 2021 11:53:41 -0400 X-Authenticated-User: mattiase@bredband.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=megamailservers.eu; s=maildub; t=1619366013; bh=aQzsr9ottiLsDy4lQ7MiJyddU97cKUjjqTM4sV/POM4=; h=From:Subject:Date:In-Reply-To:Cc:To:References:From; b=WMawoZWXBK+hLzC+O0wr0d8ZNffTPAD6WRAr+0zSufJfKmWhAdWNAblWKl4B7jAw8 QkE3L1+ik4MlQ3XN67j+qi0cOSzIMinzdF/rJVWrhoqGf4hc/zYs6UZsrpPCi/hKHp R7G4J5bWzeTZ6yzDyO8nH6aY9zbT659/GadsIAlg= Feedback-ID: mattiase@acm.or Original-Received: from [192.168.0.4] (c188-150-171-71.bredband.comhem.se [188.150.171.71]) (authenticated bits=0) by mail267c50.megamailservers.eu (8.14.9/8.13.1) with ESMTP id 13PFrVWl015705; Sun, 25 Apr 2021 15:53:32 +0000 In-Reply-To: X-Mailer: Apple Mail (2.3445.104.17) X-CTCH-RefID: str=0001.0A742F22.6085907D.001C, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0 X-CTCH-VOD: Unknown X-CTCH-Spam: Unknown X-CTCH-Score: 0.000 X-CTCH-Flags: 0 X-CTCH-ScoreCust: 0.000 X-CSC: 0 X-CHA: v=2.3 cv=OpoxNB3t c=1 sm=1 tr=0 a=SF+I6pRkHZhrawxbOkkvaA==:117 a=SF+I6pRkHZhrawxbOkkvaA==:17 a=M51BFTxLslgA:10 a=swAopTDVAAAA:8 a=S2u0-_idYBEBAFvFCK8A:9 a=CjuIK1q_8ugA:10 a=b5CK2jE2_QR4IXyYsw8A:9 a=De_Ol2h6w80A:10 a=7aPqbiTMDNvH6kBvy-t8:22 X-Origin-Country: SE X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list 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-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:204884 Archived-At: --Apple-Mail=_18EF807A-717E-4D2A-8162-9126BB002BB1 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii 22 apr. 2021 kl. 18.35 skrev Dave Gillespie : > Wow, it has been a long time since I got any correspondence on Calc! Good to hear from you, Dave! I just fix the occasional Calc bug now and = then. > Calc has a C language mode ('d C' keystroke, controlled by = calc-language). It is a good point that C mode (and probably others = like it) should format integer-valued floats as "123.0" even if the = default mode does not. If we apply Jelle's patch, I suggest making it = conditional on calc-language so that it applies only in modes such as C = mode. Or perhaps rework it as a text transformation using = calc-language-filter. >=20 > You could even create a JSON language mode, but most likely the basic = C mode is close enough to serve that purpose. These are all good suggestions. Most languages permit trailing decimal = points; the only common exceptions that I can think of are Haskell, Ada = and Swift. Apparently JSON is also one. Tying the float-format display = to the C, Pascal (etc) modes seems a bit incongruous as it has nothing = to do with the syntax of those languages. How to display a floating-point number with zero fraction also depends = on what the user wants to do with the result, so there is a good = argument for letting him or her do the required post-processing. = Sometimes '1.' should become '1.0', sometimes '1'. For instance, in a = LaTeX document it would depend on how the author wants to represent = significant digits. A JSON parser (such as the one in Emacs) will parse = '1.0' and '1' differently, as a float or integer respectively. I wrote the patch below as a possible solution but in the light of the = above, perhaps it's not ideal? --Apple-Mail=_18EF807A-717E-4D2A-8162-9126BB002BB1 Content-Disposition: attachment; filename=calc-digit-after-point.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="calc-digit-after-point.diff" Content-Transfer-Encoding: 7bit diff --git a/lisp/calc/calc.el b/lisp/calc/calc.el index ec09abb34c..1e7d5e7766 100644 --- a/lisp/calc/calc.el +++ b/lisp/calc/calc.el @@ -483,6 +483,11 @@ calc-display-sci-low "Floating-point numbers with this negative exponent or lower are displayed scientific notation in calc-mode.") +(defvar calc-digit-after-point nil + "If t, display at least one digit after the decimal point, as in `12.0'. +If nil, the decimal point may come last in a number, as in `12.'. +This setting only applies to floats in normal display mode.") + (defvar calc-other-modes nil "List of used-defined strings to append to Calculator mode line.") @@ -3184,7 +3189,8 @@ math-format-number exp (- exp adj))))) (setq str (int-to-string mant)) (let* ((len (length str)) - (dpos (+ exp len))) + (dpos (+ exp len)) + (trailing-0 (and calc-digit-after-point "0"))) (if (and (eq fmt 'float) (<= dpos (+ calc-internal-prec calc-display-sci-high)) (>= dpos (+ calc-display-sci-low 2))) @@ -3194,9 +3200,11 @@ math-format-number (setq str (concat "0" point str))) ((and (<= exp 0) (> dpos 0)) (setq str (concat (substring str 0 dpos) point - (substring str dpos)))) + (substring str dpos) + (and (>= dpos len) trailing-0)))) ((> exp 0) - (setq str (concat str (make-string exp ?0) point))) + (setq str (concat str (make-string exp ?0) + point trailing-0))) (t ; (< dpos 0) (setq str (concat "0" point (make-string (- dpos) ?0) str)))) diff --git a/test/lisp/calc/calc-tests.el b/test/lisp/calc/calc-tests.el index c5aa5a31eb..06010dd417 100644 --- a/test/lisp/calc/calc-tests.el +++ b/test/lisp/calc/calc-tests.el @@ -191,6 +191,33 @@ calc-format-radix (let ((calc-number-radix 36)) (should (equal (math-format-number 12345678901) "36#5,O6A,QT1"))))) +(ert-deftest calc-display-digit-after-point () + "Test display of trailing 0 after decimal point (bug#47302)." + (let ((calc-display-digit-after-point nil)) + ;; Integral floats have no digits after the decimal point (default). + (should (equal (math-format-number '(float 0 0)) "0.")) + (should (equal (math-format-number '(float 5 0)) "5.")) + (should (equal (math-format-number '(float 3 1)) "30.")) + (should (equal (math-format-number '(float 23 0)) "23.")) + (should (equal (math-format-number '(float 123 0)) "123.")) + (should (equal (math-format-number '(float 1 -1)) "0.1")) + (should (equal (math-format-number '(float 54 -1)) "5.4")) + (should (equal (math-format-number '(float 1 -4)) "1e-4")) + (should (equal (math-format-number '(float 1 14)) "1e14")) + (should (equal (math-format-number 12) "12"))) + (let ((calc-display-digit-after-point t)) + ;; Integral floats have at least one digit after the decimal point. + (should (equal (math-format-number '(float 0 0)) "0.0")) + (should (equal (math-format-number '(float 5 0)) "5.0")) + (should (equal (math-format-number '(float 3 1)) "30.0")) + (should (equal (math-format-number '(float 23 0)) "23.0")) + (should (equal (math-format-number '(float 123 0)) "123.0")) + (should (equal (math-format-number '(float 1 -1)) "0.1")) + (should (equal (math-format-number '(float 54 -1)) "5.4")) + (should (equal (math-format-number '(float 1 -4)) "1e-4")) + (should (equal (math-format-number '(float 1 14)) "1e14")) + (should (equal (math-format-number 12) "12")))) + (ert-deftest calc-calendar () "Test calendar conversions (bug#36822)." (should (equal (calcFunc-julian (math-parse-date "2019-07-27")) 2458692)) --Apple-Mail=_18EF807A-717E-4D2A-8162-9126BB002BB1--