1

Hello i have a verry simple problem. but i don't understand what cause the problem.

I have the following python(3) script. i have numpy 1.13.1(an old one but my problem with dtype should work). according to this the dtype exist in ufunc since 1.6

import numpy as np

M=1001

N = 2 ** np.ceil(np.log2(M))

N
Out[252]: 1024.0

2 ** np.ceil(np.log2(M),dtype=int)
Traceback (most recent call last):

  File "<ipython-input-253-4b982a04c884>", line 1, in <module>
    2 ** np.ceil(np.log2(M),dtype=int)

TypeError: No loop matching the specified signature and casting
was found for ufunc ceil




2 ** np.ceil(np.log2(M),dtype=float)
Out[254]: 1024.0




2 ** np.ceil(np.log2(M),dtype=np.float64)
Out[256]: 1024.0

2 ** np.ceil(np.log2(M),dtype=np.float32)
Out[257]: 1024.0

2 ** np.ceil(np.log2(M),dtype=np.int64)
Traceback (most recent call last):

  File "<ipython-input-258-9902fa43f3ac>", line 1, in <module>
    2 ** np.ceil(np.log2(M),dtype=np.int64)

TypeError: No loop matching the specified signature and casting
was found for ufunc ceil




2 ** np.ceil(np.log2(M),dtype=np.int32)
Traceback (most recent call last):

  File "<ipython-input-259-8a2f2834384f>", line 1, in <module>
    2 ** np.ceil(np.log2(M),dtype=np.int32)

TypeError: No loop matching the specified signature and casting
was found for ufunc ceil

As you can see when i change the dtype to int, int32 or int64 it fails. Probably anything other than float fails. I would guess this shouldn't do that! I can add a small fix so that int(np.ceil(...)) the result is what i want.

  1. I want to know what cause this problem? Since i don't read anything in the numpy reference manual about any issue with this(numpy reference ceil).

  2. If Possible to do solve this problem that it works the way i started

Thanks

user3483203
  • 50,081
  • 9
  • 65
  • 94
Jan-Bert
  • 921
  • 4
  • 13
  • 22
  • 1
    Think about what `ceil` is doing, it makes no sense to do it for `int`. When you specify the `dtype`, you aren't just specifying the return type, but the dtype the calculation is done with as well. Feel free to change the result to an integer after the calculation has been done, but what you are seeing currently is expected. Some better discussion [here](http://numpy-discussion.10968.n7.nabble.com/floor-with-dtype-td44734.html) – user3483203 Sep 19 '18 at 19:41

1 Answers1

2

The reason this fails simply has to do with how dtype is treated by numpy ufuncs. It does not only override the output dtype, but the dtype of the calculation as well. The ceil ufunc doesn't support calculations involving integers, so it fails:

From the docs for ufunc kwargs:

dtype

Overrides the dtype of the calculation and output arrays. Similar to signature.

user3483203
  • 50,081
  • 9
  • 65
  • 94