While learning the C++ programming language, I found out that there are three ways to convert number types. For instance, suppose you want to convert an int
to a float
, then you can do either of the followings:
int myInt = 10;
float myFloat1 = (float) myInt;
float myFloat2 = float(myInt);
float myFloat3 = static_cast<float>(myInt);
As explained everywhere on the Internet, the first method is a C-style method, while the second and the third are C++ methods. But, are they completely interchangeable? Try this:
float myFloat1 = (float) 1/2;
float myFloat2 = float(1/2);
float myFloat3 = static_cast<float>(1/2);
That is, we are doing a division of two int
s, and we want the result to be a float
. However, try to print each variables that we just created!
std::cout << myFloat1 << std::endl; // prints 0.5
std::cout << myFloat2 << std::endl; // prints 0
std::cout << myFloat3 << std::endl; // prints 0
Now, why do myFloat2
and myFloat3
equal to zero? I think this is due to how the difference in the mechanism of the conversion that we just did. This is my explanation of what happens:
- When we do
(float) 1/2
, the compiler will first convert each number to a float, and then execute the division. In this case,1
is converted to1.0
,2
is converted to2.0
, and finally the compiler divides1.0
with2.0
. The result is afloat
that equals to0.5
since both numbers arefloat
s. - When we do
float(1/2)
orstatic_cast<float>(1/2)
, the compiler will first execute the divison, and then convert the result tofloat
. In this case, the compiler will divide1
(anint
) with2
(anint
), and the result will be0
(since0.5
rounded down to the nearest integer is0
). After that, the number0
is converted tofloat
. So, the final result is still0
.