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


Groups > comp.lang.python > #49376

Re: ? get negative from prod(x) when x is positive integers

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <joshua.landau.ws@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.052
X-Spam-Evidence '*H*': 0.90; '*S*': 0.00; '[1,': 0.09; 'calculating': 0.09; 'integers': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'elem': 0.16; 'guess.': 0.16; 'integers.': 0.16; 'skip:3 30': 0.16; 'skip:6 30': 0.16; 'skip:[ 30': 0.16; 'subject:when': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; 'example.': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'subject:) ': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'lists': 0.32; 'actual': 0.34; 'subject:from': 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'doing': 0.36; 'skip:- 20': 0.37; 'list': 0.37; 'lists.': 0.38; 'negative': 0.60; 'skip:n 30': 0.60; 'show': 0.63; 'such': 0.63; 'skip:1 20': 0.65; '.....': 0.78; 'as:': 0.81; 'subject:get': 0.81; 'max,': 0.84; 'skip:8 30': 0.84; 'on?': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=59BfI9NvX7KihyjqWPANevd5RWd80CcHYmqnVxV0ZDw=; b=N7BrE/XNzsErGCv2Nka9l+fyV2r57KszCYYd38zTkXJRQCNimcHnMGM5S6qgmWPaCM Ynnwm2xlMyBI1grM/cj7i5KGEPV80KfqqTcZD+wkoQgrYSLPQqJGjU+fPGChLHHl8Cj6 WUlkKXb0SPtN5IplIchTyjuMB3d21xiEsKMFIks0SmUcxYCJZkmQlz/2qNLNDzhuTLZs /ODlj20sm06vqd83+dCJKapZ3gW6JG1GuI6HU3WgeOSfkPkLzktpH1ECCjvh7cUkuOk3 GsS0tw84k84e/qqn8cTBEObL/YVRRjoNHDxaAefrBwWRHUAdXjouR6VohjEEQXb2gts/ qkXA==
X-Received by 10.152.45.65 with SMTP id k1mr6642806lam.78.1372431904223; Fri, 28 Jun 2013 08:05:04 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <CALyJZZUcfwJGbn_UqCmsUUcv9MaimyD+vjjW+d64wVc0G0f92A@mail.gmail.com>
References <CALyJZZUcfwJGbn_UqCmsUUcv9MaimyD+vjjW+d64wVc0G0f92A@mail.gmail.com>
From Joshua Landau <joshua.landau.ws@gmail.com>
Date Fri, 28 Jun 2013 16:04:24 +0100
Subject Re: ? get negative from prod(x) when x is positive integers
To Vincent Davis <vincent@vincentdavis.net>
Content-Type text/plain; charset=UTF-8
Cc "python-list@python.org" <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.3960.1372431911.3114.python-list@python.org> (permalink)
Lines 73
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1372431911 news.xs4all.nl 15908 [2001:888:2000:d::a6]:54252
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:49376

Show key headers only | View raw


On 28 June 2013 15:38, Vincent Davis <vincent@vincentdavis.net> 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?

Let me guess.
These are your lists (sorted):

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4]

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4]

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4]

You are using numpy.prod()

Numpy.prod overflows:

    >>> numpy.prod([-9223372036854775808, 2])
    ... 0

You want to use something that doesn't such as:

    def prod(iter):
        p = 1
        for elem in iter:
            p *= elem
        return p

and then you get your correct products:

    8002414661101704746694488837062656
    3907429033741066770846918377472
    682872717747345471717929714096013312

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


Thread

Re: ? get negative from prod(x) when x is positive integers Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-28 16:04 +0100

csiph-web