What is the difference between casting to `float` and adding `f` as a suffix when initializing a `float`?

CCastingFloating Point

C Problem Overview


What is the difference between

float f = (float) 99.32 ;

and

float f = 99.32f ;

Both of them compiled and ran successfully.

C Solutions


Solution 1 - C

float f = 99.32f ;

That is a float literal, which means a float variable being assigned with a float value directly.

float f = (float) 99.32 ;

That is a float variable that is assigned a double value that is cast to float before being assigned.

Solution 2 - C

The difference may be optimized away, but in the first case you have a double literal that is type casted to a float while you have a float literal in the second case.

If not optimized away you will get a typecast in the code in the second example.

However there are corner cases where the result could (depending on rounding mode) be slightly different. If your number can't be exactly represented you will in the first case get rounding twice - first when you round the decimal representation to a double and then when you round that to a float, while in the first case you round the decimal representation directly to a float.

Solution 3 - C

In the first case without the cast 99.32 is interpreted as double not as a float.

A double literal is being casted to float.

In the second case you have a suffix f to make sure the compiler treats 99.32 as float.

Solution 4 - C

In the line float f = (float) 99.32; the literal 99.32 is created as a double type by default and is then cast to a float.

In the line float f = 99.32f ; the literal is created as a float type due to the trailing f in 99.32f and no type casting is required.

The latter would be analogous to writing double f = 99.32; as you'd have a double type being assigned directly to a variable of a matching type.

Solution 5 - C

Floating point literals without a suffix are by default of double type.

Thus, stating float f = (float) 99.32; you first cast explicitly literal 99.32 which is of type double to a float and then assign it to variable f.

Stating float f = 99.32; does the same thing but in this case the conversion between types is done implicitly.

If you want to avoid implicit conversion you should use the f suffix to define your literal (i.e., float f = 99.32f;)

Solution 6 - C

The difference is that the literal 99.32 is of type double, while the literal 99.32f is of type float.

The first statement assigns the float literal to a float variable. Nothing special.

The second statement casts a double literal to a float, and assigns the result to a float variable.

As far as the standard is concerned, you can assign a double literal to a float variable, without explicitly casting it yourself. In that case implicit cast occurs. E.g. :

float f = 99.32;

You can even do :

float f = (double) 10.5f;

and the right hand side is still implicitly converted to a float.

Please note that most modern compilers optimize those away, so representing floats is usually matter of style and preference. Just be consistent.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser5422167View Question on Stackoverflow
Solution 1 - CMagischView Answer on Stackoverflow
Solution 2 - CskykingView Answer on Stackoverflow
Solution 3 - CGopiView Answer on Stackoverflow
Solution 4 - Ccode_dreddView Answer on Stackoverflow
Solution 5 - C101010View Answer on Stackoverflow
Solution 6 - CEvdzhan MustafaView Answer on Stackoverflow