简体   繁体   English

下面的float初始化之间有什么区别?

[英]What is the difference between the following initialization for float?

Initialization of float can be done as follows, 浮点数的初始化可以如下进行:

float a = 0.0
float a = 0.f
float a = float(0)

Is there any pros and cons to use any of these? 使用这些方法有什么利弊吗?

It doesn't matter at all. 没关系。 You could also say float a = 0; 您也可以说float a = 0; and again it would be the same thing. 再次,这将是同一件事。 Or float a = float(); 或者float a = float(); . I think the most conventional would be 0 , 0.0 , or 0f ; 我认为,最常规的是00.0 ,或0f ; the rest are just redundant. 其余的只是多余的。

The first initializes from a double literal, whereas the second is from a float literal. 第一个从双精度字面量初始化,而第二个从浮点型字面量初始化。 The bits in the two zeros may not be the same. 两个零中的位可能不相同。

The third is c++ constructor-like syntax that actually just does direct initialization, in this case from an integer literal. 第三种是类似于c ++构造函数的语法,实际上只是直接进行初始化,在这种情况下,是从整数文字开始的。

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

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