C++ 使用的浮点数的二进制格式是什么? 在基于英特尔的系统上?
我有兴趣了解 C++ 在基于 Intel 的系统上使用的单精度或双精度类型的二进制格式。
在数据可能需要由另一个系统(即文件或网络)读取或写入的情况下,我避免使用浮点数。 我确实意识到我可以使用定点数,并且定点更准确,但我有兴趣了解浮点格式。
I am interested to learn about the binary format for a single or a double type used by C++ on Intel based systems.
I have avoided the use of floating point numbers in cases where the data needs to potentially be read or written by another system (i.e. files or networking). I do realise that I could use fixed point numbers instead, and that fixed point is more accurate, but I am interested to learn about the floating point format.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
维基百科有一个合理的总结 - 请参阅 http://en.wikipedia.org/wiki/IEEE_754 。
伯特,如果你想在系统之间传输数字,你应该避免以二进制格式进行。 要么使用 CORBA(只是开玩笑,伙计们)、Tibco 等中间件,要么依靠旧时最喜欢的文本表示形式。
Wikipedia has a reasonable summary - see http://en.wikipedia.org/wiki/IEEE_754.
Burt if you want to transfer numbers betwen systems you should avoid doing it in binary format. Either use middleware like CORBA (only joking, folks), Tibco etc. or fall back on that old favourite, textual representation.
这应该可以帮助您开始: http://docs.sun.com/source/ 806-3568/ncg_goldberg.html。 (:
This should get you started : http://docs.sun.com/source/806-3568/ncg_goldberg.html. (:
浮点格式由处理器决定,而不是由语言或编译器决定。 如今,几乎所有处理器(包括所有 Intel 台式机)要么没有浮点单元,要么有一个符合 IEEE 754 的浮点单元。您可以获得两到三种不同的大小(带有 SSE 的 Intel 提供 32、64 和 80 位),并且每个有一个符号位、一个指数和一个有效数。 所表示的数字通常由以下公式给出:
其中 k' 是有效数中的位数,k 是中间指数范围内的常数。 零(正零和负零)以及无穷大和其他“非数字”(NaN) 值有特殊的表示形式。
确实有一些怪癖; 例如,分数 1/10 不能精确地表示为二进制 IEEE 标准浮点数。 因此,IEEE 标准也提供了十进制表示形式,但这主要由手持计算器而不是通用计算机使用。
推荐阅读:David Golberg 的每个计算机科学家应该了解的浮点运算
Floating-point format is determined by the processor, not the language or compiler. These days almost all processors (including all Intel desktop machines) either have no floating-point unit or have one that complies with IEEE 754. You get two or three different sizes (Intel with SSE offers 32, 64, and 80 bits) and each one has a sign bit, an exponent, and a significand. The number represented is usually given by this formula:
where k' is the number of bits in the significand and k is a constant around the middle range of exponents. There are special representations for zero (plus and minus zero) as well as infinities and other "not a number" (NaN) values.
There are definite quirks; for example, the fraction 1/10 cannot be represented exactly as a binary IEEE standard floating-point number. For this reason the IEEE standard also provides for a decimal representation, but this is used primarily by handheld calculators and not by general-purpose computers.
Recommended reading: David Golberg's What Every Computer Scientist Should Know About Floating-Point Arithmetic
正如其他发帖者所指出的,有大量关于每个现代处理器使用的 IEEE 格式的信息,但这不是您出现问题的地方。
您可以依赖任何使用 IEEE 格式的现代系统,但您需要注意字节顺序。 在维基百科(或其他地方)查找“字节序”。 Intel系统是little-endian,很多RISC处理器是big-endian。 两者之间的交换很简单,但您需要知道您拥有什么类型。
传统上,人们使用大端格式进行传输。 有时人们会包含一个标头来指示他们正在使用的字节顺序。
如果您想要绝对的可移植性,最简单的方法就是使用文本表示。 但是,如果您想捕获完整的精度,那么对于浮点数来说,这可能会变得非常冗长。 0.1234567890123456e+123。
As other posters have noted, there is plenty of information about on the IEEE format used by every modern processor, but that is not where your problems will arise.
You can rely on any modern system using IEEE format, but you will need to watch for byte ordering. Look up "endianness" on Wikipedia (or somewhere else). Intel systems are little-endian, a lot of RISC processors are big-endian. Swapping between the two is trivial, but you need to know what type you have.
Traditionally, people use big-endian formats for transmission. Sometimes people include a header indicating the byte order they are using.
If you want absolute portability, the simplest thing is to use a text representation. However that can get pretty verbose for floating point numbers if you want to capture the full precision. 0.1234567890123456e+123.
Intel 的表示符合 IEEE 754 标准。
您可以在 http://download 找到详细信息。 intel.com/technology/itj/q41999/pdf/ia64fpbf.pdf。
Intel's representation is IEEE 754 compliant.
You can find the details at http://download.intel.com/technology/itj/q41999/pdf/ia64fpbf.pdf .
请注意,十进制浮点常量可能会在不同系统上转换为不同的浮点二进制值(即使在同一系统上使用不同的编译器)。 差异很小——对于双精度数来说可能只有 2^-54 那么大——但仍然是有差异的。
如果要保证在任何平台上具有相同的浮点二进制值,请使用十六进制常量。
Note that decimal floating-point constants may convert to different floating-point binary values on different systems (even with different compilers on the same system). The difference would be slight -- maybe only as large as 2^-54 for a double -- but is a difference nonetheless.
Use hexadecimal constants if you want to guarantee the same floating-point binary value on any platform.