浮点数到底能不能用 == 进行比较
2013-02-25 11:46
211 查看
今天看c语言,遇到一个问题,浮点类型能不能用 == 进行比较,其实这个问题以前也碰到过,在面试的时候,只知道尽量不要使用 == 对浮点数进行比较。那么为什么呢,计算机中的计算不是确定的么?
首先我们要了解浮点数在计算机中怎么存放的。《深入理解计算机系统》中这样说过,浮点数普遍的作为实数运算的近似值的计算,是很有用的。这里说的是实数的近似值的计算,所以浮点数在计算机中其实是一种不精确的表示。它存在舍入(rounding)误差。IEEE浮点标准用符号,尾数和阶码将浮点数的位表示划分为三个字段,单精度为32位,双精度为64位,因为表示方法限制了浮点数的范围和精度,浮点运算只能近似的表示实数运算。而 == 表示的是在计算机中的内存表示完全一样,这样使用
== 来表示两个浮点数的相等就会出现问题了,比如下面的例子:
when can I use operator '==' to compare two float?
Whenever you want.
A better questsion is "Why shouldn't I use == to compare floats?". The
answer is: rounding. If you're comparing values that are the result of a
mathematical expression that "should" be equal, there's a very good chance
that they won't be.
float x = 1.0f;
float y = 1.0f / 3;
if (3f * y == x)
{
// this code will likely never run
}
On the other hand, if you've assigned a value from one variable of type
float to another variable of type float, then you can safely compare them
for equality:
float x = 1.0f / 3.0f;
float y = x;
if (x == y)
{
// this code will always run
}
Of course, this is an unusual situation - the normal recommendation is to
never compare floats to equality: compare for an acceptably small difference
in value:
if (fabs(x-y) < 0.00001)
{
// ... this will reliably execute when x and y are "equal"
}
上面给出的例子的意思是在数学表达式中,不能用 == 来表示浮点数和一个结果为浮点数的数学表达式相等。
其实我们在判断两个浮点数是否相等时,最好不要用 == 来判断,要使用上面 的最后一个的判断方法,即判断一下这两个数的绝对值,即在数轴上的距离是否小于某个精度。
c++中判断一个浮点数是否为0是通过下面的方法来实现的。
const float EPSINON = 0.000001;
if ((x >= - EPSINON) && (x <= EPSINON))
首先我们要了解浮点数在计算机中怎么存放的。《深入理解计算机系统》中这样说过,浮点数普遍的作为实数运算的近似值的计算,是很有用的。这里说的是实数的近似值的计算,所以浮点数在计算机中其实是一种不精确的表示。它存在舍入(rounding)误差。IEEE浮点标准用符号,尾数和阶码将浮点数的位表示划分为三个字段,单精度为32位,双精度为64位,因为表示方法限制了浮点数的范围和精度,浮点运算只能近似的表示实数运算。而 == 表示的是在计算机中的内存表示完全一样,这样使用
== 来表示两个浮点数的相等就会出现问题了,比如下面的例子:
when can I use operator '==' to compare two float?
Whenever you want.
A better questsion is "Why shouldn't I use == to compare floats?". The
answer is: rounding. If you're comparing values that are the result of a
mathematical expression that "should" be equal, there's a very good chance
that they won't be.
float x = 1.0f;
float y = 1.0f / 3;
if (3f * y == x)
{
// this code will likely never run
}
On the other hand, if you've assigned a value from one variable of type
float to another variable of type float, then you can safely compare them
for equality:
float x = 1.0f / 3.0f;
float y = x;
if (x == y)
{
// this code will always run
}
Of course, this is an unusual situation - the normal recommendation is to
never compare floats to equality: compare for an acceptably small difference
in value:
if (fabs(x-y) < 0.00001)
{
// ... this will reliably execute when x and y are "equal"
}
上面给出的例子的意思是在数学表达式中,不能用 == 来表示浮点数和一个结果为浮点数的数学表达式相等。
其实我们在判断两个浮点数是否相等时,最好不要用 == 来判断,要使用上面 的最后一个的判断方法,即判断一下这两个数的绝对值,即在数轴上的距离是否小于某个精度。
c++中判断一个浮点数是否为0是通过下面的方法来实现的。
const float EPSINON = 0.000001;
if ((x >= - EPSINON) && (x <= EPSINON))
相关文章推荐
- 进行浮点比较
- 设计师到底能不能自己进行可用性测试(下)
- 浮点数不可以直接用“==”和“!=”进行比较
- [将小白进行到底] 如何比较两篇文章的相似度
- C语言中比较两个浮点数是否相等的方法,fabs和abs
- pyton练习,对两个文件夹子下文件名进行比较
- 项目中常用但查询有比较麻烦的数据,怎么进行初始化。
- 定点DSP与浮点DSP的比较(定点DSP和浮点DSP的定义)
- 对300万一张表数据,用游标进行循环,不同写法的效率比较
- 卡巴斯基发布全功能安全软件 誓将收费进行到底
- 对5个数进行排序,需7次比较的方法
- 有两等长数组A,B,所含元素相同,但顺序不同,只能取得A数组某值和B数组某值进行比较,比较结果为大于,小于,等于,但是不能取得同一数组A或者B中两个数进行比较,也不能取得某数组中的某个值
- 将节约进行到底 如何精打细算建机房
- 将反房地产暴利进行到底
- 浮点数比较之我见
- 将忽悠进行到底:智彗星杀毒软件推出2007
- ORM+Framework 完整开发ERP/MIS表单流程 比较下速度到底有多快 分享WinForms开发经验
- DS8880要将双控高端存储架构进行到底
- 将高兴进行到底 推荐
- python与其他语言进行比较