all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* (+ 5.1 0.1)
@ 2009-07-13 10:57 leonut
  2009-07-13 12:21 ` Anselm Helbig
  0 siblings, 1 reply; 4+ messages in thread
From: leonut @ 2009-07-13 10:57 UTC (permalink / raw)
  To: help-gnu-emacs

First I used "emacs --no-init-file" to start my emacs-22.3, then I
typed (+ 5.1 0.1) in *scratch* buffer. However, when I evaluated this
expression, I got 5.199999999999999.
Can anyone tell me why did this happen?
I tried (+ 5.0 0.1) and (+ 5.2 0.1) and both gave the answers I had
expected.


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

* Re: (+ 5.1 0.1)
  2009-07-13 10:57 (+ 5.1 0.1) leonut
@ 2009-07-13 12:21 ` Anselm Helbig
  2009-07-13 12:59   ` Teemu Likonen
  0 siblings, 1 reply; 4+ messages in thread
From: Anselm Helbig @ 2009-07-13 12:21 UTC (permalink / raw)
  To: help-gnu-emacs

Hi!

> First I used "emacs --no-init-file" to start my emacs-22.3, then I
> typed (+ 5.1 0.1) in *scratch* buffer. However, when I evaluated this
> expression, I got 5.199999999999999.
> Can anyone tell me why did this happen?
> I tried (+ 5.0 0.1) and (+ 5.2 0.1) and both gave the answers I had
> expected.

This is a fundamental problem with floating point numbers, something
similar happens in python:

  Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) 
  [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> 5.1 + 0.1
  5.1999999999999993

For an explanation why this is so, see e.g. this article:

  http://www.theregister.co.uk/2006/08/12/floating_point_approximation/

Or, of course, 

  http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

HTH, 

Anselm


-- 
Anselm Helbig 
mailto:anselm.helbig+news2009@googlemail.com


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

* Re: (+ 5.1 0.1)
  2009-07-13 12:21 ` Anselm Helbig
@ 2009-07-13 12:59   ` Teemu Likonen
  2009-07-13 13:31     ` leonut
  0 siblings, 1 reply; 4+ messages in thread
From: Teemu Likonen @ 2009-07-13 12:59 UTC (permalink / raw)
  To: help-gnu-emacs

On 2009-07-13 14:21 (+0200), Anselm Helbig wrote:

> This is a fundamental problem with floating point numbers, something
> similar happens in python:
>
>   Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) 
>   [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
>   Type "help", "copyright", "credits" or "license" for more information.
>   >>> 5.1 + 0.1
>   5.1999999999999993

Thanks for info. This is new to me too as I don't know much about
floating point numbers. For that particular example CLISP, a Common Lisp
implementation, gives the correct result:

    $ clisp -q -norc -x '(+ 5.1 0.1)'
    5.2

Emacs' arbitrary-precision calculator gives 5.2 too:

    (calc-eval "5.1 + 0.1")
    => "5.2"

>   http://www.theregister.co.uk/2006/08/12/floating_point_approximation/
>   http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

I'll certainly study these.


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

* Re: (+ 5.1 0.1)
  2009-07-13 12:59   ` Teemu Likonen
@ 2009-07-13 13:31     ` leonut
  0 siblings, 0 replies; 4+ messages in thread
From: leonut @ 2009-07-13 13:31 UTC (permalink / raw)
  To: help-gnu-emacs

Thanks very much.


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

end of thread, other threads:[~2009-07-13 13:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-07-13 10:57 (+ 5.1 0.1) leonut
2009-07-13 12:21 ` Anselm Helbig
2009-07-13 12:59   ` Teemu Likonen
2009-07-13 13:31     ` leonut

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.