Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

javascript - 节点Buffer.readFloatBE()将数据扩展到64位(Node Buffer.readFloatBE() extends data to 64-bit)

When converting data to/from a Buffer using 32-bit floating point numbers, it seems that nodejs extends the float to double using some criteria that makes the transformation not reversible:

(当使用32位浮点数将数据与Buffer之间进行数据转换时,似乎nodejs使用一些使转换不可逆的条件将float扩展为double:)

> f=3.81357913e+32
3.81357913e+32

> b.Buffer.alloc(4)
<Buffer 00 00 00 00>

> b.writeFloatBE(f)
4

> b
<Buffer 75 96 6b 4f>

So it seems that the original value (3.81357913e+32) is represented as 0x75966b4f (Big endian IEEE-754, single precision) Now, when you read the same value, you get a different value:

(因此,似乎原始值(3.81357913e + 32)表示为0x75966b4f(大端IEEE-754,单精度),现在,当您读取相同的值时,会得到一个不同的值:)

> b
<Buffer 75 96 6b 4f>

> b.readFloatBE()
3.813579129065691e+32

So the two values are different.

(因此,这两个值是不同的。)

I would expect the two values to be the same.

(我希望这两个值是相同的。)

It seems the readFloatBE() produces a 64-bit double-precision with arbitrary values.

(似乎readFloatBE()产生具有任意值的64位双精度。)

I understand when rounding a double to a float, you lose precision.

(我了解到将双精度数四舍五入为浮点数会失去精度。)

All the following numbers when reduced to float have the same encoded value:

(以下所有数字减为float时,都具有相同的编码值:)

3.813579129099999e+32  -> 0x75966b4f
3.813579129065691e+32  -> 0x75966b4f
3.813579129000000e+32  -> 0x75966b4f

... but where is this value (3.813579129065691e+32) comes from?

(...但是这个值(3.813579129065691e + 32)从哪里来?)

(Tested with node.js v.11.12.0)

((已使用node.js v.11.12.0测试))

  ask by fabrizi0 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

0x75966b4f, when converted to a lot of precision, is 3.813579129065691e+32.

(转换为很多精度时,0x75966b4f为3.813579129065691e + 32。)

Normally, a FLOAT is displayed only to the available precision, which is about 7 decimal digits.

(通常,仅以可用的精度显示FLOAT,该精度约为7个十进制数字。)

It looks like they converted to double, adding zeros to fill out to DOUBLE precision.

(看起来它们转换为双精度,添加零以填充为DOUBLE精度。)

Such would normally be represented to about 16 significant decimal digits, such as 3.813579129065691e+32.

(通常将其表示为约16个有效十进制数字,例如3.813579129065691e + 32。)

Some languages abuse the term "float" to mean any floating point number.

(一些语言滥用术语“浮点数”来表示任何浮点数。)

IEEE-754 has specific meanings for "float" and "double", as above.

(如上所述,IEEE-754具有“浮点”和“双精度”的特定含义。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...