Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8439957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:09:04+00:00 2026-06-10T08:09:04+00:00

I´m completely new to NumPy and tried a textbook code. Unfortunately, at a certain

  • 0

I´m completely new to NumPy and tried a textbook code. Unfortunately, at a certain size of calculations, the NumPy results get screwed up. Here´s the code:

import sys
from datetime import datetime
import numpy

def pythonsum(n):
    a = range(n)
    b = range(n)
    c = []
    for i in range(len(a)):
        a[i] = i**2
        b[i] = i**3
        c.append(a[i]+b[i])
    return c

def numpysum(n):
    a = numpy.arange(n) ** 2
    b = numpy.arange(n) ** 3
    c = a + b
    return c

size = int(sys.argv[1])
start = datetime.now()
c=pythonsum(size)
delta = datetime.now()-start
print "The last 2 elements of the sum",c[-2:]
print "PythonSum elapsed time in microseconds", delta.microseconds
start = datetime.now()
c=numpysum(size)
delta = datetime.now()-start
print "The last 2 elements of the sum",c[-2:]
print "NumPySum elapsed time in microseconds", delta.microseconds

Results get negative when size >= 1291
I´m working with python 2.6, MacOSX 10.6, NumPy 1.5.0
Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:09:05+00:00Added an answer on June 10, 2026 at 8:09 am

    I think there’s some confusion in this thread. The reason that the pure-Python, i.e. non-numpy, code works doesn’t have anything to do with 32-bit vs 64-bit. It will work correctly on either: Python ints can be of arbitrary size. [There’s a bit of an implementation detail in the background involving whether it calls something an int or a long but you don’t have to worry about it, the conversion is seamless. That’s why sometimes you’ll see L at the end of a number.]

    For example:

    >>> 2**100
    1267650600228229401496703205376L
    

    On the other hand, numpy integer dtypes are fixed-precision, and will always fail for large enough numbers, regardless of how wide:

    >>> for kind in numpy.int8, numpy.int16, numpy.int32, numpy.int64:
    ...     for power in 1, 2, 5, 20:
    ...         print kind, power, kind(10), kind(10)**power
    ... 
    <type 'numpy.int8'> 1 10 10
    <type 'numpy.int8'> 2 10 100
    <type 'numpy.int8'> 5 10 100000
    <type 'numpy.int8'> 20 10 -2147483648
    <type 'numpy.int16'> 1 10 10
    <type 'numpy.int16'> 2 10 100
    <type 'numpy.int16'> 5 10 100000
    <type 'numpy.int16'> 20 10 -2147483648
    <type 'numpy.int32'> 1 10 10
    <type 'numpy.int32'> 2 10 100
    <type 'numpy.int32'> 5 10 100000
    <type 'numpy.int32'> 20 10 1661992960
    <type 'numpy.int64'> 1 10 10
    <type 'numpy.int64'> 2 10 100
    <type 'numpy.int64'> 5 10 100000
    <type 'numpy.int64'> 20 10 7766279631452241920
    

    You can get the same results from numpy as from pure Python by telling it to use the Python type, i.e. dtype=object, albeit at a significant performance hit:

    >>> import numpy
    >>> numpy.array([10])
    array([10])
    >>> numpy.array([10])**100
    __main__:1: RuntimeWarning: invalid value encountered in power
    array([-2147483648])
    >>> numpy.array([10], dtype=object)
    array([10], dtype=object)
    >>> numpy.array([10], dtype=object)**100
    array([ 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000], dtype=object)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am completely new to using code first for modeling databases. I've tried to
Im completely new to Java web stuff, but here goes my question: 1) How
Completely new to mongo here. I have following fields in on of my mysql
I'm completely new to socket programming with C#, I'm trying to get two running
Completely new to R here. I ran R in SPSS to solve some complex
I am completely new to the socket.io and trying to get my feet wet
Completely new to most of this stuff, but basically Im playing around with the
Completely new to asp.net mvc... completely new to web apps so bear with me...
Completely new to java and I have been playing around with regex in a
I am completely new to HTML5 and have been reading about it for the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.