[plt-scheme] flonums question

From: Jos Koot (jos.koot at telefonica.net)
Date: Sun Jan 17 14:27:24 EST 2010

David,
Thanks again, after reading your answer more carefully it is perfectly clear 
now.
Jos

----- Original Message ----- 
From: "David Brown" <plt at davidb.org>
To: "Jos Koot" <jos.koot at telefonica.net>
Cc: <plt-scheme at list.cs.brown.edu>
Sent: Saturday, January 16, 2010 11:03 PM
Subject: Re: [plt-scheme] flonums question


> On Sat, Jan 16, 2010 at 09:50:15PM +0100, Jos Koot wrote:
>>#lang scheme
>>
>>; I do not understand the difference between the following two:
>>
>>(let loop ((x 0))
>> (let ((d (- 1.0 (+ 1.0 (expt 2 x)))))
>>  (if (= d 0) x
>>   ; d is an inexact real number.
>>   ; well, all reals are inexact I think.
>>   ; an exact real would have to be an exact ranional, is it not?
>>   (loop (sub1 x))))) ; --> 54
>>; flonum arithmetic I think.
>>
>>(let loop ((x 0))
>> (let ((d (exact->inexact (- 1 (+ 1 (expt 2 x))))))
>>  ; d is an inexact rational number.
>>  (if (= d 0) x
>>   (loop (sub1 x))))) ; --> -1075
>>; where does this high though finite precision come from?
>
> In the first case, you're computing the number of bits of magnitude in
> a flonum.  In the second case, you're computing the closest you can
> get to zero (basic you're determining the number of bits in the
> exponent).
>
> The first case reaches zero when the exponent has enough bits that
> lowest bit in it's representation is larger than '1'.  The second case
> reaches zero when the exponent is too large, and the number is
> truncated to zero.  The first case, the error happens at the subtract,
> and in the second case, it happens when that result is turned into a
> flonum.
>
> Different scheme implementation seem to give either -1075 or -1024,
> which suggest different handling of denormal numbers.  (Chicken
> returns -53, since it doesn't have exact numbers larger than a
> fixnum).
>
> It helps to print out 'd' before the if in each of the cases.
>
> David


--------------------------------------------------------------------------------



Se certifico que el correo entrante no contiene virus.
Comprobada por AVG - www.avg.es
Version: 9.0.725 / Base de datos de virus: 270.14.145/2626 - Fecha de la 
version: 01/16/10 08:35:00



Posted on the users mailing list.