On Fri, Aug 25, 2017 at 2:37 PM, Rustom Mody <rustompm...@gmail.com> wrote:
> On Friday, August 25, 2017 at 9:58:15 AM UTC+5:30, Chris Angelico wrote:
>> On Fri, Aug 25, 2017 at 1:23 PM, Rustom Mody wrote:
>> > Early in my python classes I show this:
>> >
>> > $ python
>> > Python 2.7.13 (default, Jan 19 2017, 14:48:08)
>> > [GCC 6.3.0 20170118] on linux2
>> > Type "help", "copyright", "credits" or "license" for more information.
>> >>>> .1 + .1 == .2
>> > True
>> >>>> .1 + .1 + .1 == .3
>> > False
>> >>>>
>> > --
>> > https://mail.python.org/mailman/listinfo/python-list
>>
>> Aside from the fact that I show it with Python 3 (or sometimes
>> Node.js) instead, I've demonstrated something similar - but then go on
>> to make the point that 0.1 is not "one tenth". The problem isn't with
>> addition, which is completely correct here; the problem is with the
>> assumption that writing "0.1" in your source code indicates the number
>> "one tenth". If you want the details, Python is very helpful here:
>>
>> >>> "%d/%d" % (0.1).as_integer_ratio()
>> '3602879701896397/36028797018963968'
>> >>> "%d/%d" % (0.2).as_integer_ratio()
>> '3602879701896397/18014398509481984'
>> >>> "%d/%d" % (0.3).as_integer_ratio()
>> '5404319552844595/18014398509481984'
>>
>> Or in hex:
>>
>> >>> "%x/%x" % (0.1).as_integer_ratio()
>> 'ccccccccccccd/80000000000000'
>> >>> "%x/%x" % (0.2).as_integer_ratio()
>> 'ccccccccccccd/40000000000000'
>> >>> "%x/%x" % (0.3).as_integer_ratio()
>> '13333333333333/40000000000000'
>>
>
> Thanks for the hex tip… Useful!

Yes, that makes it really obvious that (a) the denominator is a power
of two, (b) the numerator is repeating, and (c) one of them got
rounded down and the other two got rounded up. The decimal values are
almost completely useless, except for the fairly significant fact that
most people aren't used to writing numbers like
"ccccccccccccd/80000000000000" !!!

>> Clearly the second one is exactly double the first. And equally
>> clearly, the first two have been rounded up, while the second is
>> rounded down. But you don't need that much detail to understand what's
>> going on; most people can follow this analogy:
>>
>> 0.6667 + 0.6667 + 0.6667 != 2.000
>>
>> since, in grade school, most of us learned that the decimal expansion
>> for two-thirds gets rounded up. It's the same thing, just in binary.
>>
>> 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.
>
> You almost make that sound like a rare exception <wink>

Heh, worded that way it does sound rare. But people are at least
familiar with one third, one seventh, one ninth, and maybe a few
others, so it's not surprising to them when numbers get rounded.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to