GNU bug report logs - #41347
28.0.50; calculator.el: Cannot input negative exponents

Previous Next

Package: emacs;

Reported by: Chris Zheng <chriszheng99 <at> gmail.com>

Date: Sun, 17 May 2020 05:54:02 UTC

Severity: normal

Found in version 28.0.50

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 41347 in the body.
You can then email your comments to 41347 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#41347; Package emacs. (Sun, 17 May 2020 05:54:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Chris Zheng <chriszheng99 <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 17 May 2020 05:54:02 GMT) Full text and rfc822 format available.

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

From: Chris Zheng <chriszheng99 <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 28.0.50; calculator.el: Cannot input negative exponents
Date: Sun, 17 May 2020 13:53:12 +0800
Hello,

With current master, starting Emacs with `-Q`:

1. M-x calculator
2. Input 1e-3 RET

You will get 1 instead of 0.001. I believe this happens since Emacs 26.1. The root cause is the `calculator-string-to-number` function in lisp/calculator.el. Now the function gives

(calculator-string-to-number "1e-3") => 1.0
(calculator-string-to-number "1e3") => 1000.0
(calculator-string-to-number "1e") => 1.0

The funcional code is 

(replace-regexp-in-string
                 "[eE][+-]?\\([^0-9].*\\)?$" "e0\\1" str)

It changes `1e-3` to `1e0-3` that is recognized as 1. I have a possible fix attached below. Please see if it is correct. 

Thank you,

Chris Zheng



From 7693d7072e4787c4b0663f490be5d83c1d9a6ee3 Mon Sep 17 00:00:00 2001
From: Chris Zheng <chriszheng99 <at> gmail.com>
Date: Sun, 17 May 2020 13:43:35 +0800
Subject: [PATCH 1/1] Fix calculator exponent input

* lisp/calculator.el (calculator-string-to-number): Change the regexp
to correctly deal with negative exponents.
---
 lisp/calculator.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lisp/calculator.el b/lisp/calculator.el
index 7e0b2fcc6a..fa3eb19099 100644
--- a/lisp/calculator.el
+++ b/lisp/calculator.el
@@ -863,7 +863,7 @@ calculator-string-to-number
     (let* ((str (replace-regexp-in-string
                  "\\.\\([^0-9].*\\)?$" ".0\\1" str))
            (str (replace-regexp-in-string
-                 "[eE][+-]?\\([^0-9].*\\)?$" "e0\\1" str)))
+                 "[eE]\\([+-]?\\)?$" "e\\10" str)))
       (float (string-to-number str)))))
 
 (defun calculator-push-curnum ()
-- 
2.16.1.windows.1





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41347; Package emacs. (Sun, 17 May 2020 11:09:02 GMT) Full text and rfc822 format available.

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

From: Mattias Engdegård <mattiase <at> acm.org>
To: Chris Zheng <chriszheng99 <at> gmail.com>
Cc: 41347 <at> debbugs.gnu.org, Eli Barzilay <eli <at> barzilay.org>
Subject: bug#41347: 28.0.50; calculator.el: Cannot input negative exponents 
Date: Sun, 17 May 2020 13:08:30 +0200
> @@ -863,7 +863,7 @@ calculator-string-to-number
>      (let* ((str (replace-regexp-in-string
>                   "\\.\\([^0-9].*\\)?$" ".0\\1" str))
>             (str (replace-regexp-in-string
> -                 "[eE][+-]?\\([^0-9].*\\)?$" "e0\\1" str)))
> +                 "[eE]\\([+-]?\\)?$" "e\\10" str)))
>        (float (string-to-number str)))))

Thanks for the report and the suggested patch! However, I'm not sure what either of these replace-regexp-in-string calls are good for. The first one possibly to accept 1.e23 instead of 1e23; the second one is less clear. Frankly, I think we can drop both.

Eli, do you remember?





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41347; Package emacs. (Sun, 17 May 2020 11:58:02 GMT) Full text and rfc822 format available.

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

From: Andreas Schwab <schwab <at> linux-m68k.org>
To: Mattias Engdegård <mattiase <at> acm.org>
Cc: 41347 <at> debbugs.gnu.org, Chris Zheng <chriszheng99 <at> gmail.com>,
 Eli Barzilay <eli <at> barzilay.org>
Subject: Re: bug#41347: 28.0.50; calculator.el: Cannot input negative exponents
Date: Sun, 17 May 2020 13:57:50 +0200
On Mai 17 2020, Mattias Engdegård wrote:

>> @@ -863,7 +863,7 @@ calculator-string-to-number
>>      (let* ((str (replace-regexp-in-string
>>                   "\\.\\([^0-9].*\\)?$" ".0\\1" str))
>>             (str (replace-regexp-in-string
>> -                 "[eE][+-]?\\([^0-9].*\\)?$" "e0\\1" str)))
>> +                 "[eE]\\([+-]?\\)?$" "e\\10" str)))
>>        (float (string-to-number str)))))
>
> Thanks for the report and the suggested patch! However, I'm not sure what either of these replace-regexp-in-string calls are good for. The first one possibly to accept 1.e23 instead of 1e23; the second one is less clear. Frankly, I think we can drop both.

In commit f248292ede, there was

-                ((string-match-p "[eE][+-]?$" str) (concat str "0"))

so the bug is that the part matching "[+-]?" is now dropped.

Andreas.

-- 
Andreas Schwab, schwab <at> linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41347; Package emacs. (Sun, 17 May 2020 12:19:02 GMT) Full text and rfc822 format available.

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

From: Mattias Engdegård <mattiase <at> acm.org>
To: Andreas Schwab <schwab <at> linux-m68k.org>
Cc: 41347 <at> debbugs.gnu.org, Chris Zheng <chriszheng99 <at> gmail.com>,
 Eli Barzilay <eli <at> barzilay.org>
Subject: Re: bug#41347: 28.0.50; calculator.el: Cannot input negative exponents
Date: Sun, 17 May 2020 14:18:24 +0200
17 maj 2020 kl. 13.57 skrev Andreas Schwab <schwab <at> linux-m68k.org>:

> In commit f248292ede, there was
> 
> -                ((string-match-p "[eE][+-]?$" str) (concat str "0"))
> 
> so the bug is that the part matching "[+-]?" is now dropped.

In the commit you refer to, read-from-string was used; a partial input like "1e" had to be completed with an extra "0" to make the result a (floating-point) number. This no longer appears necessary since we use string-to-number and convert integers to float.





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41347; Package emacs. (Sun, 17 May 2020 20:27:02 GMT) Full text and rfc822 format available.

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

From: Eli Barzilay <eli <at> barzilay.org>
To: Mattias Engdegård <mattiase <at> acm.org>
Cc: 41347 <at> debbugs.gnu.org, Chris Zheng <chriszheng99 <at> gmail.com>
Subject: Re: bug#41347: 28.0.50; calculator.el: Cannot input negative exponents
Date: Sun, 17 May 2020 16:26:39 -0400
On Sun, May 17, 2020 at 7:08 AM Mattias Engdegård <mattiase <at> acm.org> wrote:
>
> > @@ -863,7 +863,7 @@ calculator-string-to-number
> >      (let* ((str (replace-regexp-in-string
> >                   "\\.\\([^0-9].*\\)?$" ".0\\1" str))
> >             (str (replace-regexp-in-string
> > -                 "[eE][+-]?\\([^0-9].*\\)?$" "e0\\1" str)))
> > +                 "[eE]\\([+-]?\\)?$" "e\\10" str)))
> >        (float (string-to-number str)))))
>
> Thanks for the report and the suggested patch! However, I'm not sure
> what either of these replace-regexp-in-string calls are good for. The
> first one possibly to accept 1.e23 instead of 1e23; the second one is
> less clear. Frankly, I think we can drop both.
>
> Eli, do you remember?

Sidenote: there's not much point in the double "?" in "\\([+-]?\\)?".

But more to the point, I don't remember why I switched to the regexp
mess in the first place.  The original code:

    (car (read-from-string
          (cond ((equal "." str) "0.0")
                ((string-match "[eE][+-]?$" str) (concat str "0"))
                ((string-match "\\.[0-9]\\|[eE]" str) str)
                ((string-match "\\." str)
                 ;; do this because Emacs reads "23." as an integer
                 (concat str "0"))
                ((stringp str) (concat str ".0"))
                (t "0.0"))))

makes the intention clear -- the idea is to mimic common calculators
where you can type "3." or "3e" and get 3.  (Re the fix from a short
while ago, the comment also shows that the original intention was to
always get a float.)

It looks like going back to a simplified (due to the float) version of
this would be better.  Also testing that the thing I mentioned in the
log still works ("e+" using "+" as an operator).

-- 
                   ((x=>x(x))(x=>x(x)))                  Eli Barzilay:
                   http://barzilay.org/                  Maze is Life!




Reply sent to Mattias Engdegård <mattiase <at> acm.org>:
You have taken responsibility. (Mon, 18 May 2020 09:29:02 GMT) Full text and rfc822 format available.

Notification sent to Chris Zheng <chriszheng99 <at> gmail.com>:
bug acknowledged by developer. (Mon, 18 May 2020 09:29:02 GMT) Full text and rfc822 format available.

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

From: Mattias Engdegård <mattiase <at> acm.org>
To: Eli Barzilay <eli <at> barzilay.org>
Cc: 41347-done <at> debbugs.gnu.org, Chris Zheng <chriszheng99 <at> gmail.com>
Subject: Re: bug#41347: 28.0.50; calculator.el: Cannot input negative
 exponents 
Date: Mon, 18 May 2020 11:28:21 +0200
[Message part 1 (text/plain, inline)]
17 maj 2020 kl. 22.26 skrev Eli Barzilay <eli <at> barzilay.org>:

> the idea is to mimic common calculators
> where you can type "3." or "3e" and get 3.

Thank you Eli! I can confirm that after removing all the string transformation prior to the call to string-to-number, everything works as expected except "1.e3" (dot before E). A single transformation taking care of that case was added for the sake of completeness.

The attached patch has now been pushed to master.

[0001-Fix-calculator-entry-of-numbers-with-negative-expone.patch (application/octet-stream, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41347; Package emacs. (Mon, 18 May 2020 15:02:02 GMT) Full text and rfc822 format available.

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

From: Chris Zheng <chriszheng99 <at> gmail.com>
To: Mattias Engdegård <mattiase <at> acm.org>
Cc: 41347-done <at> debbugs.gnu.org
Subject: Re: bug#41347: 28.0.50; calculator.el: Cannot input negative exponents
Date: Mon, 18 May 2020 23:01:04 +0800
On Mon, May 18, 2020 at 11:28:21AM +0200, Mattias Engdegård wrote:
>17 maj 2020 kl. 22.26 skrev Eli Barzilay <eli <at> barzilay.org>:
>
>> the idea is to mimic common calculators
>> where you can type "3." or "3e" and get 3.
>
>Thank you Eli! I can confirm that after removing all the string transformation prior to the call to string-to-number, everything works as expected except "1.e3" (dot before E). A single transformation taking care of that case was added for the sake of completeness.
>
>The attached patch has now been pushed to master.

Hello Mattias,

Thank you for fixing it. Still I think it is uncommon to use `rx` here...

Best regards,

Chris





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41347; Package emacs. (Mon, 18 May 2020 15:12:02 GMT) Full text and rfc822 format available.

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

From: Mattias Engdegård <mattiase <at> acm.org>
To: Chris Zheng <chriszheng99 <at> gmail.com>
Cc: 41347 <at> debbugs.gnu.org
Subject: Re: bug#41347: 28.0.50; calculator.el: Cannot input negative exponents
Date: Mon, 18 May 2020 17:11:03 +0200
18 maj 2020 kl. 17.01 skrev Chris Zheng <chriszheng99 <at> gmail.com>:

> Thank you for fixing it. Still I think it is uncommon to use `rx` here...

Yes, in this case rx doesn't improve the readability much. It doesn't hurt, though, does it?
It's just become a habit of mine to use rx everywhere. Try it you too!





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#41347; Package emacs. (Mon, 18 May 2020 19:20:02 GMT) Full text and rfc822 format available.

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

From: Eli Barzilay <eli <at> barzilay.org>
To: Mattias Engdegård <mattiase <at> acm.org>
Cc: 41347-done <at> debbugs.gnu.org, Chris Zheng <chriszheng99 <at> gmail.com>
Subject: Re: bug#41347: 28.0.50; calculator.el: Cannot input negative exponents
Date: Mon, 18 May 2020 15:19:11 -0400
Thanks!  -- I'll try to get to testing it soon-ish...

On Mon, May 18, 2020 at 5:28 AM Mattias Engdegård <mattiase <at> acm.org> wrote:
>
> 17 maj 2020 kl. 22.26 skrev Eli Barzilay <eli <at> barzilay.org>:
>
> > the idea is to mimic common calculators
> > where you can type "3." or "3e" and get 3.
>
> Thank you Eli! I can confirm that after removing all the string transformation prior to the call to string-to-number, everything works as expected except "1.e3" (dot before E). A single transformation taking care of that case was added for the sake of completeness.
>
> The attached patch has now been pushed to master.
>


-- 
                   ((x=>x(x))(x=>x(x)))                  Eli Barzilay:
                   http://barzilay.org/                  Maze is Life!




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

This bug report was last modified 5 years and 4 days ago.

Previous Next


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