gaztech wrote:Not a good idea. Works for 2/5 => x=1, but if the division is remainder-less, this leads to a wrong result:
int x = (int) (2/2) + 0.5;
Dividing 2 by 2 implies x == 2.
Arguably Nick's way is the most elegant, zk's is equivalent, but a bit obfusciated and only works if either of the operands is a literal, not a variable.
#96234, By Moonbender C Programming
Moonbender 407 posts
Seen 5 years ago
Registered 14 years ago