Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?

Terry Reedy tjreedy at udel.edu
Mon Oct 8 21:45:16 EDT 2012


On 10/8/2012 11:13 AM, Dave Angel wrote:

>> Isn't it true, though, that Python 3.3 has a completely new
>> implementation of decimal that largely removes this disadvantage?

> I wouldn't know, I'm on 3.2.  However, I sincerely doubt if it's within
> a factor of 100 of the speed of the binary float, at least on

 >>> import timeit as tt
 >>> tt.repeat("float('1.0')-float('0.9999999999')")
[0.6856039948871151, 0.669049830953858, 0.668688006423692]
 >>> tt.repeat("Decimal('1.0')-Decimal('0.9999999999')", "from decimal 
import Decimal")
[1.3204655578092428, 1.286977575486688, 1.2893188292009938]

 >>> tt.repeat("a-b", "a = 1.0; b=0.9999999999")
[0.06100386171601713, 0.044538539999592786, 0.04451548406098027]
 >>> tt.repeat("a-b", "from decimal import Decimal as D; a = D('1.0'); b 
= D('0.9999999999')")
[0.14685526219517442, 0.12909696344064514, 0.12646059371189722]

A factor of 3, as S. Krah, the cdecimal author, claimed
-- 
Terry Jan Reedy




More information about the Python-list mailing list