我可以在C中对一个整数进行比较并添加浮点数吗?

时间:2022-02-27 16:30:36

Can I compare a floating-point number to an integer?

我能把浮点数与整数比较吗?

Will the float compare to integers in code?

浮点数与代码中的整数比较吗?

float f;     // f has a saved predetermined floating-point value to it  
if (f >=100){__asm__reset...etc}

Also, could I...

另外,我可以……

float f;
int x = 100;
x+=f;

I have to use the floating point value f received from an attitude reference system to adjust a position value x that controls a PWM signal to correct for attitude.

我必须使用从姿态参考系统接收到的浮点值f来调整控制PWM信号的位置值x,以校正姿态。

9 个解决方案

#1


19  

The first one will work fine. 100 will be converted to a float, and IEE754 can represent all integers exactly as floats, up to about 223.

第一个可以正常工作。100将被转换为浮点数,IEE754可以将所有整数表示为浮点数,最多可达223。

The second one will also work but will be converted into an integer first, so you'll lose precision (that's unavoidable if you're turning floats into integers).

第二个也可以工作,但首先会被转换成一个整数,因此您将失去精度(如果将浮点数转换为整数,这是不可避免的)。

#2


10  

Since you've identified yourself as unfamiliar with the subtleties of floating point numbers, I'll refer you to this fine paper by David Goldberg: What Every Computer Scientist Should Know About Floating-Point Arithmetic (reprint at Sun).

既然你对浮点数的微妙之处并不熟悉,我将向你推荐大卫·戈德堡的这篇文章:每一个计算机科学家都应该了解的浮点算术(在太阳下重印)。

After you've been scared by that, the reality is that most of the time floating point is a huge boon to getting calculations done. And modern compilers and languages (including C) handle conversions sensibly so that you don't have to worry about them. Unless you do.

在你对此感到害怕之后,现实是大多数时候浮点数对完成计算是一个巨大的好处。现代的编译器和语言(包括C语言)能够合理地处理转换,因此您不必担心它们。除非你做的。

The points raised about precision are certainly valid. An IEEE float effectively has only 24 bits of precision, which is less than a 32-bit integer. Use of double for intermediate calculations will push all rounding and precision loss out to the conversion back to float or int.

关于精度提出的观点当然是正确的。IEEE浮点数实际上只有24位精度,小于32位整数。使用double进行中间计算将把所有的四舍五入和精度损失转换回浮点或int。

#3


8  

Mixed-mode arithmetic (arithmetic between operands of different types and/or sizes) is legal but fragile. The C standard defines rules for type promotion in order to convert the operands to a common representation. Automatic type promotion allows the compiler to do something sensible for mixed-mode operations, but "sensible" does not necessarily mean "correct."

混合模式算法(不同类型和/或大小的操作数之间的算术)合法但脆弱。C标准定义了类型提升的规则,以便将操作数转换为公共表示。自动类型提升允许编译器对混合模式操作执行一些合理的操作,但是“合理”并不一定意味着“正确”。

To really know whether or not the behavior is correct you must first understand the rules for promotion and then understand the representation of the data types. In very general terms:

要真正了解行为是否正确,您必须首先了解推广的规则,然后了解数据类型的表示。在一般条款:

  • shorter types are converted to longer types (float to double, short to int, etc.)
  • 更短的类型被转换为更长的类型(浮动为双,短为int,等等)
  • integer types are converted to floating-point types
  • 整数类型被转换为浮点类型
  • signed/unsigned conversions favor avoiding data loss (whether signed is converted to unsigned or vice-versa depends on the size of the respective types)
  • 有符号/无符号转换有利于避免数据丢失(有符号的转换为无符号的转换还是相反,这取决于各自类型的大小)

Whether code like x > y (where x and y have different types) is right or wrong depends on the values that x and y can take. In my experience it's common practice to prohibit (via the coding standard) implicit type conversions. The programmer must consider the context and explicitly perform any type conversions necessary.

代码是否像x > y (x和y有不同的类型)是正确的还是错误的取决于x和y所能取的值。根据我的经验,通常的做法是禁止(通过编码标准)隐式类型转换。程序员必须考虑上下文并显式地执行任何类型转换。

#4


2  

Can you compare a float and an integer, sure. But the problem you will run into is precision. On most C/C++ implementations, float and int have the same size (4 bytes) and wildly different precision levels. Neither type can hold all values of the other type. Since one type cannot be converted to the other type without loss of precision and the types cannot be native compared, doing a comparison without considering another type will result in precision loss in some scenarios.

你能比较一个浮点数和一个整数吗?但是您将遇到的问题是精度。在大多数C/ c++实现中,float和int具有相同的大小(4字节)和非常不同的精度级别。两种类型都不能保存其他类型的所有值。由于一种类型不能被转换为另一种类型而不损失精度,而且类型不能被本地比较,所以在不考虑另一种类型的情况下进行比较会导致某些场景的精度损失。

What you can do to avoid precision loss is to convert both types to a type which has enough precision to represent all values of float and int. On most systems, double will do just that. So the following usually does a non-lossy comparison

为了避免精度损失,您可以将这两种类型转换为具有足够的精度来表示浮点数和整数的所有值的类型。下面的例子通常是一个无损耗的比较

float f = getSomeFloat();
int i = getSomeInt();
if ( (double)i == (double)f ) { 
   ...
}

#5


1  

LHS defines the precision, So if your LHS is int and RHS is float, then this results in loss of precision.

LHS定义了精度,所以如果LHS是int型的,RHS是浮动的,那么这将导致精度损失。

Also take a look at FP related CFAQ

也看看FP相关的CFAQ

#6


1  

Yes, you can compare them, you can do math on them without terribly much regard for which is which, in most cases. But only most. The big bugaboo is that you can check for f<i etc. but should not check for f==i. An integer and a float that 'should' be identical in value are not necessarily identical.

是的,你可以对它们进行比较,你可以对它们进行数学运算而不太在意在大多数情况下,哪个是哪个。但只有最多。最大的问题是你可以检查f 等等,但是不应该检查f=>

#7


0  

Yeah, it'll work fine. Specifically, the int will be converted to float for the purposes of the conversion. In the second one you'll need to cast to int but it should be fine otherwise.

是的,它会正常工作。具体地说,为了转换的目的,int将被转换为float。在第二个例子中,您需要将属性转换为int类型,否则应该没问题。

#8


0  

Yes, and sometimes it'll do exactly what you expect.

是的,有时它会做你所期望的事情。

As the others have pointed out, comparing, eg, 1.0 == 1 will work out, because the integer 1 is type cast to double (not float) before the comparison.

正如其他人所指出的,比较(例如,1.0 == 1)将是可行的,因为在比较之前,整数1的类型转换为double(而不是float)。

However, other comparisons may not.

然而,其他的比较可能不会。

#9


0  

About that, the notation 1.0 is of type double so the comparison is made in double by type promotion rules like said before. 1.f or 1.0f is of type float and the comparison would have been made in float. And it would have worked as well since we said that 2^23 first integers are representible in a float.

关于这一点,符号1.0的类型是double,所以比较是用double进行的,按类型升级规则进行,如前所述。1。f或1.0f类型为float,比较应该是float类型。和它将一直以来我们首先说2 ^ 23整数representible浮动。

#1


19  

The first one will work fine. 100 will be converted to a float, and IEE754 can represent all integers exactly as floats, up to about 223.

第一个可以正常工作。100将被转换为浮点数,IEE754可以将所有整数表示为浮点数,最多可达223。

The second one will also work but will be converted into an integer first, so you'll lose precision (that's unavoidable if you're turning floats into integers).

第二个也可以工作,但首先会被转换成一个整数,因此您将失去精度(如果将浮点数转换为整数,这是不可避免的)。

#2


10  

Since you've identified yourself as unfamiliar with the subtleties of floating point numbers, I'll refer you to this fine paper by David Goldberg: What Every Computer Scientist Should Know About Floating-Point Arithmetic (reprint at Sun).

既然你对浮点数的微妙之处并不熟悉,我将向你推荐大卫·戈德堡的这篇文章:每一个计算机科学家都应该了解的浮点算术(在太阳下重印)。

After you've been scared by that, the reality is that most of the time floating point is a huge boon to getting calculations done. And modern compilers and languages (including C) handle conversions sensibly so that you don't have to worry about them. Unless you do.

在你对此感到害怕之后,现实是大多数时候浮点数对完成计算是一个巨大的好处。现代的编译器和语言(包括C语言)能够合理地处理转换,因此您不必担心它们。除非你做的。

The points raised about precision are certainly valid. An IEEE float effectively has only 24 bits of precision, which is less than a 32-bit integer. Use of double for intermediate calculations will push all rounding and precision loss out to the conversion back to float or int.

关于精度提出的观点当然是正确的。IEEE浮点数实际上只有24位精度,小于32位整数。使用double进行中间计算将把所有的四舍五入和精度损失转换回浮点或int。

#3


8  

Mixed-mode arithmetic (arithmetic between operands of different types and/or sizes) is legal but fragile. The C standard defines rules for type promotion in order to convert the operands to a common representation. Automatic type promotion allows the compiler to do something sensible for mixed-mode operations, but "sensible" does not necessarily mean "correct."

混合模式算法(不同类型和/或大小的操作数之间的算术)合法但脆弱。C标准定义了类型提升的规则,以便将操作数转换为公共表示。自动类型提升允许编译器对混合模式操作执行一些合理的操作,但是“合理”并不一定意味着“正确”。

To really know whether or not the behavior is correct you must first understand the rules for promotion and then understand the representation of the data types. In very general terms:

要真正了解行为是否正确,您必须首先了解推广的规则,然后了解数据类型的表示。在一般条款:

  • shorter types are converted to longer types (float to double, short to int, etc.)
  • 更短的类型被转换为更长的类型(浮动为双,短为int,等等)
  • integer types are converted to floating-point types
  • 整数类型被转换为浮点类型
  • signed/unsigned conversions favor avoiding data loss (whether signed is converted to unsigned or vice-versa depends on the size of the respective types)
  • 有符号/无符号转换有利于避免数据丢失(有符号的转换为无符号的转换还是相反,这取决于各自类型的大小)

Whether code like x > y (where x and y have different types) is right or wrong depends on the values that x and y can take. In my experience it's common practice to prohibit (via the coding standard) implicit type conversions. The programmer must consider the context and explicitly perform any type conversions necessary.

代码是否像x > y (x和y有不同的类型)是正确的还是错误的取决于x和y所能取的值。根据我的经验,通常的做法是禁止(通过编码标准)隐式类型转换。程序员必须考虑上下文并显式地执行任何类型转换。

#4


2  

Can you compare a float and an integer, sure. But the problem you will run into is precision. On most C/C++ implementations, float and int have the same size (4 bytes) and wildly different precision levels. Neither type can hold all values of the other type. Since one type cannot be converted to the other type without loss of precision and the types cannot be native compared, doing a comparison without considering another type will result in precision loss in some scenarios.

你能比较一个浮点数和一个整数吗?但是您将遇到的问题是精度。在大多数C/ c++实现中,float和int具有相同的大小(4字节)和非常不同的精度级别。两种类型都不能保存其他类型的所有值。由于一种类型不能被转换为另一种类型而不损失精度,而且类型不能被本地比较,所以在不考虑另一种类型的情况下进行比较会导致某些场景的精度损失。

What you can do to avoid precision loss is to convert both types to a type which has enough precision to represent all values of float and int. On most systems, double will do just that. So the following usually does a non-lossy comparison

为了避免精度损失,您可以将这两种类型转换为具有足够的精度来表示浮点数和整数的所有值的类型。下面的例子通常是一个无损耗的比较

float f = getSomeFloat();
int i = getSomeInt();
if ( (double)i == (double)f ) { 
   ...
}

#5


1  

LHS defines the precision, So if your LHS is int and RHS is float, then this results in loss of precision.

LHS定义了精度,所以如果LHS是int型的,RHS是浮动的,那么这将导致精度损失。

Also take a look at FP related CFAQ

也看看FP相关的CFAQ

#6


1  

Yes, you can compare them, you can do math on them without terribly much regard for which is which, in most cases. But only most. The big bugaboo is that you can check for f<i etc. but should not check for f==i. An integer and a float that 'should' be identical in value are not necessarily identical.

是的,你可以对它们进行比较,你可以对它们进行数学运算而不太在意在大多数情况下,哪个是哪个。但只有最多。最大的问题是你可以检查f 等等,但是不应该检查f=>

#7


0  

Yeah, it'll work fine. Specifically, the int will be converted to float for the purposes of the conversion. In the second one you'll need to cast to int but it should be fine otherwise.

是的,它会正常工作。具体地说,为了转换的目的,int将被转换为float。在第二个例子中,您需要将属性转换为int类型,否则应该没问题。

#8


0  

Yes, and sometimes it'll do exactly what you expect.

是的,有时它会做你所期望的事情。

As the others have pointed out, comparing, eg, 1.0 == 1 will work out, because the integer 1 is type cast to double (not float) before the comparison.

正如其他人所指出的,比较(例如,1.0 == 1)将是可行的,因为在比较之前,整数1的类型转换为double(而不是float)。

However, other comparisons may not.

然而,其他的比较可能不会。

#9


0  

About that, the notation 1.0 is of type double so the comparison is made in double by type promotion rules like said before. 1.f or 1.0f is of type float and the comparison would have been made in float. And it would have worked as well since we said that 2^23 first integers are representible in a float.

关于这一点,符号1.0的类型是double,所以比较是用double进行的,按类型升级规则进行,如前所述。1。f或1.0f类型为float,比较应该是float类型。和它将一直以来我们首先说2 ^ 23整数representible浮动。