Assigning C float from literal

When reading part of a book on 3D graphics, I came across the following purpose:

const float vertexPositions[] = {
0.75f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f,
};

Why is the suffix f needed? Is it possible to determine the type of literals by the type of variable? I believe that without the letters f, floats are interpreted as paired, but why when the array is obviously of type float?

+4
source share
4 answers

​​ C, , double. , double, float, float. , , , , .

, , float .

, , float double - "double-rounding" ( "double" ). f1 f2 , , f1 1.01161128282547:

  float f1 = 1.01161128282547f;
  float f2 = 1.01161128282547;

, f2 1.01161128282547 double, float. , float, f1.

+4
  • .

    • , .
    • {} (, 1.0f), , . C , . C 1.0 .
  • C ( ) . . , . . , , . , 1.0 double, float.

  • double (, 1.0) float. , double float. , . , , (, 1.0f) float (, (float) 1.0) float. , DRY (Don't-Repeat-Yourself), .

+1

f, , , :

float num = 0.1;
(num == 0.1)

Evaluation will result in 0, because it is numrounded to 0.1both a float and 0.1in a double expression. Thus, they do not actually match, although they may seem.

0
source

No, the literal type is never inferred. Without fthese, they are doubleliterals, and an instruction is a transformation that can lose accuracy (in fact, this is not so, because all literals are accurately represented using float).

The same warnings as you

float x = 3.0;
0
source

All Articles