all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#41279: 26.3; Calc: sum(gcd(0,n),n,-1,-1) evaluates to -1
@ 2020-05-15  4:52 David Ongaro
  2020-05-15 10:19 ` Mattias Engdegård
  0 siblings, 1 reply; 4+ messages in thread
From: David Ongaro @ 2020-05-15  4:52 UTC (permalink / raw)
  To: 41279

By accident I noticed that this algebraic expression yields 6 instead of
8 which would be correct:

    sum(sum(gcd(n, k), k, -1, 1), n, -1, 1) => 6

I could narrow it down to this, which yields -1 instead of 1

    sum(gcd(0, n), n , -1, -1) => -1

Notably gcd(0, -1) evaluates to 1 as it should be. Also we have

    sum(gcd(n, 0), n, -1, -1) => -1

but

    sum(sum(gcd(n, k), k, 0, 0), n, -1, -1) => -1

    sum(sum(gcd(n, k), k, -1, -1), n, 0, 0) => 1

which violates gcd(n, k) = gcd(k, n). We also have

    sum(sum(gcd(n, k) = 1, k, -1, 1), n, -1, 1) => 7

    sum(sum(abs(gcd(n, k)), k, -1, 1), n, -1, 1) => 8

which seems to confirm that it's not just the sum function but the
combination of sum and gcd which might introduce the problem.

To reproduce you can simply enter Calc embedded mode with C-x 8 e in a
buffer with this email content, put the point on a corresponding
formular and reevaluate with SPACE.

I'd appreciate if this could be fixed.

David



In GNU Emacs 26.3 (build 1, x86_64-apple-darwin18.2.0, NS appkit-1671.20 Version 10.14.3 (Build 18D109))
of 2019-09-02 built on builder10-14.porkrind.org
Windowing system distributor 'Apple', version 10.3.1894
Recent messages:
previous-line: Beginning of buffer [2 times]
(Switching Calc Embedded mode to new formula.) [5 times]
Working... [3 times]
(Canceled)
C-c C-c is undefined [2 times]
(Switching Calc Embedded mode to new formula.) [9 times]
user-error: Beginning of history; no preceding item [3 times]
Quit
Making completion list... [2 times]
user-error: Beginning of history; no preceding item

Configured using:
'configure --with-ns '--enable-locallisppath=/Library/Application
Support/Emacs/${version}/site-lisp:/Library/Application
Support/Emacs/site-lisp' --with-modules'

Configured features:
NOTIFY ACL GNUTLS LIBXML2 ZLIB TOOLKIT_SCROLL_BARS NS MODULES THREADS

Important settings:
  value of $LC_ALL: en_US.utf-8
  value of $LC_CTYPE: UTF-8
  value of $LANG: en_US.utf-8
  locale-coding-system: utf-8-unix

Major mode: Fundamental

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  buffer-read-only: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny seq byte-opt gv
bytecomp byte-compile cconv dired dired-loaddefs format-spec rfc822 mml
mml-sec password-cache epa derived epg epg-config gnus-util rmail
rmail-loaddefs mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums
mm-util mail-prsvr mail-utils calc-arith calc-prog calc-yank calccomp
calcalg2 calc-misc calc-comb calc-map calc-alg calc-aent cl-seq
calc-menu easymenu calc-embed cl-loaddefs cl-lib calc-ext calc
calc-loaddefs calc-macs elec-pair time-date tooltip eldoc electric
uniquify ediff-hook vc-hooks lisp-float-type mwheel term/ns-win ns-win
ucs-normalize mule-util term/common-win tool-bar dnd fontset image
regexp-opt fringe tabulated-list replace newcomment text-mode elisp-mode
lisp-mode prog-mode register page menu-bar rfn-eshadow isearch timer
select scroll-bar mouse jit-lock font-lock syntax facemenu font-core
term/tty-colors frame cl-generic cham georgian utf-8-lang misc-lang
vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms cp51932
hebrew greek romanian slovak czech european ethiopic indian cyrillic
chinese composite charscript charprop case-table epa-hook jka-cmpr-hook
help simple abbrev obarray minibuffer cl-preloaded nadvice loaddefs
button faces cus-face macroexp files text-properties overlay sha1 md5
base64 format env code-pages mule custom widget hashtable-print-readable
backquote threads kqueue cocoa ns multi-tty make-network-process emacs)

Memory information:
((conses 16 243141 10243)
(symbols 48 24190 1)
(miscs 40 99 401)
(strings 32 37828 1759)
(string-bytes 1 996761)
(vectors 16 38186)
(vector-slots 8 766279 17952)
(floats 8 49 73)
(intervals 56 315 13)
(buffers 992 15))








^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#41279: 26.3; Calc: sum(gcd(0,n),n,-1,-1) evaluates to -1
  2020-05-15  4:52 bug#41279: 26.3; Calc: sum(gcd(0,n),n,-1,-1) evaluates to -1 David Ongaro
@ 2020-05-15 10:19 ` Mattias Engdegård
  2020-05-15 17:44   ` David Ongaro
  0 siblings, 1 reply; 4+ messages in thread
From: Mattias Engdegård @ 2020-05-15 10:19 UTC (permalink / raw)
  To: David Ongaro; +Cc: 41279

Thank you for reporting this. It seems that Calc first simplifies gcd(0,n) to n and then computes sum(n,n,-1,-1) = -1.
We could either make gcd(0,x)=gcd(x,0)=|x|, or try preventing sum from simplifying its operand first, or both.
For the smallest possible change, what about the hack below?

--- a/lisp/calc/calc-comb.el
+++ b/lisp/calc/calc-comb.el
@@ -241,8 +241,8 @@ calcFunc-gcd
         (calcFunc-gcd (math-neg a) b))
        ((Math-looks-negp b)
         (calcFunc-gcd a (math-neg b)))
-       ((Math-zerop a) b)
-       ((Math-zerop b) a)
+       ((Math-zerop a) (math-abs b))
+       ((Math-zerop b) (math-abs a))
        ((and (Math-ratp a)
              (Math-ratp b))
         (math-make-frac (math-gcd (if (eq (car-safe a) 'frac) (nth 1 a) a)







^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#41279: 26.3; Calc: sum(gcd(0,n),n,-1,-1) evaluates to -1
  2020-05-15 10:19 ` Mattias Engdegård
@ 2020-05-15 17:44   ` David Ongaro
  2020-05-15 18:21     ` Mattias Engdegård
  0 siblings, 1 reply; 4+ messages in thread
From: David Ongaro @ 2020-05-15 17:44 UTC (permalink / raw)
  To: Mattias Engdegård; +Cc: 41279

On 15.05.20, 03:19, "Mattias Engdegård" <mattiase@acm.org> wrote:

> Thank you for reporting this. It seems that Calc first simplifies gcd(0,n) to n and then computes sum(n,n,-1,-1) = -1.
> We could either make gcd(0,x)=gcd(x,0)=|x|, or try preventing sum from simplifying its operand first, or both.
> For the smallest possible change, what about the hack below?

I see, so the simplest reproduction of the bug doesn't involve the sum, but just the evaluation of gcd(0, x) or gcd(x, 0). Your fix seems sensible to me, I don't see why you consider it a 'hack'. The simplification gcd(0, x)=gcd(x, 0)=abs(x) seems legit and matches the gcd definition "The largest positive integer that divides each of the integers".







^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#41279: 26.3; Calc: sum(gcd(0,n),n,-1,-1) evaluates to -1
  2020-05-15 17:44   ` David Ongaro
@ 2020-05-15 18:21     ` Mattias Engdegård
  0 siblings, 0 replies; 4+ messages in thread
From: Mattias Engdegård @ 2020-05-15 18:21 UTC (permalink / raw)
  To: David Ongaro; +Cc: 41279-done

15 maj 2020 kl. 19.44 skrev David Ongaro <david.ongaro@hamburg.de>:

> I see, so the simplest reproduction of the bug doesn't involve the sum, but just the evaluation of gcd(0, x) or gcd(x, 0). Your fix seems sensible to me, I don't see why you consider it a 'hack'. The simplification gcd(0, x)=gcd(x, 0)=abs(x) seems legit and matches the gcd definition "The largest positive integer that divides each of the integers".

Quite, but there is a small possibility that further simplification relies on the gcd(0,x)=x behaviour. You never know with Calc: it is complex and everything seems interconnected. After all, abs is often an obstacle in algebraic transforms; you often end up splitting cases.

That said, it's probably the right thing to do. Now pushed to master, which means that you need to build a development snapshot from source, or apply the change (60cd6cce55) to your Emacs.






^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-05-15 18:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-05-15  4:52 bug#41279: 26.3; Calc: sum(gcd(0,n),n,-1,-1) evaluates to -1 David Ongaro
2020-05-15 10:19 ` Mattias Engdegård
2020-05-15 17:44   ` David Ongaro
2020-05-15 18:21     ` Mattias Engdegård

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.