Subject: Re: Unexpected "double" behaviour
To: Matthew Fincham <matthewf@cat.co.za>
From: Andrew Brown <atatat@atatdot.net>
List: netbsd-help
Date: 09/26/2001 12:41:46
>> bash$ bc -l
>> ibase=2
>> .0111001110110110010001011010000111001010110000001000001100010010
>> .4519999999999999999765812330743131042254390195012092590332031250
>>
>>As you can see, the result is not 0.452... There is no exact binary
>>representation for 0.452. As all floatingpoint values are stored as
>>fraction and an exponent, (double)452 has no exact binary representation
>>as well.
>
>Thank you for your response. This seems remarkable - there are values that
>connot be represented using floating point numbers, even though as decimal
>numbers they are "simple" (non-recursive, and without any fractional part
>e.g. 452). Are there any other such numbers?

1/3 is a good example.  it can't be represented accurately in binary
(.01010101010101...) or in decimal (.333333333...).  just about any
fraction you can pick that has factors in the denominator that are not
included in the you base representational system (decimal means ten,
giving us 2 and 5, where binary means two, so we have only 2) will
produce a infinitely long, repeating sequence after the...
uh... fractional delimiter usually represented as a . or a , and
commonly called a decimal point.

.2 (in decimal) is another example.  .2 is 1/5.  the 5 (from the
denominator) occurs in ten, so we can represent it finitely in
decimal.  1/5 in binary, however,  looks like this: .001100110011...

lesson concludes.  phew.  :)

-- 
|-----< "CODE WARRIOR" >-----|
codewarrior@daemon.org             * "ah!  i see you have the internet
twofsonet@graffiti.com (Andrew Brown)                that goes *ping*!"
andrew@crossbar.com       * "information is power -- share the wealth."