简体   繁体   English

(浮动)在C / C ++中

[英](float) in c / c++

Trying to find out what the (float) in this function means: 尝试找出此函数中的(浮点数)是什么意思:

static float avgEndpoints (int i, int stride, float *fa)
{
    return ((float) (fa[i-stride] +
                 fa[i+stride]) * .5f);
}

The reason I'm confused is the function already returns floating point (or appears to), so what is the (float) doing there? 我感到困惑的原因是该函数已经返回了浮点数(或似乎返回了浮点数),那么那里的(浮点数)在做什么呢?

I'm attempting to port this program to another language and learn a bit of c/c++ at the same time. 我试图将此程序移植到另一种语言,并同时学习一些c / c ++。

Thanks 谢谢

The (float) is a cast, which in this example casts the expression (fa[i-stride] + fa[i+stride]) to floating point. (float)是强制转换,在此示例中将表达式(fa[i-stride] + fa[i+stride])强制转换为浮点。

In this case the cast is redundant and not required (as the expression will be a float anyway due to the *.5f ) 在这种情况下, *.5f是多余的并且不是必需的(由于*.5f无论如何该表达式都是浮点数)

I think it is useless in this context. 我认为在这种情况下是没有用的。 The intent was to have/cast return value as float, but 目的是让/将返回值设置为float,但是

(fa[i-stride] + fa[i+stride]) * .5f

is already a float so, don't see any reason for another (float) which is redundant. 已经是浮点数,因此,看不到任何多余的原因(float)

Looks to me like it's redundant; 在我看来,这是多余的; you don't really need it there, but you might have if for some reason the compiler was trying to make that a double or something, which it would have done if it read ".5" instead of ".5f". 您实际上并不需要它,但是如果由于某种原因,编译器试图将其设为double或其他值,则可能会有,如果它读为“ .5”而不是“ .5f”,则可能会这样做。

You can safely remove it without changing anything. 您可以安全地删除它,而无需进行任何更改。

That "(float)" is called a cast. 那个“(浮动)”被称为演员表。 It forces the compiler to treat the result that follows as a float regardless of what it would otherwise be. 它将强制编译器将其后的结果视为浮点数,而不管其结果如何。 For more information see this wiki page . 有关更多信息,请参见此Wiki页面

It is a redundant cast, just as the outer parenthesis is redundant. 就像外部括号是多余的一样,它是多余的转换。 I would too ask the programmer who has written that code why they are doing what they do. 我也会问那些编写该代码的程序员为什么他们要做什么。 It appears that they were not sure how the C language works. 看来他们不确定C语言如何工作。

Completely equivalent but clearer code: 完全等效但更清晰的代码:

return (fa[i-stride] + fa[i+stride]) * .5f;

This code 这段代码

(float) is a cast, which will force the expression fa[i-stride] + fa[i+stride] to be converted to a float . (float)是强制转换,将强制将fa[i-stride] + fa[i+stride]表达式转换为float

For this code, this is a case of throwing in an unnecessary extra cast - just like throwing in an extra pair of parentheses (which this code also does). 对于此代码,这是抛出不必要的额外强制转换的情况-就像抛出一对额外的括号一样(此代码也是如此)。

I'd recommend removing extra unneeded parentheses, and learning the C++ standard order of operations/type promotions. 我建议删除多余的括号,并学习操作/类型提升的C ++标准顺序。

Cases where you'd need a cast 您需要演员表的情况

If that fa[i-stride] + fa[i+stride] expression returned a double instead, then there may be a subtle floating-point difference if you remove the cast. 如果该fa[i-stride] + fa[i+stride]表达式返回一个double ,那么如果删除强制转换,则可能会有细微的浮点数差异。

Also if fa[i-stride] + fa[i+stride] returned an integral type, then it is sometimes possible you'd need to cast to float to ensure that the answer wouldn't be integral-truncated. 同样,如果fa[i-stride] + fa[i+stride]返回整数类型,那么有时可能需要强制转换为float形式,以确保答案不会被整数截断。

However, the C++ standard's order of operations/type promotion determines whether this will happen or not. 但是,C ++标准的操作/类型提升顺序确定是否会发生这种情况。 If those rules make the cast unnecessary, then you can (and probably should) take them out. 如果这些规则使演员表变得不必要,那么您可以(可能应该)将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM