#96234, By Moonbender C Programming

  • Moonbender 24 Jun 2003 12:05:33 407 posts
    Seen 7 years ago
    Registered 16 years ago
    gaztech wrote:
    You could just add 0.5 to the result then cast it. Its dirty but its an easy way of doing it.
    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.
Log in or register to reply