[plt-scheme] question about Scheme numbers

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Sat May 13 04:40:14 EDT 2006

Danny Yoo wrote:
 > Peter wrote:

>>> (square (sqrt 3))
>>
>> 2.9999999999999996
>>
>> How come?
>
>     http://htdp.org/2003-09-26/Book/curriculum-Z-H-41.html#node_chap_33

The short version:

  (sqrt 3) can not be represented exactly using floating point numbers,
  so instead the result of (sqrt 3) is the closest representable number.
  Squaring a number close to (sqrt 3) results in a number close to 3
  namely 2.9999999999999996.
  The same thing happens internally on a calculator. Calculators cheat
  though, so it is hard to notice. Internally they use, say, 2 digits more
  than they display. That is, the above result would be displayed
  as 3.00000000000000. Try (sqrt(3.0))^2*10000 on a calculator to see the
  same phenomenon.

If you are interested in floating point, then besides the chapter Danny
links to, read Goldberg's "What Every Computer Scientist Should Know About
Floating-Point Arithmetic".

  <http://docs.sun.com/source/806-3568/ncg_goldberg.html>

-- 
Jens Axel Søgaard
<http://www.scheme.dk/blog/>




Posted on the users mailing list.