*BSD News Article 54010


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!simtel!chi-news.cic.net!usc!math.ohio-state.edu!jussieu.fr!univ-lille1.fr!lefranc
From: lefranc@lsh01.univ-lille1.fr (Marc Lefranc)
Newsgroups: comp.unix.bsd.freebsd.misc,gnu.emacs.help
Subject: Re: Floating point arithmetic in ELisp.
Followup-To: comp.unix.bsd.freebsd.misc,gnu.emacs.help
Date: 5 Nov 1995 01:04:54 GMT
Organization: Universite des Sciences et Technologie de LILLE, France
Lines: 59
Message-ID: <47h2jm$po3@netserver.univ-lille1.fr>
References: <47ae1c$68e@blackice.winternet.com> <m2ybtxl4ip.fsf@diana.miranova.com>
Reply-To: lefranc@lsh.univ-lille1.fr
NNTP-Posting-Host: lsh01.univ-lille1.fr
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.freebsd.misc:8195 gnu.emacs.help:29592

In <m2ybtxl4ip.fsf@diana.miranova.com> [gnu.emacs.help], Steven L. Baur (steve@diana.miranova.com) wrote:
>>>>>> "Gareth" == Gareth Rees <gdr11@cl.cam.ac.uk> writes:
>> John D. Boggs <jdb@robigo.winternet.com> wrote:
>    >> I am teaching myself LISP using the lisp interaction mode in
>    >> Emacs, and it doesn't seem to like floating point arithmetic.
>    >> For instance, the expression (+ 3.14 2.71) returns
>    >> 5.8499999999999996.  I was curious, is this typical of Emacs
>    >> 19.29, or is it a result of running under
>    >> FreeBSD-2.0.5-950622-SNAP?  I don't foresee needing floating
>    >> point with what I have in mind, but you never know ...

>    Gareth> What happened here was that the number 5.85 is not
>    Gareth> representible in whatever floating-point format Emacs
>    Gareth> uses.

>It is Emacs/system dependent.

>(On a Linux 1.2/a.out system -- as typed in a Lisp Interaction buffer)

>emacs-version
>"19.13 XEmacs Lucid"
>(+ 3.14 2.71)
>5.85


>emacs-version
>"19.28.1"
>(+ 3.14 2.71)
>5.8499999999999996


This just means that Xemacs does some rounding. There is NOTHING wrong
with Emacs returning 5.8499999999999996. It just has to do with the
fact that 3.14 and 2.71 cannot be represented exactly by base-2
floating point numbers, plus the rounding error when adding the two
numbers.

int main() {
    double x = 3.14;
    double y = 2.71;
    printf("%.16f\n",x);
    printf("%.16f\n",y);
    printf("%.16f\n",(x+y));
    printf("%.15f\n",(x+y));    
}

3.1400000000000001
2.7100000000000000
5.8499999999999996
5.850000000000000

--
________________________________________________________________________
| Marc Lefranc, Laboratoire de Spectroscopie Hertzienne (URA CNRS 249) |
| Bat P5, UFR de Physique                                              |
| Universite des Sciences et Technologies de Lille                     |
| F-59655 Villeneuve d'Ascq CEDEX (FRANCE)                             |
| e-mail: lefranc@lsh.univ-lille1.fr ; FAX : (+33) 20 33 70 20         |
|______________________________________________________________________|