Skip to content
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

Random#rand with Range(Float32, Float32) doesn't work #6444

Closed
Heaven31415 opened this issue Jul 25, 2018 · 2 comments
Closed

Random#rand with Range(Float32, Float32) doesn't work #6444

Heaven31415 opened this issue Jul 25, 2018 · 2 comments

Comments

@Heaven31415
Copy link
Contributor

Random#rand method with Range(Float, Float) overload is returning Float64, which makes it impossible to use it with Range(Float32, Float32) argument.

random = Random.new

random.rand(1..10) # ok
random.rand(1u8..10u8) # ok

random.rand(1.0..10.0) # ok
random.rand(1.0f32..10.0f32) # error: type must be Float64, not Float32

I don't know whether there is a reason, but this overload should probably return Float instead of Float64, as in the case of Range(Int, Int) overload.

Any idea how to work around this, beside casting to Float64 and then back to Float32 after generating random number?

Ubuntu 18.04, Crystal 0.25.1 [b782738] (2018-06-27)

@straight-shoota
Copy link
Member

Yeah, it's just the return type is to restrictive, it should be Float not Float64.
Until this is fixed, you can just reopen the class in your code and override the method.

@Heaven31415
Copy link
Contributor Author

Thanks.

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

No branches or pull requests

2 participants