[Python-ideas] Python Numbers as Human Concept Decimal System

Terry Reedy tjreedy at udel.edu
Sun Mar 9 00:16:52 CET 2014


On 3/7/2014 5:12 AM, Steven D'Aprano wrote:

> For example, between 1 and 100000, about 12% of integer-valued floats
> fail the "1/(1/n) == n" test, but over 51% of the integer-valued
> Decimals:
>
> py> from decimal import Decimal as D
> py> sum(1/(1/n) != n for n in range(1, 100001))
> 11846
> py> sum(1/(1/D(n)) != n for n in range(1, 100001))
> 51665
>
>
> Likewise we can test how many fail the "(1/n)*n == 1" test:
>
> py> sum((1/n)*n != 1 for n in range(1, 100001))
> 13117
> py> sum((1/D(n))*n != 1 for n in range(1, 100001))
> 36806
>
> 13% for floats versus 36% for Decimals.
>
>
> One more to prove it isn't a fluke: the "sqrt(n)**2 == n" test:
>
> py> sum((n**0.5)**2 != n for n in range(1, 100001))
> 49544
> py> sum((n**D("0.5"))**2 != n for n in range(1, 100001))
> 71303
>
>
> That's three for three in favour of binary floats.

Thank you for these concrete examples. Yesterday I posted that binary 
floats are better because they are smoother, so that the relative 
approximation is more constant. I would not be surprised if this is 
behind the numbers above. In any case, the above convince me more 
strongly that we should stay with binary floats as default.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list