Huayi Wei
Huayi Wei

Reputation: 849

Does numpy.bincount support numpy.float128 type weights?

Here is a sample code using numpy.bincount

import numpy as np
a = np.array([1.0, 2.0, 3.0], dtype=np.float128)
b = np.array([1, 2, 0], dtype=np.int)
c = np.bincount(b, weights=a)

If run it, I get the following error report:

----> 1 c = np.bincount(b, weights=a)
TypeError: Cannot cast array data from dtype('float128') to dtype('float64') according to the rule 'safe'

Is it a bug of np.bincount? Does there exist any similar function which I can use to work with numpy.float128 type weights?

Upvotes: 4

Views: 499

Answers (1)

user545424
user545424

Reputation: 16179

I wouldn't necessarily call it a bug, but it's not supported. The bincount() function is implemented here. As you can see the weights parameter is cast directly to a double array:

if (!(wts = PyArray_ContiguousFromAny(weight, PyArray_DOUBLE, 1, 1))) {
    goto fail;
}

Therefore, it's not possible to pass a np.float128 object to bincount.

Of course you can always cast it to a np.float64 object as suggested in the comments if the extra precision isn't required.

Upvotes: 1

Related Questions