-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
math/rand: rand.Float{32,64}() can erroneously return 1 #6721
Labels
Milestone
Comments
Float32() is just float32(r.Float64()) This happens on play.golang.org on call number 7533753 to Float64, which returns 0.9999999777168924 (bits: 11111111101111111111111111111111110100000010010110111011011010) http://play.golang.org/p/tGq_lqQIeL Does seem like a better solution is to use Int32 to get mantissa bits and set the exponent, then use http://golang.org/pkg/math/#Float32frombits on the bits. This technique would also allow us to skip the floating point divide in Float64(). |
An alternative solution from my response in https://groups.google.com/forum/#!topic/golang-nuts/94bTFkFUteE (math.Float32frombits(0x3f800000 | (0x007fffff & uint32(VALUE))) - 1.0) use IEEE 754 exponent for range [1,2) combine with mantissa bits of VALUE make float32 subtract 1 I initially proposed this for float64 in https://golang.org/issue/4965 but Remy convinced me that the current approach for float64 is superior. Maybe it's usefull for float32. |
This issue was closed by revision 17dc712. Status changed to Fixed. |
CL https://golang.org/cl/95460049 mentions this issue. |
This issue was closed by revision 5aca051. |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
by akalin:
The text was updated successfully, but these errors were encountered: