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


Groups > comp.lang.python > #39499

Re: Confusing math problem

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'essentially': 0.04; 'importing': 0.04; 'float': 0.05; 'computed': 0.07; 'overflow': 0.07; 'corresponds': 0.09; 'method:': 0.09; 'received:mail-vb0-f46.google.com': 0.09; 'typeerror:': 0.09; 'underlying': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'called,': 0.16; 'cc:name:python list': 0.16; 'double)': 0.16; 'exposes': 0.16; 'wrote:': 0.17; 'basically': 0.17; '>>>': 0.18; 'received:209.85.212.46': 0.18; 'module': 0.19; 'appropriate': 0.20; 'math': 0.20; 'import': 0.21; '"",': 0.22; 'converted': 0.22; 'subject:problem': 0.22; 'cc:2**0': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'wrote': 0.26; '(most': 0.27; 'library.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'skip:_ 10': 0.29; 'class': 0.29; 'returned': 0.30; 'function': 0.30; 'file': 0.32; 'int': 0.33; 'like:': 0.33; 'traceback': 0.33; 'received:google.com': 0.34; 'whatever': 0.35; 'returning': 0.35; 'received:209.85': 0.35; 'method': 0.36; 'possible': 0.37; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'your': 0.60; 'different': 0.63; 'skip:n 10': 0.63; '2013': 0.84; 'oscar': 0.84; 'not:': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=xBBOrnHGZJuUhp/hzeJ9oAJ2F508b17/V7G8vMdSm6M=; b=twpOvGceKw46Dmjq5EiU8uAoOD0DgmQ7WxOrgo1URJcLT2FVx2snMgNeuuh27xgjVN 4sSJZG4e5e2dC5fManz/SM1/mfi6plK3xhBLHJYBHqfgznDjDYtsxQQI3FFXyN6N/0nJ 4sBa75RV+fGnPt9VAwwP8UBP/sXSN7p7AtH7PKY+fap0TLEjY8/MORav6a6OVY9v3BMr ltf+Wv+GAQRP1nMer6BKBdD7n/Ib2rAQs23kbgClsl1hQZE29/Kx8jb+5fzUYfLQ7CQA +5fUrMSV3qnmpQRkagMgPHKYpVMHN9uGVSHyVlTxlapwYtg1wg639HDc9qIkKatcumcY 72Sw==
X-Received by 10.52.89.83 with SMTP id bm19mr29536527vdb.123.1361491490920; Thu, 21 Feb 2013 16:04:50 -0800 (PST)
MIME-Version 1.0
In-Reply-To <kg6b9d$sj2$1@dont-email.me>
References <kg5sog$lfb$1@dont-email.me> <mailman.2191.1361478337.2939.python-list@python.org> <kg67l7$bm8$1@dont-email.me> <mailman.2209.1361487258.2939.python-list@python.org> <kg6b9d$sj2$1@dont-email.me>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Fri, 22 Feb 2013 00:04:30 +0000
Subject Re: Confusing math problem
To Schizoid Man <schiz_man@21stcentury.com>
Content-Type text/plain; charset=ISO-8859-1
Cc Python List <python-list@python.org>
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.2221.1361491498.2939.python-list@python.org> (permalink)
Lines 53
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361491498 news.xs4all.nl 6881 [2001:888:2000:d::a6]:44622
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39499

Show key headers only | View raw


On 21 February 2013 23:41, Schizoid Man <schiz_man@21stcentury.com> wrote:
> "Oscar Benjamin" <oscar.j.benjamin@gmail.com> wrote in
>> Then you want operator.pow:
>>
>>>>> import operator
>>>>> operator.pow(3, 2)
>>
>> 9
>>
>> math.pow is basically the (double)pow(double, double) function from
>> the underlying C library. operator.pow(a, b) is precisely the same as
>> a**b.
>
> So how is operator.pow() different from just pow()?

operator.pow(a, b) calls a.__pow__(b). This is also what a**b does. If
a and b are ints then the __pow__ method will create the appropriate
int exactly without overflow and return it. You can make your own
class and give it a __pow__ function that does whatever you like:

>>> class Number(object):
...   def __pow__(self, other):
...     print('__pow__ called, returning 3')
...     return 3
...
>>> n = Number()
>>> result = n**3
__pow__ called, returning 3
>>> result
3

operator.pow will call the method:

>>> import operator
>>> operator.pow(n, 3)
__pow__ called, returning 3
3

math.pow will not:

>>> import math
>>> math.pow(n, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required

The math module essentially exposes the functions that would be
available in C after importing math.h. So when you call math.pow(a,
b), a and b are if possible converted to float (which corresponds to a
double in C) and then the result is computed and returned as a float.


Oscar

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