0

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?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Adam Lee
  • 23,314
  • 47
  • 144
  • 221
  • possible duplicate of [What is the significance of 0.0f when initializing (in C)?](http://stackoverflow.com/questions/5199338/what-is-the-significance-of-0-0f-when-initializing-in-c) – user1810087 Mar 22 '14 at 07:52

2 Answers2

1

It doesn't matter at all. You could also say float a = 0; and again it would be the same thing. Or float a = float();. I think the most conventional would be 0, 0.0, or 0f; the rest are just redundant.

John Zwinck
  • 223,042
  • 33
  • 293
  • 407
0

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.

rubenvb
  • 72,003
  • 32
  • 177
  • 319
  • The bits in a float initialized from a double 0 might differ from one directly from a float 0? How? IEEE 754 has only two ways to represent zero as far as I know, and one is -0 which neither of the above will give you. – John Zwinck Mar 22 '14 at 06:54