F suffix for number assigned to double

I know that the suffix of a floating point number with f allows the compiler to know that it is a float, not a double. This is important to prevent unnecessary casting when working on floats, rather than two-floats. However, in some code I was provided by an external company, I saw, for example, the following:

double i = 1.4f; 

Now, what interests me is what happens in this case? Will the compiler silently ignore "f", and this is just something that has nothing to do with anything, and I can safely ignore it?

Then I thought that if numbers are assigned to doubles with the suffix f in a different place, and not just during initialization, will this be a different case that acts differently?

+4
source share
2 answers

Corresponding quotes from standard C:

§6.4.4.2 clause 4

If the suffix is ​​letter f or F, it is of type float.

point 5

Floating constants are converted to an internal format, as if during translation.

point 7

Converting floating point time to time conversion should correspond to converting a sequence of characters in a sequence of execution using library functions such as strtod, given matching inputs suitable for both conversions, the same result format and round-off by default execution time.

Assuming that the numerical values ​​of IEEE-754 1.4f value:

 1.39999997615814208984375 

whereas 1.4 matters:

 1.399999999999999911182158029987476766109466552734375. 
+11
source

A good compiler should convert 1.4f directly to double.

The corresponding compiler converts the 1.4f format to float , then converts the value to double , and then assigns the value i .

By the way, historically from the days of FORTRAN, a variable named i usually contains integer values.

+3
source

Source: https://habr.com/ru/post/1411792/


All Articles