Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!news.skynet.be!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'float': 0.07; 'calculating': 0.09; 'integers': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'bit)': 0.16; 'integers.': 0.16; 'numpy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'skip:[ 30': 0.16; 'subject:when': 0.16; 'wrote:': 0.18; "python's": 0.19; 'machine': 0.22; '>>>': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; 'example.': 0.24; 'integer': 0.24; 'header:X-Complaints-To:1': 0.27; 'subject:) ': 0.29; 'workaround': 0.31; 'lists': 0.32; 'actual': 0.34; 'subject:from': 0.34; 'but': 0.35; 'really': 0.36; 'doing': 0.36; 'skip:- 20': 0.37; 'list': 0.37; 'lists.': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'negative': 0.60; 'show': 0.63; 'skip:n 10': 0.64; 'skip:1 20': 0.65; '.....': 0.78; 'subject:get': 0.81; 'max,': 0.84; 'on?': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: ? get negative from prod(x) when x is positive integers Date: Fri, 28 Jun 2013 17:01:30 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b9a0.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372431698 news.xs4all.nl 15907 [2001:888:2000:d::a6]:51896 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49375 Vincent Davis wrote: > I have a list of a list of integers. The lists are long so i cant really > show an actual example of on of the lists, but I know that they contain > only the integers 1,2,3,4. so for example. > s2 = [[1,2,2,3,2,1,4,4],[2,4,3,2,3,1]] > > I am calculating the product, sum, max, min.... of each list in s2 but I > get negative or 0 for the product for a lot of the lists. (I am doing this > in ipython) > > for x in s2: > print('len = ', len(x), 'sum = ', sum(x), 'prod = ', prod(x), 'max = > ', > max(x), 'min = ', min(x)) > > ... > > ('len = ', 100, 'sum = ', 247, 'prod = ', 0, 'max = ', 4, 'min = ', 1) > ('len = ', 100, 'sum = ', 230, 'prod = ', -4611686018427387904, 'max = > ', 4, 'min = ', 1) > ('len = ', 100, 'sum = ', 261, 'prod = ', 0, 'max = ', 4, 'min = ', 1) > > ..... > > ('prod =', 0, 'max =', 4, 'min =', 1) > ('prod =', 1729382256910270464, 'max =', 4, 'min =', 1) > ('prod =', 0, 'max =', 4, 'min =', 1) > > .... > > > Whats going on? Instead of Python's integer type (int/long) numpy uses the faster machine integers (typically 32 or 64 bit) that overflow: >>> a = numpy.array([0xffffffff, 0xffffffff]) >>> numpy.prod(a) -8589934591 >>> a.dtype dtype('int64') As a workaround you can use Python's ints (slower) >>> a = numpy.array([0xffffffff, 0xffffffff], dtype=object) >>> numpy.prod(a) 18446744065119617025L or float (loss of precision) >>> a = numpy.array([0xffffffff, 0xffffffff], dtype=float) >>> numpy.prod(a) 1.8446744065119617e+19