Reputation: 3
I am trying to get float number result from this c++ code, but i cannot for some reason:
int n = 5;
std::cout << static_cast<float>(n + n % 2) / 2 << std::endl;
Console output is 3. Could you explain what I am missing in this expression?
Upvotes: 0
Views: 61
Reputation: 238461
How can I cast (n+n%2) expression to float so it will give me float number after division?
You can use static_cast
, as you did in your example.
Console output is 3. Could you explain what I am missing in this expression?
Your are missing nothing. The cast to float was achieved successfully and 3 is the correct result of 6.f / 2
using floating point division.
Upvotes: 3