Linked by Thom Holwerda on Mon 24th Sep 2012 15:07 UTC, submitted by MOS6510
"I've been programming professionally for about 3 years at this point, and I've noticed some interesting patterns in other programmers I've worked with. One of the key differentiators among programmers is motivation. I'm not referring to an individual's passion to simply be successful in their career, but rather the type of work they want to pursue. The thing they want to do with computers every day, the types of problems they are interested in solving."
by ssokolow on Tue 25th Sep 2012 04:45 UTC in reply to "Sad Truth"

Member since:
2010-01-21

"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.
"

Thank you for reminding me to re-teach myself two's complement. Last time I learned it, I was sleepy, rushed, and hadn't discovered Taskwarrior.

(Because I don't remember the relationship between one's complement and two's complement enough to use it from memory, I'd have multiplied by -1 to convert instead... or just used abs() in a practical situation. 95% of the code I've had to write has been in Python lately and the rest in bash script.)

by JeeperMate on Tue 25th Sep 2012 10:24 in reply to "RE: Sad Truth"
Member since:
2010-06-12

Yes, sure... There's abs(). There are a gazillion other functions offered by various 3rd-party libraries for various purposes. Heck, one could even Google for solutions to basic programming problems. But a) we cannot always depend on 3rd-party solutions in real projects (most of the times we have to implement our own libraries or at least fork something that's closest to our requirements), and b) we need a programmer who can come up with good solutions without depending on Google Search.

If one doesn't remember such a basic thing he/she learned at school, what else has he/she forgotten?

by phreck on Tue 25th Sep 2012 12:01 in reply to "RE[2]: Sad Truth"
Member since:
2009-08-13

Now you are showing ridiculous behaviour.

You show to your working comrades a young boys code for teh lulz (which is a form of sadism), and then don't even now that abs() is a standard function?

Fellows and Me: "What."

Seriously: If you want a simple and elegant solution:

#include <algorithm>
...
std::abs(...)
...

Edited 2012-09-25 12:09 UTC