Reading the documentation

Chris Angelico rosuav at gmail.com
Fri Aug 25 01:22:53 EDT 2017


On Fri, Aug 25, 2017 at 3:08 PM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> 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')

Now do the same exercise with pencil and paper. What's 1/13?

Actually most people don't know the thirteenths. They might know the
sevenths, let's use them.

> 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')

Two sevenths is 0.285714285714. Add seven of those together, and
you'll get 0.999999999999. If you want to call it "2/7", you can use
fractions.Fraction, but if you treat it as a decimal fraction, it's
going to be rounded, just as it will be in a binary fraction (or a
float). That's what I mean about the confusion - that there are
numbers that you can write accurately as decimal fractions, but as
binary fractions, they repeat.

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

Now that one, I'll grant you :) I never learned calculus.

ChrisA



More information about the Python-list mailing list