C语言 float 和 double 变量的比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3988821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:46:11  来源:igfitidea点击:

Comparison of float and double variables

ccomparisonfloating-pointdouble

提问by Chetan Sharma

Possible Duplicates:
Difference between float and double
strange output in comparision of float with float literal

可能的重复:
float 和 double奇怪输出之间的差异与 float 和
float 文字的比较

I am using visual C++ 6.0 and in a program I am comparing float and double variables For example for this program

我正在使用 Visual C++ 6.0 并且在一个程序中我正在比较 float 和 double 变量例如对于这个程序

#include<stdio.h>
int main()  
{    
    float a = 0.7f;
    double b = 0.7; 
    printf("%d %d %d",a<b,a>b,a==b);
    return 0;
 }  

I am getting 1 0 0 as output

我得到 1 0 0 作为输出

and for

并为

#include<stdio.h>
int main()  
{    
    float a = 1.7f;
    double b = 1.7; 
    printf("%d %d %d",a<b,a>b,a==b);
    return 0;
 }  

I am getting 0 1 0 as output.

我得到 0 1 0 作为输出。

Please tell me why I am getting these weird output and is there any way to predict these outputs on the same processor. Also how comparison is done of two variables in C ?

请告诉我为什么我会得到这些奇怪的输出,有没有办法在同一个处理器上预测这些输出。另外如何比较 C 中的两个变量?

回答by Starkey

It has to do with the way the internal representation of floats and doubles are in the computer. Computers store numbers in binary which is base 2. Base 10 numbers when stored in binary may have repeating digits and the "exact" value stored in the computer is not the same.

它与浮点数和双精度数在计算机中的内部表示方式有关。计算机以二进制形式存储数字,即以 2 为基数。以二进制形式存储的 10 进制数字可能具有重复的数字,并且计算机中存储的“确切”值并不相同。

When you compare floats, it's common to use an epsilon to denote a small change in values. For example:

当您比较浮点数时,通常使用 epsilon 来表示值的微小变化。例如:

float epsilon = 0.000000001;
float a = 0.7;
double b = 0.7;

if (abs(a - b) < epsilon)
  // they are close enough to be equal.

回答by Jon Skeet

1.7d and 1.7f are very likely to be different values: one is the closest you can get to the absolute value 1.7 in a double representation, and one is the closest you can get to the absolute value 1.7 in a float representation.

1.7d 和 1.7f 很可能是不同的值:一个是在双精度表示中可以得到的最接近绝对值 1.7 的值,一个是在浮点表示中可以得到的与绝对值 1.7 最接近的值。

To put it into simpler-to-understand terms, imagine that you had two types, shortDecimaland longDecimal. shortDecimalis a decimal value with 3 significant digits. longDecimalis a decimal value with 5 significant digits. Now imagine you had some way of representing pi in a program, and assigning the value to shortDecimaland longDecimalvariables. The short value would be 3.14, and the long value would be 3.1416. The two values aren't the same, even though they're both the closest representable value to pi in their respective types.

用更容易理解的术语来说,假设您有两种类型,shortDecimallongDecimal. shortDecimal是具有 3 位有效数字的十进制值。longDecimal是具有 5 位有效数字的十进制值。现在想象你有某种方式在程序中表示 pi,并将值分配给shortDecimallongDecimal变量。空头值为 3.14,多头值为 3.1416。这两个值并不相同,即使它们都是各自类型中最接近 pi 的可表示值。

回答by Benoit

1.7 is decimal. In binary, it has non-finite representation.

1.7 是十进制。在二进制中,它具有非有限表示。

Therefore, 1.7 and 1.7f differ.

因此,1.7 和 1.7f 不同。

Heuristic proof: when you shift bits to the left (ie multiply by 2) it will in the end be an integer if ever the binary representation is “finite”.

启发式证明:当你向左移动位(即乘以 2)时,如果二进制表示是“有限的”,它最终将是一个整数。

But in decimal, multiply 1.7 by 2, and again: you will only obtain non-integers (decimal part will cycle between .4, .8, .6and .2). Therefore 1.7 is not a sum of powers of 2.

但在小数乘以2 1.7,并再次:你只会得到非整数(小数之间的部分将循环.4.8.6.2)。因此,1.7 不是 2 的幂之和。

回答by Andrey

You can't compare floating point variables for equality. The reason is that decimal fractions are represented as binary ones, that means loss of precision.

您不能比较浮点变量是否相等。原因是十进制小数表示为二进制小数,这意味着精度损失。