GNU bug report logs - #19025
24.4; precision error when adding two floats

Previous Next

Package: emacs;

Reported by: "Kevin W. Currey" <kevalier68 <at> hotmail.com>

Date: Wed, 12 Nov 2014 01:25:02 UTC

Severity: normal

Tags: notabug

Found in version 24.4

Done: Stefan Monnier <monnier <at> iro.umontreal.ca>

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 19025 in the body.
You can then email your comments to 19025 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#19025; Package emacs. (Wed, 12 Nov 2014 01:25:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to "Kevin W. Currey" <kevalier68 <at> hotmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 12 Nov 2014 01:25:02 GMT) Full text and rfc822 format available.

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

From: "Kevin W. Currey" <kevalier68 <at> hotmail.com>
To: "bug-gnu-emacs <at> gnu.org" <bug-gnu-emacs <at> gnu.org>
Subject: 24.4; precision error when adding two floats
Date: Tue, 11 Nov 2014 19:36:39 -0500
[Message part 1 (text/plain, inline)]
(message "%s" (+ 379.744 57.93))

The above prints 437.67400000000004.  Same if I use 57.930, but if I use 57.931 (below) I get the correct answer 437.675.
 
(message "%s" (+ 379.744 57.931))

I was able to work around by using format:
 
(message "%s" (format "%f" (+ 379.744 57.93)))

which prints 437.674000.
 
FYI, I can replicate the issue with Emacs 21.3.1.
 
Thanks,
-Kevin
 
 
In GNU Emacs 24.4.1 (x86_64-unknown-cygwin)
 of 2014-10-21 on desktop-new
Windowing system distributor `Microsoft Corp.', version 6.1.7601
Configured using:
 `configure
 --srcdir=/home/kbrown/src/cygemacs/emacs-24.4-1.x86_64/src/emacs-24.4
 --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin
 --libexecdir=/usr/libexec --datadir=/usr/share --localstatedir=/var
 --sysconfdir=/etc --libdir=/usr/lib --datarootdir=/usr/share
 --docdir=/usr/share/doc/emacs --htmldir=/usr/share/doc/emacs/html -C
 --with-w32 --without-file-notification --enable-checking 'CFLAGS=-ggdb
 -O2 -pipe -Wimplicit-function-declaration -Og -g3
 -fdebug-prefix-map=/home/kbrown/src/cygemacs/emacs-24.4-1.x86_64/build=/usr/src/debug/emacs-24.4-1
 -fdebug-prefix-map=/home/kbrown/src/cygemacs/emacs-24.4-1.x86_64/src/emacs-24.4=/usr/src/debug/emacs-24.4-1'
 CPPFLAGS= LDFLAGS='
 
 
Important settings:
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix
 
 
Major mode: Fundamental
 
 
Minor modes in effect:
  shell-dirtrack-mode: t
  display-time-mode: t
  mouse-wheel-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  line-number-mode: t

Recent messages:
Load-path shadows:
None found.
 
 
Features:
(pp shadow sort gnus-util mail-extr emacsbug message format-spec rfc822
mml mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231 mailabbrev
gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums mm-util
mail-prsvr mail-utils info find-func grep tabify sh-script smie
executable shell pcomplete compile comint ansi-color ring dabbrev
cc-langs cc-mode cc-fonts cc-guess cc-menus cc-cmds cc-styles cc-align
cc-engine cc-vars cc-defs arc-mode archive-mode dired-aux ispell apropos
dired help-fns help-mode easymenu mule-util server edmacro kmacro
cl-loaddefs cl-lib time jka-compr time-date tooltip electric uniquify
ediff-hook vc-hooks lisp-float-type mwheel w32-common-fns disp-table
w32-win w32-vars tool-bar dnd fontset image regexp-opt fringe
tabulated-list newcomment lisp-mode prog-mode register page menu-bar
rfn-eshadow timer select scroll-bar mouse jit-lock font-lock syntax
facemenu font-core frame cham georgian utf-8-lang misc-lang vietnamese
tibetan thai tai-viet lao korean japanese hebrew greek romanian slovak
czech european ethiopic indian cyrillic chinese case-table epa-hook
jka-cmpr-hook help simple abbrev minibuffer 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 make-network-process dbusbind w32 multi-tty emacs)
 
 
Memory information:
((conses 16 145217 10616)
 (symbols 48 23227 0)
 (miscs 40 133 446)
 (strings 32 31064 5702)
 (string-bytes 1 930985)
 (vectors 16 15771)
 (vector-slots 8 452262 7494)
 (floats 8 82 775)
 (intervals 56 640 31)
 (buffers 960 24))

 		 	   		  
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19025; Package emacs. (Wed, 12 Nov 2014 01:46:02 GMT) Full text and rfc822 format available.

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

From: Alexis <flexibeast <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: Re: bug#19025: 24.4; precision error when adding two floats
Date: Wed, 12 Nov 2014 12:38:12 +1100
Kevin W. Currey writes:

> (message "%s" (+ 379.744 57.93))
>
> The above prints 437.67400000000004.  Same if I use 57.930, but if I use 57.931 (below) I get the correct answer 437.675.
>  
> (message "%s" (+ 379.744 57.931))
>
> I was able to work around by using format:
>  
> (message "%s" (format "%f" (+ 379.744 57.93)))
>
> which prints 437.674000.
>  
> FYI, I can replicate the issue with Emacs 21.3.1.

Isn't this an artifact of the nature of floating-point arithmetic, rather than
an issue with Emacs per se? For example, in Python:

    Python 2.7.3 (default, Mar 13 2014, 11:03:55) 
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 379.744 + 57.93
    437.67400000000004
    >>>


Alexis.




Added tag(s) notabug. Request was from Stefan Monnier <monnier <at> iro.umontreal.ca> to control <at> debbugs.gnu.org. (Wed, 12 Nov 2014 02:07:01 GMT) Full text and rfc822 format available.

Reply sent to Stefan Monnier <monnier <at> iro.umontreal.ca>:
You have taken responsibility. (Wed, 12 Nov 2014 02:07:02 GMT) Full text and rfc822 format available.

Notification sent to "Kevin W. Currey" <kevalier68 <at> hotmail.com>:
bug acknowledged by developer. (Wed, 12 Nov 2014 02:07:03 GMT) Full text and rfc822 format available.

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

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: "Kevin W. Currey" <kevalier68 <at> hotmail.com>
Cc: 19025-done <at> debbugs.gnu.org
Subject: Re: bug#19025: 24.4; precision error when adding two floats
Date: Tue, 11 Nov 2014 21:06:35 -0500
tags 19025 notabug
thanks

> (message "%s" (+ 379.744 57.93))
> The above prints 437.67400000000004.

That's the difference between floating-point numbers and real or
rational numbers.

> (message "%s" (format "%f" (+ 379.744 57.93)))

You can cut the middle man: (message "%f" (+ 379.744 57.93))

But note that this prints something "incorrect":

     M-: (message "%f" (+ 379.744 57.93))
     => 437.674000.
     M-: (equal (+ 379.744 57.93) 437.674000)
     => nil

It's just that %f rounds differently.



        Stefan




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19025; Package emacs. (Wed, 12 Nov 2014 02:29:01 GMT) Full text and rfc822 format available.

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

From: Jay Belanger <jay.p.belanger <at> gmail.com>
To: 19025 <at> debbugs.gnu.org
Cc: jay.p.belanger <at> gmail.com
Subject: Re: bug#19025: 24.4; precision error when adding two floats
Date: Tue, 11 Nov 2014 20:28:49 -0600
>> (message "%s" (+ 379.744 57.93))
>>
>> The above prints 437.67400000000004.  Same if I use 57.930, but if I
>> use 57.931 (below) I get the correct answer 437.675.
>>  
>> (message "%s" (+ 379.744 57.931))
>>
>> I was able to work around by using format:
>>  
>> (message "%s" (format "%f" (+ 379.744 57.93)))
>>
>> which prints 437.674000.
>>  
>> FYI, I can replicate the issue with Emacs 21.3.1.
>
> Isn't this an artifact of the nature of floating-point arithmetic, rather than
> an issue with Emacs per se?

Yes, although it probably wouldn't be a bad idea to point out this issue
in the Elisp documentation.





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19025; Package emacs. (Wed, 12 Nov 2014 02:50:02 GMT) Full text and rfc822 format available.

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

From: Alexis <flexibeast <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: Re: bug#19025: 24.4; precision error when adding two floats
Date: Wed, 12 Nov 2014 13:42:48 +1100
Jay Belanger writes:

>> Isn't this an artifact of the nature of floating-point arithmetic,
>> rather than an issue with Emacs per se?
>
> Yes, although it probably wouldn't be a bad idea to point out this
> issue in the Elisp documentation.

*nod* And perhaps mention Calc functions, which can work with a
specified precision?

(Assuming we can't simply link to e.g. http://floating-point-gui.de/ :-) )


Alexis.




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

This bug report was last modified 10 years and 196 days ago.

Previous Next


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