2

I am observing this weird behavior when I am raising an integer to the negative power using an np.array. Specifically, I am doing

import numpy as np

a = 10**(np.arange(-1, -8, -1))

and it results in the following error.

ValueError: Integers to negative integer powers are not allowed.

This is strange as the code 10**(-1) works fine. However the following workaround (where 10 is a float instead of integer) works fine.

import numpy as np

a = 10.**(np.arange(-1, -8, -1)
print(a)  # Prints array([1.e-01, 1.e-02, 1.e-03, 1.e-04, 1.e-05, 1.e-06, 1.e-07])

Why is it not valid for integers? Any explanation is appreciated.

learner
  • 3,168
  • 3
  • 18
  • 35

1 Answers1

2

This is happening because the input 10 is an integer.

10**(np.arange(-1, -8, -1))

numpy.arange() is designed such a way that that has to give 10**(np.arange(-1, -8, -1)) integers or nothing since input is an integer.

On the contrary;

a = 10.**(np.arange(-1, -8, -1)

gives results happily as 10.0 is a float


Edit: found an answer to back my point; voted for a duplicate:

Why can't I raise to a negative power in numpy?

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44