Reading the documentation

Steve D'Aprano steve+python at pearwood.info
Fri Aug 25 01:08:04 EDT 2017


On Fri, 25 Aug 2017 02:21 pm, Chris Angelico wrote:

> In fact, the ONLY way to create this confusion is to use (some
> derivative of) one fifth, which is a factor of base 10 but not of base
> 2. Any other fraction will either terminate in both bases (eg "0.125"
> in decimal or "0.001" in binary), or repeat in both (any denominator
> with any other prime number in it). No other rational numbers can
> produce this apparently-irrational behaviour, pun intended.


I think that's a bit strong. A lot strong. Let's take 1/13 for example:


py> from decimal import Decimal
py> sum([1/13]*13)
0.9999999999999998
py> sum([Decimal(1)/Decimal(13)]*13)
Decimal('0.9999999999999999999999999997')


Or 2/7, added 7 times, should be 2:

py> sum([2/7]*7)
1.9999999999999996
py> sum([Decimal(2)/Decimal(7)]*7)
Decimal('2.000000000000000000000000001')


Its not just addition that "fails". We can take the reciprocal of the reciprocal
of a number, and expect to get the original number:

py> 1/(1/99)
98.99999999999999
py> Decimal(1)/( Decimal(1)/Decimal(99) )
Decimal('99.00000000000000000000000001')


or we can multiply a fraction by the denominator and expect to get the
numerator:

py> (7/207)*207
6.999999999999999
py> (Decimal(7)/Decimal(207))*Decimal(207)
Decimal('6.999999999999999999999999999')


These are not isolated examples.


You'll note that none of the numbers involved are multiples of 5.



Besides: the derivative of 1/5 is 0, like that of every other constant.

*wink*

-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list