Subject: Re: Lisp Programmers in X Months
From: Erik Naggum <erik@naggum.net>
Date: Sun, 14 Apr 2002 21:53:58 GMT
Newsgroups: comp.lang.lisp
Message-ID: <3227810034793834@naggum.net>

* Christopher Browne <cbbrowne@acm.org>
| 1.52 is 10^1 * 1369094242697216 / 2^53
| 1.75 is 10^1 * 1576259842736128 / 2^53

  Huh?  1.75 is 7/4, and can be represented exactly using any base-2
  floating point.  Your value is 0.175, which cannot.  But why did you
  divide by 10 first?  This is a really, really bad way of converting
  floating-point numbers to and from internal form.  I have no idea where
  this 10^1 thing comes from, but it is usually the other way around, you
  have  175/1000, not 0.175*10.

  What I get is this (with *read-default-float-format* permanently set to
  double-float)

(integer-decode-float 1.52)
=> 6845471433603154
=> -52
=> 1

(integer-decode-float 1.75)
=> 7881299347898368
=> -52
=> 1

  Adding these together, I get 14726770781501522 / 2^52 which is
  fortunately identical to 7363385390750761 / 2^51.

| Adding them together gives you
| 
|         10^1 * 2945354085433344 / 2^53
| 
| This does happen to coincide with the FP representation of 3.27, but
| _none_ of the values were exact

  Well, at least one was.

| The exact FP value of the result, 3.27, is
| (* 10 2945354085433344 (/ 1 (expt 2 53)))
| 54861495/16777216

(integer-decode-float 3.27)
=> 7363385390750761
=> -51
=> 1

| Comparing that to the exact result:
| (- 54861495/16777216 327/100)
| -33/419430400
| 
| The result is absolutely NOT "exact"; it is off by -33/419430400.

  This is a pretty odd way to calculate things.

(rational 3.27)
=> 7363385390750761/2251799813685248
(- * 327/100)
=> 1/56294995342131200

  However, we also have

(- (rational 1.52) 152/100)
=> 1/56294995342131200

  I have no idea where you learned floating-point representation, but you
  have just introduced a computational error by working with more inexact
  values than you could have, and you have not reported your inaccuracies
  for the operands, only the result.  I wonder why you did this.  Nobody
  does normalization in the decimal realm _before_ converting to binary.

///
-- 
  In a fight against something, the fight has value, victory has none.
  In a fight for something, the fight is a loss, victory merely relief.

  Post with compassion: http://home.chello.no/~xyzzy/kitten.jpg