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


Groups > comp.lang.python > #39481

Re: Confusing math problem

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!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; 'python.': 0.02; 'operator': 0.03; 'correct.': 0.07; 'overflow': 0.07; 'see.': 0.07; 'integers': 0.09; 'modules.': 0.09; 'received:mail- vc0-f174.google.com': 0.09; 'underlying': 0.09; 'cc:addr:python- list': 0.10; '2.7.3': 0.16; 'cc:name:python list': 0.16; 'double)': 0.16; 'integers,': 0.16; 'non-integers': 0.16; 'precision,': 0.16; 'shorthand': 0.16; 'storing': 0.16; 'url:decimal': 0.16; 'wrote:': 0.17; 'basically': 0.17; 'integer': 0.17; 'library,': 0.17; 'examples': 0.18; '>>>': 0.18; 'memory': 0.18; '(not': 0.20; 'suggested': 0.20; 'import': 0.21; 'operations.': 0.22; 'subject:problem': 0.22; 'runs': 0.22; 'cc:2**0': 0.23; 'work.': 0.23; "python's": 0.23; 'seems': 0.23; 'machine': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'wrote': 0.26; 'library.': 0.27; 'message- id:@mail.gmail.com': 0.27; '(unless': 0.29; 'arithmetic': 0.29; 'cases.': 0.29; 'decimal': 0.29; 'received:209.85.220.174': 0.29; 'strings,': 0.29; 'class': 0.29; "i'm": 0.29; 'normally': 0.30; 'performing': 0.30; 'function': 0.30; 'url:python': 0.32; 'int': 0.33; 'operations': 0.33; 'received:google.com': 0.34; 'third': 0.34; 'built-in': 0.35; 'pm,': 0.35; 'received:209.85.220': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; "didn't": 0.36; 'uses': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'url:docs': 0.38; 'url:en': 0.38; 'instead': 0.39; 'think': 0.40; 'your': 0.60; 'range': 0.60; 'high': 0.61; 'url:index': 0.61; 'more': 0.63; 'results': 0.65; 'friend': 0.81; '*really*': 0.84; '2013': 0.84; 'oscar': 0.84; 'hand,': 0.97
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=eQ67IXKixZXh/lmtMbcqjt3pzqWJR9D4G1xeNc0ur4o=; b=x5HnW7HpaqoejnRUepXOW7GqtC1Ew1PMDtHwO0mL2pfJx+OuHUQ2derjXwccsPxZEm Zr9ut6K7Pmq6agJStM0ZKIfaVHgaDQwFUKdesLUO+vcCo4hTp0+XLbpro62TXwR1XYcH tpKHBYbr8zepZWvA5bf4qn2Md46N3/iagby3iJ9jGPu41pBUg5eeM02CRD8JjhzJI4Tb XSK7mlJTwSWlBaqUWm84QZxjNNSv7K0z4Xrdfqr29WfOm45yP2l/BkQJWo5PFmlcLUnB iAJ9XW1sNTrHIUbdmRbvs4hIO5l7QaKMWgJuQ0/WnKkFTN6MoUntMuA0OGUMR8/x1jMp oqRA==
X-Received by 10.58.171.38 with SMTP id ar6mr34737020vec.23.1361487256120; Thu, 21 Feb 2013 14:54:16 -0800 (PST)
MIME-Version 1.0
In-Reply-To <kg67l7$bm8$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>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Thu, 21 Feb 2013 22:53:55 +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.2209.1361487258.2939.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361487258 news.xs4all.nl 6882 [2001:888:2000:d::a6]:47828
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:39481

Show key headers only | View raw


On 21 February 2013 22:39, Schizoid Man <schiz_man@21stcentury.com> wrote:
> "Dave Angel" <davea@davea.name> wrote in message
>>
>> On 02/21/2013 02:33 PM, Schizoid Man wrote:
>> 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()
>
> I see. I thought using the ** was shorthand for math.pow() and didn't think
> that one would be integer operations and the other floats.

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.

> I'm performing
> some large integer arithmetic operations. I would normally do this my
> writing my own multiplication class and storing results as strings, but a
> friend suggested that I look at Python.

There's no need to use strings if you're working with integers in
Python. The results with int (not float) will be exact and will not
overflow since Python's ints have unlimited range (unless your machine
runs out of memory but that only happens with *really* big integers).

If you want to do computations with non-integers and high precision,
take a look at the decimal and fractions modules.
http://docs.python.org/2/library/decimal.html
http://docs.python.org/2/library/fractions.html

There is also a good third party library, sympy, for more complicated
exact algebra:
http://sympy.org/en/index.html


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