C语言 初始化(在 C 中)时 0.0f 的意义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5199338/
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 07:58:16  来源:igfitidea点击:

What is the significance of 0.0f when initializing (in C)?

c

提问by O_O

I've seen code where people initialize float variables like this:

我见过人们像这样初始化浮点变量的代码:

float num = 0.0f;

Is there a significant difference between this and just doing the following below?

这与仅执行以下操作之间有显着差异吗?

float num = 0; 

Thanks.. :)

谢谢.. :)

回答by EvilTeach

float x = 0has an implicit typecast from int to float.
float x = 0.0fdoes not have such a typecast.
float x = 0.0has an implicit typecast from double to float.

float x = 0具有从 int 到 float 的隐式类型转换。
float x = 0.0f没有这样的类型转换。
float x = 0.0具有从 double 到 float 的隐式类型转换。

Depending on the compiler, implicit typecast can require the compiler to generate extra code.

根据编译器的不同,隐式类型转换可能需要编译器生成额外的代码。

回答by Steve Jessop

Probably the reason is that they once wrote something like:

可能是因为他们曾经写过类似的东西:

float f = 1 / i; // i an integer

Having debugged that, they swore always to decorate literals sufficiently to get the right type:

调试之后,他们发誓总是充分修饰文字以获得正确的类型:

float f = 1.0f / i;

In this case, the .0is to ensure that the division is floating-point, not integer division. The fis because there's no need for the operation to be done in double-- I expect there's more chance of an implementation where 1.0/iwould be significantly slower for no benefit (software float emulation, indifferent optimization), than one where 1.0fis significantly slower for no benefit (if double is faster than float that's because you have fp hardware, so conversion between the two will be very fast, so not introduce significantslowdown).

在这种情况下,.0是为了确保除法是浮点数,而不是整数除法。这f是因为不需要在其中完成操作double- 我希望有更多的机会在1.0/i没有任何好处的情况下(软件浮点仿真,无所谓的优化)1.0f会显着变慢,而不是在没有好处的情况下显着变慢(如果 double 比 float 快,那是因为你有 fp 硬件,所以两者之间的转换会非常快,所以不会引入显着的减速)。

One you've got into the habit of decorating literals, you might well write:

一个你已经养成装饰文字的习惯,你可能会写:

float f = 0.0f;

even though it has exactly the same effect as float f = 0.0;or float f = 0;.

即使它与float f = 0.0;或具有完全相同的效果float f = 0;

Of course the author might not have gone through this revelation personally, they might just have inherited the style of someone else who did.

当然,作者可能没有亲自经历过这个启示,他们可能只是继承了别人经历过的风格。

I'd just write 0.

我只想写0

R.. points out in a comment an another answer that writing 0 also has the benefit that when you change the type of fin future, you don't have to update the literal to match. And if the assignment is separate from the definition, then changing:

R.. 在评论中指出另一个答案,写 0 也有好处,当您f将来更改类型时,您不必更新文字以匹配。如果分配与定义分开,则更改:

float f = something
// some time later
f = 0.1f;

to:

到:

double f = something;
// some time later
f = 0.1f;

is probably a bug. Better to use 0.1and let the compiler truncate to float if necessary. You could probably argue that using floatat all is an optimization, for space if not for time, and the burden of dealing with any differences between float and double should be counted as a developer cost of performing that optimization.

可能是一个错误。最好使用0.1并让编译器在必要时截断为浮动。您可能会争辩说,使用float完全是一种优化,如果不是为了时间,也是为了空间,并且处理 float 和 double 之间的任何差异的负担应该算作执行该优化的开发人员成本。

回答by Paul R

It's just considered good practice to initialise a variable with a literal constant of the same type. In this case you have a float variable and you should initialise it with a float literal constant, i.e. 0.0f, rather than an int (0) which is then implicitly cast to a float.

使用相同类型的文字常量初始化变量被认为是一种很好的做法。在这种情况下,您有一个浮点变量,您应该使用浮点文字常量来初始化它,即0.0f,而不是一个 int ( 0) 然后隐式转换为浮点数。

回答by Ilya Kogan

Well, strictly speaking, 0 is an integer, so float num = 0requires a casting from integer to float. But I suppose the compiler does this for you anyway. I guess people use 0.0fin order to emphasize that this is a float, so nobody mistakes it for an integer.

好吧,严格来说,0 是一个整数,因此float num = 0需要从整数转换为浮点数。但我想编译器无论如何都会为你做这件事。我猜人们使用它0.0f是为了强调这是一个浮点数,所以没有人将它误认为是一个整数。

回答by winwaed

Paul R has written the answer. Your second example has an integer initialization value.

保罗 R 写了答案。你的第二个例子有一个整数初始化值。

You should always use an initializer of the same type as the variable that is being initialized. This avoids any conversion at compile time (ideally) or run time (lazy compilers: are any this lazy, though?). And perhaps more importantly, conversion can lead to some strange things in the general case.

您应该始终使用与正在初始化的变量相同类型的初始化程序。这避免了在编译时(理想情况下)或运行时(懒惰的编译器:有这么懒惰的人吗?)的任何转换。也许更重要的是,在一般情况下,转换可能会导致一些奇怪的事情。

Here conversion should do exactly what is expected, but it is still good style and avoids a compiler warning.

这里的转换应该完全符合预期,但它仍然是很好的风格并且避免了编译器警告。

回答by PyRick

'f' indicates that you want a float :

'f' 表示你想要一个浮点数:

0 is an int

0 是一个整数

0f is a float

0f 是一个浮点数

0.0 is a double

0.0 是双数

0.0f is a float

0.0f 是一个浮点数

回答by Punit Soni

I don't see any reason to use this for initialization process. But, for operations involving floating point literals, this would be useful. For example;

我看不出有任何理由在初始化过程中使用它。但是,对于涉及浮点文字的操作,这会很有用。例如;

float a=0.43, b;
b = 0.5*a + 2.56*a*a;

Floating point literals without a suffix are considered doubles. So, for this computation, "a" will be type-casted to double and the final answer of RHS evaluation will be a double. During the assignment, the double value of RHS is casted to float and assigned to "b". This would degrade performance if the machine does not have double precision FPU. To avoid this and use float for entire computation. suffixes are used. For example,

没有后缀的浮点文字被认为是双精度的。因此,对于此计算,“a”将被强制转换为 double,并且 RHS 评估的最终答案将是 double。在赋值过程中,RHS 的 double 值被强制转换为 float 并赋值给“b”。如果机器没有双精度 FPU,这会降低性能。为了避免这种情况并在整个计算中使用浮点数。使用后缀。例如,

float a=0.43, b;
b = 0.5f*a + 2.56f*a*a;