生成 n 个不是 ∞ 或 NaN 的不同浮点数(在 Go 中)
Generate n different floats that aren't ∞ or NaN (in Go)
我想要一个函数 getNthFloat(uint32 n) float32
使得对于每个 n,m < 2³²-4 且 n≠m,getNthFloat(n)
和 getNthFloat(m)
return 不同的浮点数数字(既不是 NaN 也不是 ±∞)。选择 2³²-4 是因为如果我理解 IEEE 754 正确,NaN 有两种二进制表示,一种表示 ∞,一种表示 -∞。
我想我应该将我的 uint32 转换为位并将位转换为 float32,但我不知道如何有效地避免这四个值。
我认为您正在寻找 math.Float32frombits
函数:https://golang.org/pkg/math/#Float32frombits
You can't get 2^32-4 valid floating point numbers in a float32. IEEE 754 binary32 numbers have two infinities (negative and positive) and 2^24-2 possible NaN values.
A 32 bit floating point number has the following bits:
31 30...23 22...0
sign exponent mantissa
All exponents with the value 0xff are either infinity (when mantissa is 0) or NaN (when mantissa isn't 0). So you can't generate those exponents.
Then it's just a simple matter or mapping your allowed integers into this format and then use math.Float32frombits
to generate a float32. How you do that is your choice. I'd probably be lazy and just use the lowest bit for the sign and then reject all numbers higher than 2^32 - 2^24 - 1 and then shift the bits around.
So something like this (untested):
func foo(n uint32) float32 {
if n >= 0xff000000 {
panic("xxx")
}
return math.Float32frombits((n & 1) << 31 | (n >> 1))
}
N.B。 I'd probably also avoid denormal numbers, that is numbers with the exponent 0 and non-zero mantissa. They can be slow and might not be handled correctly. For example they could all be mapped to zero, there's nothing in the go spec that talks about how denormal numbers are handled, so I'd be careful.
我想要一个函数 getNthFloat(uint32 n) float32
使得对于每个 n,m < 2³²-4 且 n≠m,getNthFloat(n)
和 getNthFloat(m)
return 不同的浮点数数字(既不是 NaN 也不是 ±∞)。选择 2³²-4 是因为如果我理解 IEEE 754 正确,NaN 有两种二进制表示,一种表示 ∞,一种表示 -∞。
我想我应该将我的 uint32 转换为位并将位转换为 float32,但我不知道如何有效地避免这四个值。
我认为您正在寻找 math.Float32frombits
函数:https://golang.org/pkg/math/#Float32frombits
You can't get 2^32-4 valid floating point numbers in a float32. IEEE 754 binary32 numbers have two infinities (negative and positive) and 2^24-2 possible NaN values.
A 32 bit floating point number has the following bits:
31 30...23 22...0
sign exponent mantissa
All exponents with the value 0xff are either infinity (when mantissa is 0) or NaN (when mantissa isn't 0). So you can't generate those exponents.
Then it's just a simple matter or mapping your allowed integers into this format and then use math.Float32frombits
to generate a float32. How you do that is your choice. I'd probably be lazy and just use the lowest bit for the sign and then reject all numbers higher than 2^32 - 2^24 - 1 and then shift the bits around.
So something like this (untested):
func foo(n uint32) float32 {
if n >= 0xff000000 {
panic("xxx")
}
return math.Float32frombits((n & 1) << 31 | (n >> 1))
}
N.B。 I'd probably also avoid denormal numbers, that is numbers with the exponent 0 and non-zero mantissa. They can be slow and might not be handled correctly. For example they could all be mapped to zero, there's nothing in the go spec that talks about how denormal numbers are handled, so I'd be careful.