chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] arithmetic-shift bug


From: John Cowan
Subject: Re: [Chicken-users] arithmetic-shift bug
Date: Thu, 27 Mar 2008 17:43:26 -0400
User-agent: Mutt/1.5.13 (2006-08-11)

Alex Shinn scripsit:

> #;1> (arithmetic-shift 1 31)
> 2147483648.0
> #;2> (arithmetic-shift 1 32)
> 1
> #;3> (* 2 (arithmetic-shift 1 31))
> 4294967296.0

This inconsistency is a consequence of Chicken's default fixnum-flonum
architecture.  Arithmetic-shift verifies that its arguments are integral
and will fit in a full word (32 or 64 bits as the case may be), and
then uses the C << and >> operators to do a full-word shift.  However,
the second argument is effectively reduced modulo the width of a full
word, at least on x86-family processors.  The C standard permits this
behavior; it is undefined what happens when you shift a negative number
or to shift all the bits away.  In any case, if the result is too big
to fit in a fixnum, Chicken converts it to a flonum instead.

As with most numeric problems, the cure is to do (use numbers) first.

-- 
John Cowan  http://ccil.org/~cowan    address@hidden
There are books that are at once excellent and boring.  Those that at
once leap to the mind are Thoreau's Walden, Emerson's Essays, George
Eliot's Adam Bede, and Landor's Dialogues.  --Somerset Maugham




reply via email to

[Prev in Thread] Current Thread [Next in Thread]