如何存储浮点数?什么时候重要? [英] How is floating point stored? When does it matter?

查看:162
本文介绍了如何存储浮点数?什么时候重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在跟进这个问题后,它看起来有些数字不能用浮点数表示,而是近似值。

浮点数如何存储?

有不同大小的共同标准吗?

如果我使用浮点数,需要注意哪些问题?他们是跨语言兼容的(即,我需要处理什么转换从python程序发送到C程序通过TCP / IP的浮点数)?

-Adam

解决方案

http://en.wikipedia.org/wiki/IEEE_754-1985 =noreferrer>关于IEEE 754的维基百科文章很好地展示了在大多数系统上如何存储浮点数。现在,这里有一些常见的陷阱:


  • 最大的一点是,你几乎不想比较两个浮点数的相等(或不等式)。您将希望使用大于/小于比较。

  • 对浮点数执行的操作越多,舍入误差就越显着。

  • 精确度受分数大小的限制,因此您可能无法正确添加相隔几个数量级的数字。 (例如,您将无法将1E-30添加到1E30。)


In follow up to this question, it appears that some numbers cannot be represented by floating point at all, and instead are approximated.

How are floating point numbers stored?

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?

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)?

-Adam

解决方案

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:

  • The biggest is that you almost never want to compare two floating point numbers for equality (or inequality). You'll want to use greater than/less than comparisons instead.
  • The more operations you do on a floating point number, the more significant rounding errors can become.
  • Precision is limited by the size of the fraction, so you may not be able to correctly add numbers that are separated by several orders of magnitude. (For example, you won't be able to add 1E-30 to 1E30.)

这篇关于如何存储浮点数?什么时候重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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