Is this the right way to do this?
Thank youCode:int input; float temp = input * (2/3); int output = roundf(temp);
This is a discussion on Rounding a float and casting to an int within the C Programming forums, part of the General Programming Boards category; Is this the right way to do this? Code: int input; float temp = input * (2/3); int output = ...
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;
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
For my purpose, either is fine. But was curious for future reference.
Thank you