Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #39453

Re: Confusing math problem

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'diff': 0.05; 'linux,': 0.05; 'correct.': 0.07; 'lines.': 0.07; 'python': 0.09; 'assumed': 0.09; 'truncated': 0.09; '2.7.3': 0.16; 'integers,': 0.16; 'subtlety': 0.16; 'wrote:': 0.17; 'examples': 0.18; 'windows': 0.19; 'sort': 0.21; '(on': 0.22; '2.x': 0.22; 'subject:problem': 0.22; 'work.': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'run': 0.28; 'cases.': 0.29; "i'm": 0.29; 'code': 0.31; 'mac': 0.32; 'print': 0.32; 'to:addr:python- list': 0.33; 'members.': 0.33; 'times.': 0.33; 'built-in': 0.35; 'pm,': 0.35; "won't": 0.35; 'there': 0.35; 'thank': 0.36; 'two': 0.37; 'uses': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'shows': 0.38; 'there,': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'notice': 0.39; 'received:192.168': 0.40; 'you.': 0.61; 'between': 0.63; 'different': 0.63; 'box,': 0.69; 'received:74.208': 0.71; 'received:74.208.4.194': 0.84; 'hand,': 0.97
Date Thu, 21 Feb 2013 15:25:23 -0500
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2
MIME-Version 1.0
To python-list@python.org
Subject Re: Confusing math problem
References <kg5sog$lfb$1@dont-email.me>
In-Reply-To <kg5sog$lfb$1@dont-email.me>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:wi8CUYMqg6z+oBn9ouvnG2FAVaov4hG9Hy5YJKzbLTe yssD1dOR2TauSgg8S+s8IyZGH7IjnFWNGs8CcyHtILwVGdOL80 /05XJ9Wieir04ORfRrjePqxPoIoxkpWP7eov/OGJghOo1nqsSM 551gGySSxlUhoJsx8g6SWmO2vhJLCovc2uTnxEh6Qs2Ch0EVyw KbnqG6a81NjgVgtev7AHz/jKUUEkriEUFOGzyDOdMHbO2zcwCT gMT3TJ5evGCpx40rkdAQvQKwaQiHJ+7ZYcfzw8DnLiFIeCFpXi bDFW1nkD6UNRI7PK6jRxyef6vUV+dAzh0hkuWPqLdS6ViQqcA= =
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2191.1361478337.2939.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361478337 news.xs4all.nl 6937 [2001:888:2000:d::a6]:41089
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39453

Show key headers only | View raw


On 02/21/2013 02:33 PM, Schizoid Man wrote:
> Hi there,
>
> I run the following code in Python 3.3.0 (on a Windows 7 machine) and
> Python 2.7.3 on a Mac and I get two different results:
>
> result1 = []
> result2 = []
> for a in range(2,101):
>     for b in range(2,101):
>         result1.append(math.pow(a,b))
>         result2.append(a**b)
> result1 = list(set(result1))
> result2 = list(set(result2))
> print (len(result1))
> print (len(result2))
>
> On the Windows box, I get 9183 for on both lines. However, on the Mac I
> get 9220 and 9183. Why this difference? Is there some sort of precision
> subtlety I'm missing between ** and math.pow()?
>
> Thank you.

I assumed this was some difference between Python 2.x and 3.x. 
However, on my 2.7.3 on Linux, I also get 9183 both times.

However, there is an important inaccuracy in math.pow, because it uses 
floats to do the work.  If you have very large integers, that means some 
of them won't be correct.  The following are some examples for 2.7.3 on 
Linux:

  a  b  math.pow(a,b)       a**b
  3 34 1.66771816997e+16 16677181699666569
  3 35 5.0031545099e+16 50031545098999707
...
  5 23 1.19209289551e+16 11920928955078125

The built-in pow, on the other hand, seems to get identical answers for 
all these cases.  So use pow() instead of math.pow()

One other test:

diff = set(map(int, result1)).symmetric_difference(set(result2))
if diff:
     print diff
     print len(diff)

shows me a diff set of 15656 members.  One such member:
 
13552527156068805425093160010874271392822265625000000000000000000000000000000000000000000000000000000000000000000L

Notice how using floats truncated lots of the digits in the value?
-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 19:33 +0000
  Re: Confusing math problem Dave Angel <davea@davea.name> - 2013-02-21 15:25 -0500
    Re: Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 22:39 +0000
      Re: Confusing math problem Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-21 22:53 +0000
        Re: Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 23:41 +0000
          Re: Confusing math problem Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-22 00:04 +0000
          Re: Confusing math problem Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-21 17:19 -0700
    Re: Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 23:39 +0000
  Re: Confusing math problem Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-21 13:42 -0700
  Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 07:46 +1100
    Re: Confusing math problem "Schizoid Man" <schiz_man@21stcentury.com> - 2013-02-21 22:44 +0000
      Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 11:29 +1100
      Re: Confusing math problem Dave Angel <davea@davea.name> - 2013-02-21 21:19 -0500
  Re: Confusing math problem Dave Angel <davea@davea.name> - 2013-02-21 15:49 -0500
  Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 08:23 +1100
    Re: Confusing math problem Peter Pearson <ppearson@nowhere.invalid> - 2013-02-21 21:59 +0000
      Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 09:11 +1100
      Re: Confusing math problem Dave Angel <davea@davea.name> - 2013-02-21 17:33 -0500
      Re: Confusing math problem Chris Angelico <rosuav@gmail.com> - 2013-02-22 10:15 +1100
    Re: Confusing math problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-22 09:16 +0000
      Re: Confusing math problem Serhiy Storchaka <storchaka@gmail.com> - 2013-02-22 13:48 +0200
  Re: Confusing math problem Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-22 09:27 +0000

csiph-web