Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F suffix on number assigned to double

Tags:

c++

c

I am aware that suffixing a floating point number with f lets the compiler know that it is a float rather than a double. Which is important to prevent unnecessary casting when operating on floats rather than doubles. However in some code I have been provided by an external company I have seen the following for example:

double i = 1.4f;

Now what I'm wondering is what happens in this case? Will the compiler silently ignore the 'f' and it is just something that has no bearing on anything and I can safely ignore?

I then wondered that if numbers are assigned to doubles with an f suffix elsewhere, not just on initialisation would that be a different case that acts differently?

like image 468
Firedragon Avatar asked Dec 09 '22 00:12

Firedragon


1 Answers

Relevant quotes from the C standard:

§6.4.4.2 paragraph 4

If suffixed by the letter f or F, it has type float.

paragraph 5

Floating constants are converted to internal format as if at translation-time.

paragraph 7

The translation-time conversion of floating constants should match the execution-time conversion of character strings by library functions, such as strtod, given matching inputs suitable for both conversions, the same result format, and default execution-time rounding.

Assuming IEEE-754 numerics 1.4f produces the value:

1.39999997615814208984375

whereas 1.4 has the value:

1.399999999999999911182158029987476766109466552734375.
like image 167
Stephen Canon Avatar answered Dec 21 '22 13:12

Stephen Canon