如何将Float的小数部分的前32位转换为Word32?

Ste*_*gle 4 floating-point haskell sha sha256

说我有浮动.我想要这个Float的小数部分的前32位?具体来说,我正在寻找sha256伪代码的这部分工作(来自维基百科)

# Note 1: All variables are unsigned 32 bits and wrap modulo 232 when calculating
# Note 2: All constants in this pseudo code are in big endian

# Initialize variables
# (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
h[0..7] := 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
Run Code Online (Sandbox Code Playgroud)

我天真地尝试做地板(((sqrt 2) - 1)*2 ^ 32),然后将返回的整数转换为Word32.这根本不是正确的答案.我认为通过乘以2 ^ 32的功率,我实际上将它移动了32个位置(在地板之后).显然,并非如此.无论如何,长期和短期是,我如何生成h [0..7]?

aug*_*tss 5

得到h [0..7]的最好方法是从维基百科页面复制十六进制常量.那样你知道你会有正确的.

但如果你真的想要计算它们:

scaledFrac :: Integer -> Integer
scaledFrac x =
    let s = sqrt (fromIntegral x) :: Double
    in  floor ((s - fromInteger (floor s)) * 2^32)

[ printf "%x" (scaledFrac i) | i <- [2,3,5,7,11,13,17,19] ]
Run Code Online (Sandbox Code Playgroud)