GNU bug report logs - #47302
27.1; calc math-format-number formatting for floats without decimals is unusual

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Reported by: Jelle Licht <jlicht@HIDDEN>; Keywords: patch; dated Sun, 21 Mar 2021 21:55:02 UTC; Maintainer for emacs is bug-gnu-emacs@HIDDEN.
Added tag(s) patch. Request was from Lars Ingebrigtsen <larsi@HIDDEN> to control <at> debbugs.gnu.org. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 21 Mar 2021 21:55:02 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Sun Mar 21 17:55:02 2021
Received: from localhost ([127.0.0.1]:55586 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1lO62H-0002nf-M8
	for submit <at> debbugs.gnu.org; Sun, 21 Mar 2021 17:55:02 -0400
Received: from lists.gnu.org ([209.51.188.17]:43892)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <jlicht@HIDDEN>) id 1lO62G-0002nY-6k
 for submit <at> debbugs.gnu.org; Sun, 21 Mar 2021 17:55:01 -0400
Received: from eggs.gnu.org ([2001:470:142:3::10]:49200)
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <jlicht@HIDDEN>) id 1lO62E-0007gA-Ed
 for bug-gnu-emacs@HIDDEN; Sun, 21 Mar 2021 17:54:59 -0400
Received: from mail1.fsfe.org ([2001:aa8:ffed:f5f3::151]:59802)
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <jlicht@HIDDEN>) id 1lO62C-0007PH-IO
 for bug-gnu-emacs@HIDDEN; Sun, 21 Mar 2021 17:54:58 -0400
From: Jelle Licht <jlicht@HIDDEN>
To: bug-gnu-emacs@HIDDEN
Subject: 27.1; calc math-format-number formatting for floats without
 decimals is unusual
Date: Sun, 21 Mar 2021 22:54:50 +0100
Message-ID: <86k0q0nped.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
Received-SPF: pass client-ip=2001:aa8:ffed:f5f3::151;
 envelope-from=jlicht@HIDDEN; helo=mail1.fsfe.org
X-Spam_score_int: -41
X-Spam_score: -4.2
X-Spam_bar: ----
X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3,
 SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: -1.3 (-)
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -2.3 (--)

--=-=-=
Content-Type: text/plain

Hey folks,

I run into the following behaviour when using calc in Emacs 27.1 (using
`emacs -Q'):

--8<---------------cut here---------------start------------->8---
(calc-eval "123.0") ; => "123."
(calc-eval "0.0") ; => "0."
--8<---------------cut here---------------end--------------->8---

I would expect this output to be either "123.0" or "123" and "0.0" or
"0" respectively.

I have attached a super simple patch that fixes this issue for me, but I
am not actually certain of the following:

- Was this formatting quirk perhaps there with a purpose? 
- Does my 'fix' have unintended consequences?

Thanks,
 Jelle


--=-=-=
Content-Type: text/x-patch
Content-Disposition: inline;
 filename=0001-calc-Fix-formatting-of-floats-without-decimals.patch

From 88e52b073510c86b582487a65fa8ddf52a48e7dd Mon Sep 17 00:00:00 2001
From: Jelle Licht <jlicht@HIDDEN>
Date: Sun, 21 Mar 2021 22:39:41 +0100
Subject: [PATCH] (calc): Fix formatting of floats without decimals

* lisp/calc/calc.el (math-format-number): Fix formatting for floats
  without decimals.
---
 lisp/calc/calc.el | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lisp/calc/calc.el b/lisp/calc/calc.el
index ec09abb34c..fac9f70915 100644
--- a/lisp/calc/calc.el
+++ b/lisp/calc/calc.el
@@ -3193,8 +3193,9 @@ math-format-number
 		   ((= dpos 0)
 		    (setq str (concat "0" point str)))
 		   ((and (<= exp 0) (> dpos 0))
-		    (setq str (concat (substring str 0 dpos) point
-				      (substring str dpos))))
+                    (when (> len dpos)
+		      (setq str (concat (substring str 0 dpos) point
+				        (substring str dpos)))))
 		   ((> exp 0)
 		    (setq str (concat str (make-string exp ?0) point)))
 		   (t   ; (< dpos 0)
-- 
2.31.0


--=-=-=--




Acknowledgement sent to Jelle Licht <jlicht@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to bug-gnu-emacs@HIDDEN:
bug#47302; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Mon, 22 Mar 2021 19:00:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.