Skip to content

Using C int default leads to overflow on 32-bit platforms #6056

Closed

Description

@matthew-brett

I did not expect this behavior:

In [2]: import numpy as np
In [3]: np.__version__
Out[3]: '1.10.0.dev0+b1a7d4c'
In [4]: np.prod((500, 500, 500, 500))
Out[4]: -1924509440

I assume this is because:

In [8]: np.array((500, 500, 500, 500)).dtype
Out[8]: dtype('int32')

Is there a good reason not to default to int64 int casting on 32 bit platforms? Is the speed difference large enough to justify this risk? Or is it the memory consumption?

Activity

  1. seberg commented on Jul 9, 2015

    @seberg
    Member

    The reason used to be that long is what pythons 2 int is, so we use that as default int (that way what you get subclasses python's int on python 2). Only addition/sums have an "upcast to 64 bit to be safe" logic.
    Yeah, I think it might make sense to default to 64bits on all platforms (or at least on python 3).

    EDIT: On windows you get int32 even if with 64 bit platform.

  2. jorenham commented on Jul 22, 2026

    @jorenham
    Member

    This is no longer a problem :)

    In [1]: np.prod(np.array([500, 500, 500, 500], dtype=np.int32))
    Out[1]: np.int64(62500000000)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions