浮点数是如何存储的?什么时候重要? [英] How is floating point stored? When does it matter?
问题描述
跟进这个问题,看来有些数字根本不能用浮点数表示,而是近似的.
In follow up to this question, it appears that some numbers cannot be represented by floating point at all, and instead are approximated.
浮点数是如何存储的?
不同尺寸是否有共同标准?
Is there a common standard for the different sizes?
如果我使用浮点,我需要注意哪些问题?
What kind of gotchas do I need to watch out for if I use floating point?
它们是否跨语言兼容(即,我需要处理哪些转换才能通过 TCP/IP 将浮点数从 python 程序发送到 C 程序)?
Are they cross-language compatible (ie, what conversions do I need to deal with to send a floating point number from a python program to a C program over TCP/IP)?
推荐答案
如前所述,维基百科文章IEEE 754 很好地展示了浮点数在大多数系统上的存储方式.
As mentioned, the Wikipedia article on IEEE 754 does a good job of showing how floating point numbers are stored on most systems.
现在,这里有一些常见的陷阱:
Now, here are some common gotchas:
- 最大的问题是您几乎不想比较两个浮点数是否相等(或不相等).您需要改用大于/小于比较.
- 对浮点数执行的操作越多,舍入误差就越大.
- 精度受分数大小的限制,因此您可能无法正确添加相隔几个数量级的数字.(例如,您将无法将 1E-30 添加到 1E30.)
这篇关于浮点数是如何存储的?什么时候重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!