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

  • Home
  • SEARCH
  • 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 7414829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:10:42+00:00 2026-05-29T07:10:42+00:00

I have the following code: import numpy def numpysum(n): a = numpy.arange(n) ** 2

  • 0

I have the following code:

import numpy

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


size = 3000
c = numpysum(size)

When running, I get the error:

D:\Work\programming\python\test_1\src\test1_numpy.py:6: RuntimeWarning: invalid value encountered in power
b = numpy.arange(n) ** 3

Note that the following numpyless function works fine:

def pythonsum(n):
   a = list(range(n))
   b = list(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

I guess it happens because I try to raise a large number to power three. What can I do, beside working with floating point numbers?

I am working with Python 3.2.

  • 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-05-29T07:10:43+00:00Added an answer on May 29, 2026 at 7:10 am

    numpy is actually looking out for you on this one. Unlke in standard Python, its integer operations don’t work on arbitrary-precision objects. I’d guess you were running a 32-bit python, because the same operations don’t overflow for me:

    >>> sys.maxsize
    9223372036854775807
    >>> size = 3000
    >>> c = numpysum(size)
    >>>
    

    but they will eventually. Even easier to see if you control the size of the type manually:

    >>> numpy.arange(10, dtype=numpy.int8)**10
    __main__:1: RuntimeWarning: invalid value encountered in power
    array([  0,   1,   0, -87,   0,  -7,   0, -15,   0,   0], dtype=int8)
    >>> numpy.arange(10, dtype=numpy.int16)**10
    array([     0,      1,   1024,  -6487,      0,    761, -23552,  15089,
                0,      0], dtype=int16)
    >>> numpy.arange(10, dtype=numpy.int32)**10
    array([          0,           1,        1024,       59049,     1048576,
               9765625,    60466176,   282475249,  1073741824, -2147483648], dtype=int32)
    >>> numpy.arange(10, dtype=numpy.int64)**10
    array([         0,          1,       1024,      59049,    1048576,
              9765625,   60466176,  282475249, 1073741824, 3486784401])
    

    where things improve as the number of bits increases. If you really want numpy array operations on Python arbitrary-size integers, you can set dtype to object:

    >>> numpy.arange(10, dtype=object)**20
    array([0, 1, 1048576, 3486784401, 1099511627776, 95367431640625,
           3656158440062976, 79792266297612001, 1152921504606846976,
           12157665459056928801], dtype=object)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have the following code: import sqlite sqlite.connect('tmp.db').cursor().close() I get the following error
I have the following code: import string def translate_non_alphanumerics(to_translate, translate_to='_'): not_letters_or_digits = u'!#%\'()*+,-./:;<=>?@[\]^_`{|}~' translate_table
I have 3 1-D ndarrays: x, y, z and the following code: import numpy
I have the following code import smtplib from email.mime.text import MIMEText smtpserver = 'smtp.gmail.com'
I have the following code: import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; public
I have the following code: import string import random d =[random.choice(string.uppercase) for x in
I have the following code: import com.apple.dnssd.*; public interface IServiceAnnouncer { public void registerService();
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
I have the following code: from selenium import selenium selenium = selenium(localhost, 4444, *chrome,
I have written the following IronPython code: import clr clr.AddReference(System.Drawing) from System import *

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.