Bug in floating-point addition: is anyone else seeing this?
Jerry Hill
malaclypse2 at gmail.com
Wed May 21 16:44:29 EDT 2008
On Wed, May 21, 2008 at 4:34 PM, Dave Parker
<daveparker at flamingthunder.com> wrote:
> On May 21, 12:38 pm, Mark Dickinson <dicki... at gmail.com> wrote:
>
>> >>> a+0.999 # gives expected result
>> 9999999999999998.0
>> >>> a+0.9999 # doesn't round correctly.
>>
>> 10000000000000000.0
>
> Shouldn't both of them give 9999999999999999.0?
My understand is no, not if you're using IEEE floating point.
> I wrote the same program under Flaming Thunder:
>
> Set a to 10^16-2.0.
> Writeline a+0.999.
> Writeline a+0.9999.
>
> and got:
>
> 9999999999999998.999
> 9999999999999998.9999
You can get the same results by using python's decimal module, like this:
>>> from decimal import Decimal
>>> a = Decimal("1e16")-2
>>> a
Decimal("9999999999999998")
>>> a+Decimal("0.999")
Decimal("9999999999999998.999")
>>> a+Decimal("0.9999")
Decimal("9999999999999998.9999")
>>>
--
Jerry
More information about the Python-list
mailing list