9

I wonder why a - b and a + (-b) give the same result but in different types in numpy:

import numpy as np

minuend = np.array(1, dtype=np.int64)
subtrahend = 1 << 63

result_minus = minuend - subtrahend
result_plus = minuend + (-subtrahend)

print(result_minus == result_plus)  # True
print(type(result_minus))  # float64
print(type(result_plus))  # int64

Why is that, and where can I read about it?

bers
  • 4,817
  • 2
  • 40
  • 59

1 Answers1

12

The point is that 1 << 63 cannot be represented using a int64 type, but -(1 << 63) can. This is a pathological case coming from how signed integers are represented in binary (C2 representation).

On one hand, Numpy converts subtrahend (1 << 63) to a uint64 value because int64 is too small to hold the value.

On another hand, -subtrahend is computed by CPython so it results in a pure-Python integer containing -(1 << 63). The value is then converted by Numpy to a int64 value (because this type is large enough to hold the value).

Numpy only operates on arrays of the same types internally. Binary operations involving different types result in array promotions (inherited from the C language mainly because Numpy is written in C) : Numpy converts the type of the input arrays so the target binary operation makes sense and is also safe (no overflow, at the expense of a possible loss of precision in pathological cases).

In this case, when the subtraction is performed, Numpy chooses to store the final array in a float64 array because a binary operation on both uint64 and int64 array is likely to cause overflows (unsigned integers are too big to be stored in signed ones and negative signed integer cannot be represented in unsigned ones). When the addition is performed, the two array/values are of the same type (i.e. int64), so there is no need for any promotion and the resulting array is of type int64.

Here is a way to see that:

>>> np.int64(1 << 63)
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
Cell In [7], line 1
----> 1 np.int64(1 << 63)

OverflowError: Python int too large to convert to C long

>>> np.int64(-(1 << 63))
-9223372036854775808

>>> np.array(subtrahend).dtype
dtype('uint64')

# Numpy first convert both to float64 arrays to avoid overflows in this specific case due to mixed uint64+int64 integer types
>>> (np.array(subtrahend) + minuend).dtype
dtype('float64')

>>> np.array(-subtrahend).dtype
dtype('int64')
Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59
  • "When the value of an integer is too big to be converted to the biggest available integer type, Numpy convert it to floating-point type (64-bit ones if available)." - no, NumPy doesn't do that. Depending on the context, it might use `object` dtype, or it might overflow, but it won't convert too-big integers to floating point. Here, it's using uint64 - your "PS" section is right about that. – user2357112 Aug 05 '23 at 00:56
  • 1
    @user2357112 Yes, this sentence was not actually correct. I wrote a first (quick) answer, then I updated it with a "PS" part that was more correct and forgot to adapt the old part. I fully rewrote the answer by combining both parts to make it more clear and correct. Thank you. – Jérôme Richard Aug 05 '23 at 01:06
  • A general point worth mentioning is that `numpy` does "value-based casting" which I, also using C++, find quite intuitive but very hard to predict. – bers Aug 06 '23 at 19:39