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

Previous Next

Package: emacs;

Reported by: Jelle Licht <jlicht <at> fsfe.org>

Date: Sun, 21 Mar 2021 21:55:02 UTC

Severity: normal

Tags: wontfix

Found in version 27.1

Done: Mattias Engdegård <mattiase <at> acm.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 47302 in the body.
You can then email your comments to 47302 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#47302; Package emacs. (Sun, 21 Mar 2021 21:55:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Jelle Licht <jlicht <at> fsfe.org>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 21 Mar 2021 21:55:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Jelle Licht <jlicht <at> fsfe.org>
To: bug-gnu-emacs <at> gnu.org
Subject: 27.1; calc math-format-number formatting for floats without
 decimals is unusual
Date: Sun, 21 Mar 2021 22:54:50 +0100
[Message part 1 (text/plain, inline)]
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

[0001-calc-Fix-formatting-of-floats-without-decimals.patch (text/x-patch, inline)]
From 88e52b073510c86b582487a65fa8ddf52a48e7dd Mon Sep 17 00:00:00 2001
From: Jelle Licht <jlicht <at> fsfe.org>
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


Added tag(s) patch. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Mon, 22 Mar 2021 18:58:01 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#47302; Package emacs. (Wed, 21 Apr 2021 15:17:02 GMT) Full text and rfc822 format available.

Message #10 received at 47302 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Kangas <stefan <at> marxist.se>
To: Jelle Licht <jlicht <at> fsfe.org>
Cc: Mattias Engdegård <mattiase <at> acm.org>,
 David Gillespie <daveg <at> synaptics.com>, 47302 <at> debbugs.gnu.org
Subject: Re: bug#47302: 27.1; calc math-format-number formatting for floats
 without decimals is unusual
Date: Wed, 21 Apr 2021 10:16:35 -0500
Jelle Licht <jlicht <at> fsfe.org> writes:

> Hey folks,
>
> I run into the following behaviour when using calc in Emacs 27.1 (using
> `emacs -Q'):
>
> (calc-eval "123.0") ; => "123."
> (calc-eval "0.0") ; => "0."
>
> 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?

Copying in Mattias Engdegård in the hope that he can provide some
insight here.

I also put David Gillespie on Cc.

>
> Thanks,
>  Jelle
>
>>From 88e52b073510c86b582487a65fa8ddf52a48e7dd Mon Sep 17 00:00:00 2001
> From: Jelle Licht <jlicht <at> fsfe.org>
> 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)




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#47302; Package emacs. (Thu, 22 Apr 2021 13:45:02 GMT) Full text and rfc822 format available.

Message #13 received at 47302 <at> debbugs.gnu.org (full text, mbox):

From: Mattias Engdegård <mattiase <at> acm.org>
To: Stefan Kangas <stefan <at> marxist.se>
Cc: David Gillespie <daveg <at> synaptics.com>, Jelle Licht <jlicht <at> fsfe.org>,
 47302 <at> debbugs.gnu.org
Subject: Re: bug#47302: 27.1; calc math-format-number formatting for floats
 without decimals is unusual
Date: Thu, 22 Apr 2021 15:44:07 +0200
21 apr. 2021 kl. 17.16 skrev Stefan Kangas <stefan <at> marxist.se>:
> 
> Jelle Licht <jlicht <at> fsfe.org> writes:

>> (calc-eval "123.0") ; => "123."
>> (calc-eval "0.0") ; => "0."
>> 
>> I would expect this output to be either "123.0" or "123" and "0.0" or
>> "0" respectively.

Jelle, thanks for the report!

Calc distinguishes integers from floating-point numbers by the presence of a decimal point, so removing it is probably not a good idea. The lack of a trailing 0 after the decimal point is clearly intended but mainly a cosmetic issue; it's uncommon in hand-writing but not in computerese.

We could put a 0 after the decimal point but not doing so saves some space in the often narrow Calc windows. I'd suggest leaving it as it is. The notation is distinctive enough that there is no serious risk of misunderstanding.

If you disagree, do say.





Added tag(s) wontfix. Request was from Stefan Kangas <stefan <at> marxist.se> to control <at> debbugs.gnu.org. (Thu, 22 Apr 2021 14:16:02 GMT) Full text and rfc822 format available.

Removed tag(s) patch. Request was from Stefan Kangas <stefan <at> marxist.se> to control <at> debbugs.gnu.org. (Thu, 22 Apr 2021 14:25:01 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#47302; Package emacs. (Thu, 22 Apr 2021 15:04:02 GMT) Full text and rfc822 format available.

Message #20 received at 47302 <at> debbugs.gnu.org (full text, mbox):

From: Jelle Licht <jlicht <at> fsfe.org>
To: Mattias Engdegård <mattiase <at> acm.org>, Stefan Kangas
 <stefan <at> marxist.se>
Cc: David Gillespie <daveg <at> synaptics.com>, 47302 <at> debbugs.gnu.org
Subject: Re: bug#47302: 27.1; calc math-format-number formatting for floats
 without decimals is unusual
Date: Thu, 22 Apr 2021 17:03:42 +0200
Mattias Engdegård <mattiase <at> acm.org> writes:

> 21 apr. 2021 kl. 17.16 skrev Stefan Kangas <stefan <at> marxist.se>:
>> 
>> Jelle Licht <jlicht <at> fsfe.org> writes:
>
>>> (calc-eval "123.0") ; => "123."
>>> (calc-eval "0.0") ; => "0."
>>> 
>>> I would expect this output to be either "123.0" or "123" and "0.0" or
>>> "0" respectively.
>
> Jelle, thanks for the report!
>
> Calc distinguishes integers from floating-point numbers by the presence of a decimal point, so removing it is probably not a good idea. The lack of a trailing 0 after the decimal point is clearly intended but mainly a cosmetic issue; it's uncommon in hand-writing but not in computerese.
>
> We could put a 0 after the decimal point but not doing so saves some space in the often narrow Calc windows. I'd suggest leaving it as it is. The notation is distinctive enough that there is no serious risk of misunderstanding.
>
> If you disagree, do say.
Hi Mattias,

It would be nice to have a (formatting) option to print 'full'
floating-point numbers. There are a lot of other knobs to tweak
w.r.t. printing numbers, so in that sense it does not seem totally
inconceivable for such a thing to exist.

The context in which I would use this is one where the output of calc is
being used by other programs and data formats (e.g. JSON). For now, I'll
just have to have emacs read and then subsequently print the result of
my calc-adventures, which works fine.

Thanks for the consideration,
 - Jelle




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#47302; Package emacs. (Thu, 22 Apr 2021 16:45:02 GMT) Full text and rfc822 format available.

Message #23 received at 47302 <at> debbugs.gnu.org (full text, mbox):

From: Dave Gillespie <Daveg <at> synaptics.com>
To: Jelle Licht <jlicht <at> fsfe.org>, Mattias Engdegård
 <mattiase <at> acm.org>, Stefan Kangas <stefan <at> marxist.se>
Cc: "47302 <at> debbugs.gnu.org" <47302 <at> debbugs.gnu.org>
Subject: RE: bug#47302: 27.1; calc math-format-number formatting for floats
 without decimals is unusual
Date: Thu, 22 Apr 2021 16:35:25 +0000
Wow, it has been a long time since I got any correspondence on Calc!

Yes, I used the notation "123." versus "123.0" with the goal to keep the output concise for casual interactive users.  I agree with Mattias that it is best to leave the default as it is.

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.

You could even create a JSON language mode, but most likely the basic C mode is close enough to serve that purpose.

Thanks!

                                                                -- Dave



-----Original Message-----
From: Jelle Licht <jlicht <at> fsfe.org> 
Sent: Thursday, April 22, 2021 8:04 AM
To: Mattias Engdegård <mattiase <at> acm.org>; Stefan Kangas <stefan <at> marxist.se>
Cc: 47302 <at> debbugs.gnu.org; Dave Gillespie <Daveg <at> synaptics.com>
Subject: Re: bug#47302: 27.1; calc math-format-number formatting for floats without decimals is unusual

CAUTION: Email originated externally, do not click links or open attachments unless you recognize the sender and know the content is safe.


Mattias Engdegård <mattiase <at> acm.org> writes:

> 21 apr. 2021 kl. 17.16 skrev Stefan Kangas <stefan <at> marxist.se>:
>>
>> Jelle Licht <jlicht <at> fsfe.org> writes:
>
>>> (calc-eval "123.0") ; => "123."
>>> (calc-eval "0.0") ; => "0."
>>>
>>> I would expect this output to be either "123.0" or "123" and "0.0" or
>>> "0" respectively.
>
> Jelle, thanks for the report!
>
> Calc distinguishes integers from floating-point numbers by the presence of a decimal point, so removing it is probably not a good idea. The lack of a trailing 0 after the decimal point is clearly intended but mainly a cosmetic issue; it's uncommon in hand-writing but not in computerese.
>
> We could put a 0 after the decimal point but not doing so saves some space in the often narrow Calc windows. I'd suggest leaving it as it is. The notation is distinctive enough that there is no serious risk of misunderstanding.
>
> If you disagree, do say.
Hi Mattias,

It would be nice to have a (formatting) option to print 'full'
floating-point numbers. There are a lot of other knobs to tweak
w.r.t. printing numbers, so in that sense it does not seem totally
inconceivable for such a thing to exist.

The context in which I would use this is one where the output of calc is
being used by other programs and data formats (e.g. JSON). For now, I'll
just have to have emacs read and then subsequently print the result of
my calc-adventures, which works fine.

Thanks for the consideration,
 - Jelle

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#47302; Package emacs. (Sun, 25 Apr 2021 15:54:02 GMT) Full text and rfc822 format available.

Message #26 received at 47302 <at> debbugs.gnu.org (full text, mbox):

From: Mattias Engdegård <mattiase <at> acm.org>
To: Dave Gillespie <Daveg <at> synaptics.com>
Cc: Jelle Licht <jlicht <at> fsfe.org>, Stefan Kangas <stefan <at> marxist.se>,
 "47302 <at> debbugs.gnu.org" <47302 <at> debbugs.gnu.org>
Subject: Re: 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 part 1 (text/plain, inline)]
22 apr. 2021 kl. 18.35 skrev Dave Gillespie <Daveg <at> synaptics.com>:

> 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.
> 
> 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?

[calc-digit-after-point.diff (application/octet-stream, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#47302; Package emacs. (Mon, 26 Apr 2021 14:59:01 GMT) Full text and rfc822 format available.

Message #29 received at 47302 <at> debbugs.gnu.org (full text, mbox):

From: Dave Gillespie <Daveg <at> synaptics.com>
To: Mattias Engdegård <mattiase <at> acm.org>
Cc: Jelle Licht <jlicht <at> fsfe.org>, Stefan Kangas <stefan <at> marxist.se>,
 "47302 <at> debbugs.gnu.org" <47302 <at> debbugs.gnu.org>
Subject: RE: bug#47302: 27.1; calc math-format-number formatting for floats
 without decimals is unusual
Date: Mon, 26 Apr 2021 14:58:12 +0000
Yes, that patch makes sense.  Thanks, and thanks for helping with this old beast!

                                                                -- Dave



-----Original Message-----
From: Mattias Engdegård <mattiase <at> acm.org> 
Sent: Sunday, April 25, 2021 8:54 AM
To: Dave Gillespie <Daveg <at> synaptics.com>
Cc: Jelle Licht <jlicht <at> fsfe.org>; Stefan Kangas <stefan <at> marxist.se>; 47302 <at> debbugs.gnu.org
Subject: Re: bug#47302: 27.1; calc math-format-number formatting for floats without decimals is unusual

CAUTION: Email originated externally, do not click links or open attachments unless you recognize the sender and know the content is safe.


22 apr. 2021 kl. 18.35 skrev Dave Gillespie <Daveg <at> synaptics.com>:

> 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.
>
> 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?





Reply sent to Mattias Engdegård <mattiase <at> acm.org>:
You have taken responsibility. (Tue, 27 Apr 2021 16:16:02 GMT) Full text and rfc822 format available.

Notification sent to Jelle Licht <jlicht <at> fsfe.org>:
bug acknowledged by developer. (Tue, 27 Apr 2021 16:16:02 GMT) Full text and rfc822 format available.

Message #34 received at 47302-done <at> debbugs.gnu.org (full text, mbox):

From: Mattias Engdegård <mattiase <at> acm.org>
To: Dave Gillespie <Daveg <at> synaptics.com>
Cc: 47302-done <at> debbugs.gnu.org, Jelle Licht <jlicht <at> fsfe.org>,
 Stefan Kangas <stefan <at> marxist.se>
Subject: Re: bug#47302: 27.1; calc math-format-number formatting for floats
 without decimals is unusual
Date: Tue, 27 Apr 2021 18:15:43 +0200
26 apr. 2021 kl. 16.58 skrev Dave Gillespie <Daveg <at> synaptics.com>:
> 
> Yes, that patch makes sense.

Good, patch pushed as-is. It can be tweaked later on if needed. Jelle, hope this helps.

>  Thanks, and thanks for helping with this old beast!

Happy to give a hand. Nowhere else in Emacs -- and I mean this in the best possible way -- do I get a feeling of Mad Science as strongly as when dealing with Calc. "Surely Calc doesn't attempt to do this... oh yes. Yes it does."





bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Wed, 26 May 2021 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 2 years and 329 days ago.

Previous Next


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