Possible Duplicate:
strange output in comparision of float with float literal可能重复:奇怪的输出与浮动文字的比较
#include<stdio.h>
int main()
{
float me = 1.7;
if(me==1.7)
printf("C");
else
printf("C++");
}
Output: C++
Now the reason for this behaviour is said that many floating point numbers cant be represented with absolute precision in binary.
现在,这种行为的原因是许多浮点数不能用二进制的绝对精度来表示。
My question is that - If computer thinks and manipulates in binary. Any uncertanity in representation of me
would be same for 1.7
when compared. So both should be equal.
我的问题是-如果电脑用二进制来思考和操作。与1.7相比,任何代表我的不确定性都是一样的。所以两者应该相等。
ALso how typecasting solves this problem? (float)1.7
类型化如何解决这个问题?(浮动)1.7
7 个解决方案
#1
13
You are comparing a float to a double. the literal 1.7
is a double.
您正在将浮点数与双精度浮点数进行比较。1。7是2。
You've stored that in a float, which might have less precision than a double, thus the me == 1.7
is comparing 1.7 as a float(promoted to a double) to 1.7 as a double.
您已经将其存储在一个浮点数中,它的精度可能低于double数,因此me = 1.7将1.7作为一个浮点数(提升为double)与1.7作为double数进行比较。
In this case, me == 1.7f
should make them compare as equal, or changing me
to a double double me = 1.7;
在这种情况下,我== 1。7f应该使它们比较相等,或者把我变成一个2倍的i = 1。7;
In the general case though, you'd want equality to be compared using an epsilon as @duffymo shows.
但是,在一般情况下,您希望用@duffymo所示的epsilon来比较等式。
Also, Obligatory read.
同时,必须阅读。
#2
2
Here is some reading material What Every Computer Scientist Should Know About Floating-Point Arithmetic
这里有一些阅读材料,是每个计算机科学家都应该知道的浮点运算
#3
2
The closest representation of 1.7 is different for float and double, so casting to a float should usually result in the same number.
对于浮点数和双精度数,1.7的最接近表示是不同的,因此对浮点数进行强制转换通常会得到相同的结果。
One of the main reasons you can't compare floating point numbers is that identities that work for real numbers and integers don't necessarily work for floating point because of rounding - i.e. (x+y)+z and x+(y+z) can often be different (note that writing them that way will often not change a compiler's behavior, but you can induce the order by doing something the compiler wont optimize around).
的一个主要原因你不能比较浮点数是身份工作的整数和实数不一定因为四舍五入——即为浮点工作。(x + y)+ z、x + y + z)往往是不同的(注意,写他们,通常不会改变一个编译器的行为方式,但是你可以诱导order by做编译器不会优化)。
For instance, (100 - (100 - .0000000001)) != .00000000001 using IEEE-754 doubles, even though math says they should be equal. So the computation that should be producing .00000000001 will be slightly off. This is especially a problem with more complicated calculations, such as linear algebra, where the answer can be the result of thousands of additions and subtractions, each of which can add to floating point rounding error.
例如,(100 -(100 - .0000000001)!= . 0000000000000000001使用IEEE-754双打,即使数学上说它们应该是相等的。因此,应该生成。00000000001的计算会稍微偏离一点。这是一个需要更复杂的计算的问题,比如线性代数,其中的答案可能是数千个加减的结果,每个加减都可能增加浮点舍入误差。
IEEE-754 floating point can be very tricky, if you don't really understand what's going on.
IEEE-754浮点数是非常棘手的,如果你不明白到底是怎么回事的话。
I recommend the excellent "What every Computer Scientist Should Know About Floating-Point Arithmetic": http://docs.sun.com/source/806-3568/ncg_goldberg.html
我推荐优秀的“每个计算机科学家都应该知道的浮点算法”:http://docs.sun.com/source/806-3568/ncg_goldberg.html
#4
2
Assuming an IEEE-754 representation, the literals 1.7f and 1.7 stand for the following values:
假设IEEE-754表示,文字1.7f和1.7代表以下值:
1.7f == 1.7000000476837158203125
1.7 == 1.6999999999999999555910790149937383830547332763671875
Clearly, these are not the same value, and thus they compare as false.
很明显,它们不是相同的值,因此它们比较为假。
#5
1
float me = 1.7f;
浮动我f = 1.7;
#6
0
It is a problem of representation concerning numerical analysis
, since you have a limited amount of bit to represent the number what is represented is not exactly equal to the number you meant, it is just a close approximation to the nearest number representable with the bit you have. Take a read here
这是一个关于数值分析的表示法问题,因为你有有限的位来表示被表示的数字并不完全等于你的意思,它只是一个与你所拥有的位表示的最接近的数字的近似。在这里读
#7
0
The 1.7 is converted automatically to double in the comparison. So you're comparing 1.7 with 1.7d, which probably differ by some (double) machine epsilons.
1.7在比较中自动转换为双倍。所以你将1。7和1。7d进行比较,这可能会有一些(双)机器的。
#1
13
You are comparing a float to a double. the literal 1.7
is a double.
您正在将浮点数与双精度浮点数进行比较。1。7是2。
You've stored that in a float, which might have less precision than a double, thus the me == 1.7
is comparing 1.7 as a float(promoted to a double) to 1.7 as a double.
您已经将其存储在一个浮点数中,它的精度可能低于double数,因此me = 1.7将1.7作为一个浮点数(提升为double)与1.7作为double数进行比较。
In this case, me == 1.7f
should make them compare as equal, or changing me
to a double double me = 1.7;
在这种情况下,我== 1。7f应该使它们比较相等,或者把我变成一个2倍的i = 1。7;
In the general case though, you'd want equality to be compared using an epsilon as @duffymo shows.
但是,在一般情况下,您希望用@duffymo所示的epsilon来比较等式。
Also, Obligatory read.
同时,必须阅读。
#2
2
Here is some reading material What Every Computer Scientist Should Know About Floating-Point Arithmetic
这里有一些阅读材料,是每个计算机科学家都应该知道的浮点运算
#3
2
The closest representation of 1.7 is different for float and double, so casting to a float should usually result in the same number.
对于浮点数和双精度数,1.7的最接近表示是不同的,因此对浮点数进行强制转换通常会得到相同的结果。
One of the main reasons you can't compare floating point numbers is that identities that work for real numbers and integers don't necessarily work for floating point because of rounding - i.e. (x+y)+z and x+(y+z) can often be different (note that writing them that way will often not change a compiler's behavior, but you can induce the order by doing something the compiler wont optimize around).
的一个主要原因你不能比较浮点数是身份工作的整数和实数不一定因为四舍五入——即为浮点工作。(x + y)+ z、x + y + z)往往是不同的(注意,写他们,通常不会改变一个编译器的行为方式,但是你可以诱导order by做编译器不会优化)。
For instance, (100 - (100 - .0000000001)) != .00000000001 using IEEE-754 doubles, even though math says they should be equal. So the computation that should be producing .00000000001 will be slightly off. This is especially a problem with more complicated calculations, such as linear algebra, where the answer can be the result of thousands of additions and subtractions, each of which can add to floating point rounding error.
例如,(100 -(100 - .0000000001)!= . 0000000000000000001使用IEEE-754双打,即使数学上说它们应该是相等的。因此,应该生成。00000000001的计算会稍微偏离一点。这是一个需要更复杂的计算的问题,比如线性代数,其中的答案可能是数千个加减的结果,每个加减都可能增加浮点舍入误差。
IEEE-754 floating point can be very tricky, if you don't really understand what's going on.
IEEE-754浮点数是非常棘手的,如果你不明白到底是怎么回事的话。
I recommend the excellent "What every Computer Scientist Should Know About Floating-Point Arithmetic": http://docs.sun.com/source/806-3568/ncg_goldberg.html
我推荐优秀的“每个计算机科学家都应该知道的浮点算法”:http://docs.sun.com/source/806-3568/ncg_goldberg.html
#4
2
Assuming an IEEE-754 representation, the literals 1.7f and 1.7 stand for the following values:
假设IEEE-754表示,文字1.7f和1.7代表以下值:
1.7f == 1.7000000476837158203125
1.7 == 1.6999999999999999555910790149937383830547332763671875
Clearly, these are not the same value, and thus they compare as false.
很明显,它们不是相同的值,因此它们比较为假。
#5
1
float me = 1.7f;
浮动我f = 1.7;
#6
0
It is a problem of representation concerning numerical analysis
, since you have a limited amount of bit to represent the number what is represented is not exactly equal to the number you meant, it is just a close approximation to the nearest number representable with the bit you have. Take a read here
这是一个关于数值分析的表示法问题,因为你有有限的位来表示被表示的数字并不完全等于你的意思,它只是一个与你所拥有的位表示的最接近的数字的近似。在这里读
#7
0
The 1.7 is converted automatically to double in the comparison. So you're comparing 1.7 with 1.7d, which probably differ by some (double) machine epsilons.
1.7在比较中自动转换为双倍。所以你将1。7和1。7d进行比较,这可能会有一些(双)机器的。