Should I initialize floats using 0.f?

When I initialize float variables in my program, I usually have vectors such as:

 Vector forward(0.f,0.f,-1.f),right(1.f,0.f,0.f),up(0.f,1.f,0.f) 

(Vectors are only 3 floats, such as struct Vector{ float x,y,z; }; )

It looks a lot easier:

 Vector forward(0,0,-1),right(1,0,0),up(0,1,0) 

Should I initialize float variables using float s? Am I losing something or taking some kind of punishment when I use integers (or double s) to initialize a float ?

+8
c ++ variables initialization
source share
3 answers

There is no semantic difference between the two. However, depending on some compilers, additional code may be generated. See also this and this SO questions on the same topic.

I can confirm that gcc generates the same code for all options

 int main() { float a = 0.0f; /* or 0 or 0.0 */ return 0; } 

and what is this code

  .file "1.c" .text .globl main .type main, @function main: .LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl $0x00000000, %eax movl %eax, -4(%rbp) movl $0, %eax popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE0: .size main, .-main .ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3" .section .note.GNU-stack,"",@progbits 

Matching line

  movl $0x00000000, %eax 

Changing a to 0.1 (or 0.1f ) changes the string to

  movl $0x3dcccccd, %eax 

It seems that gcc might output the correct constant and not generate additional code.

+7
source share

For a single literal constant, this does not matter. In the context of the initializer, a constant of any numeric type will be implicitly converted to the type of the object being initialized. This is guaranteed by the locale. So, all:

 float x = 0; float x = 0.0; float x = 0.0f; float x = 0.0L; // converted from long double to float 

are equally valid and lead to the fact that the same value is stored in x .

A literal constant in a more complex expression may have unexpected results.

In most cases, each expression is evaluated on its own, regardless of the context in which it appears. Any implicit conversion is applied after evaluating the subexpression.

So if you write:

 float x = 1 / 2; 

expression 1 / 2 will be evaluated as int , which will give 0 , which is then converted to float. It will set float. It will set x to 0.0f , not to 0.5f`.

I think you should be safe using unsuffixed floating point constants (which are of type double ).

By the way, you can use double instead of float in your program. double , as I mentioned, is a type of an undefined floating point constant and in a sense can be considered a default floating point type. It usually has a greater range and accuracy than float , and usually there are not many differences in performance.

+3
source share

Good programming practice has always been to always write 0.f, 1.f, etc., even if gcc can often figure out what programmer 1.0 means, etc.

Problematic cases are not so much the trivial initializations of floating-point variables, but numerical constants in somewhat more complex formulas, where a combination of operators, floating-point variables, and these constants can easily lead to unintentional double-digit calculations and the costly double-float.

Manifesting these transformations without specifically checking the compiled code becomes very difficult for them if the intended type for numeric values ​​is mostly omitted from the code and instead is included only when it is absolutely necessary. Therefore, I would choose the approach simply by typing f and getting used to them.

+1
source share

All Articles