Linked by Thom Holwerda on Mon 24th Sep 2012 15:07 UTC, submitted by MOS6510
Permalink for comment 536366
To read all comments associated with this story, please click here.
To read all comments associated with this story, please click here.
Features
Linked by Thom Holwerda on 06/13/13 14:35 UTC
Linked by Thom Holwerda on 06/11/13 17:07 UTC
Linked by Thom Holwerda on 06/10/13 23:13 UTC
Linked by Thom Holwerda on 06/08/13 14:57 UTC
Linked by Thom Holwerda on 06/07/13 11:40 UTC
Linked by Thom Holwerda on 06/04/13 12:45 UTC
Linked by nfeske on 05/31/13 10:12 UTC
Linked by Thom Holwerda on 05/29/13 16:59 UTC
Linked by Thom Holwerda on 05/24/13 17:26 UTC
Linked by Thom Holwerda on 05/21/13 21:38 UTC
More Features »
Sponsored Links



Member since:
2010-03-08
"int myAbs(const int num) { return num < 0 ? (~num) + 1 : num; }"
There is another way: multiply the number by itself and return the square root of the multiplication (which will make it work with real numbers if you replace all instances of 'int' with 'double'). But the above code should suffice, given the requirement explicitly states 'some 32-bit signed integer'.
Computer Engineering/Computer Science graduates who don't understand why or how the above code works have wasted years of life studying a subject they're not even interested in.
Just out of curiosity, would this have been a valid option in your opinion ?
int absolute(const int num) { return (num>=0)?num:-num; }
That would pretty much have been my spontaneous answer to your problem (except with an if ... else structure instead of the trigraph).