*BSD News Article 54469


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!simtel!zombie.ncsc.mil!news.mathworks.com!tank.news.pipex.net!pipex!lyra.csx.cam.ac.uk!nntp-serv.cam.ac.uk!gdr11
From: gdr11@cl.cam.ac.uk (Gareth Rees)
Newsgroups: comp.unix.bsd.freebsd.misc,gnu.emacs.help
Subject: Re: Floating point arithmetic in ELisp.
Date: 03 Nov 1995 16:30:02 GMT
Organization: University of Cambridge, England
Lines: 31
Message-ID: <GDR11.95Nov3163002@stint.cl.cam.ac.uk>
References: <47ae1c$68e@blackice.winternet.com>
NNTP-Posting-Host: stint.cl.cam.ac.uk
In-reply-to: jdb@robigo.winternet.com's message of 2 Nov 1995 12:37:00 GMT
Xref: euryale.cc.adfa.oz.au comp.unix.bsd.freebsd.misc:8544 gnu.emacs.help:29733

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 ...

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

    5.85

in a buffer in lisp-interaction-mode, and press C-j, I get

    5.8499999999999996

Floating point numbers are typically represented as two binary integers,
the mantissa and the exponent.  The number they represent is something
like

    mantissa * 2^(exponent - k)

for some value of k.  So if there is some n in the correct range for
exponents such that m * 2^n is an integer, then m can be represented
exactly in floating point.  (Try the experiment above with 5.75 or 5.875
to check).  If not, then Emacs will use the best approximation
available.

--
Gareth Rees