20

我可以将浮点数与整数进行比较吗?

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

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

还有,我能不能...

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

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

4

9 回答 9

21

第一个会正常工作。100 将被转换为浮点数,IEE754 可以将所有整数完全表示为浮点数,最多约为 2 23

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

于 2009-04-17T04:30:53.070 回答
11

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.

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.

于 2009-04-17T07:28:33.860 回答
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."

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.)
  • 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.

于 2009-04-17T18:19:50.253 回答
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.

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 ) { 
   ...
}
于 2009-04-17T05:49:46.487 回答
1

LHS 定义了精度,所以如果你的 LHS 是 int 而 RHS 是 float,那么这会导致精度损失。

还可以看看 FP 相关的CFAQ

于 2009-04-17T04:35:35.667 回答
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.

于 2009-04-17T05:40:20.143 回答
0

是的,它会正常工作的。具体来说,为了转换的目的,int 将被转换为 float。在第二个中,您需要强制转换为 int 但否则应该没问题。

于 2009-04-17T04:26:43.067 回答
0

是的,有时它会完全符合您的预期。

正如其他人所指出的那样,比较例如 1.0 == 1 会起作用,因为在比较之前整数 1 是类型转换为double(not float) 的。

但是,其他比较可能不会。

于 2009-04-17T05:32:57.010 回答
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.

于 2010-10-28T08:41:22.647 回答