用双精度表示整数 [英] Representing integers in doubles

查看:37
本文介绍了用双精度表示整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

double(给定字节数,具有合理的尾数/指数平衡)能否始终完全精确地保持该字节数一半的无符号整数的范围?

Can a double (of a given number of bytes, with a reasonable mantissa/exponent balance) always fully precisely hold the range of an unsigned integer of half that number of bytes?

例如八字节双精度型能否完全精确地保存四字节无符号整数的数字范围?

E.g. can an eight byte double fully precisely hold the range of numbers of a four byte unsigned int?

这将归结为两个字节的浮点数是否可以容纳一个字节的无符号整数的范围.

What this will boil down to is if a two byte float can hold the range of a one byte unsigned int.

一个一字节的无符号整数当然是 0 -> 255.

A one byte unsigned int will of course be 0 -> 255.

推荐答案

一个 IEEE754 64 位双精度型可以表示任何 32 位整数,仅仅因为它有 53 个奇数(a) 位可用为了精度和 32 位整数只需要 32 :-)

An IEEE754 64-bit double can represent any 32-bit integer, simply because it has 53-odd(a) bits available for precision and the 32-bit integer only needs, well, 32 :-)

对于(非 IEEE754 双精度)64 位浮点数来说,小于 32 位的精度是合理的.这将允许真正巨大的数字(由于指数),但以精度为代价.

It would be plausible for a (non IEEE754 double precision) 64-bit floating point number to have less than 32 bits of precision. That would allow truly huge numbers (due to the exponent) but at the cost of precision.

最重要的是,如果浮点数的尾数中的精度位数比整数中的位数多(并且指数中的位数足以缩放它),那么它可以在不丢失的情况下表示精度.

The bottom line is that, provided there are more bits of precision in the mantissa of the floating point number than there are in the integer (and enough bits in the exponent to scale it), then it can be represented without loss of precision.

(a) 从技术上讲,第 53 位精度是序列开头的隐含 1,因此可变性"的数量可能只有 52 位.无论是 52 还是 53,仍然足够表示每个 32 位整数.

(a) Technically, the 53rd bit of precision is an implied 1 at the start of the sequence so the amount of "variablity" may only be 52 bits. Whether it's 52 or 53, it's still enough bits to represent every 32-bit integer.

这篇关于用双精度表示整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆