Just using static_cast<int>(y) , you get all the benefits you are looking for:
- truncation
- casting
- explicit conversion for clarity.
reasons why I will not use trunc()
- This is not so often, and probably someone who reads your code will have to look through the documentation (which is why I did it, but again, I'm not an expert)
- you use implicit conversion
trunc() , trunc() does not return int. - for me, this is not clear enough, after reading your code and documentation, I asked myself this: "did he intend to pour on int, or do you just need a float without part of the share"
I can think of a situation or two where I want to get rid of part of the fractions, but I still want the variable to be of type float for several reasons like: I want the operation x + 0.1f save part of the fraction. therefore, I will nevertheless doubt your intentions, perhaps you did not mean the implicit conversion.
OR , you can simply add a small comment next to it int x = y; // yes, I know what I'm doing int x = y; // yes, I know what I'm doing .
It will also give you clarity.
Mhd.tahawi
source share