8000 random.choice doesn't allow float128 probs · Issue #6132 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

random.choice doesn't allow float128 probs #6132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
argriffing opened this issue Jul 29, 2015 · 5 comments
Open

random.choice doesn't allow float128 probs #6132

argriffing opened this issue Jul 29, 2015 · 5 comments

Comments

@argriffing
Copy link
Contributor

I think this is due to the same_kind casting in numpy 1.10, and I don't know whether this is considered a bug.

>>> import numpy as np
>>> probs = np.array([0.4, 0.4, 0.2], dtype=np.float128)
>>> np.random.choice([1, 2, 3], p=probs)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mtrand.pyx", line 1087, in mtrand.RandomState.choice (numpy/random/mtrand/mtrand.c:11331)
TypeError: Cannot cast array data from dtype('float128') to dtype('float64') according to the rule 'safe'
@charris
Copy link
Member
charris commented Jul 30, 2015

"same_kind" would work, it is "safe" that is failing. Since double is specified "same_kind" should work. For that routine we could use long double as the type, which would be platform specific but would work. I think specifying same_kind would be an adequate solution.

@ron819
Copy link
ron819 commented Nov 18, 2018

Is this still an issue? Open since 2015 with no comments.

@mattip
Copy link
Member
mattip commented Nov 19, 2018

@ron819 it is relatively simple to run the code at the top of the issue to determine this is still not solved

bashtage added a commit to bashtage/numpy that referenced this issue Apr 23, 2021
Force cast longdouble to double to allow longdoubles to be used as probabilities

closes numpy#6132
@bashtage
Copy link
Contributor
bashtage commented Apr 23, 2021

Consensus in #18835 is that implicit conversion of unsafe types to float64 is a bad idea, and so raising is the right choice.

@bashtage
Copy link
Contributor

Correctly implementing this within the current implementation of choice would first require Generator.random to support longdouble random values (or any other extended precision type that is supported). The current implementation only support float32 and float64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
0