Is this the right way to do this?
Thank youCode:int input; float temp = input * (2/3); int output = roundf(temp);
Is this the right way to do this?
Thank youCode:int input; float temp = input * (2/3); int output = roundf(temp);
Assuming that you actually give input a value, the problem would be that (2/3) results in integer division which truncates the result to 0. If you change it to (2.0f/3.0f) it should work. However, I wonder if this would be simpler yet give identical results:
Code:int output = (input * 2) / 3;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ah, yes. thank you.
So, for
int output = (input * 2) / 3;
Is it truncating or rounding?
Ah yes, that would truncate, not round.Originally Posted by pollypocket4eva
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
For my purpose, either is fine. But was curious for future reference.
Thank you