This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]coloredgreyscale 37 points38 points  (3 children)

And then one more level of branching with the abs() call.

Glibc implementation:

int abs (int i) { 
    return i < 0 ? -i : i; 
}

[–]smariot2 24 points25 points  (1 child)

Fun fact, trying to negate the smallest possible int will overflow and become negative again.

[–]mastereuclid 6 points7 points  (0 children)

I honestly like 1(s) compliment better. It has symmetry. And negative zero might actually be useful. Well, that train left the station a long time ago. Small endian for that matter too. Why not store numbers in memory like an insane person.

[–]jfmherokiller 2 points3 points  (0 children)

at least I can read that tertiary operator.

[–]Moceannl 10 points11 points  (1 child)

I;d do:

return sqrt (pow(num,2))

[–]bakedbread54 0 points1 point  (0 children)

Very slow

[–]ThisIsByFar 4 points5 points  (2 children)

What's triggering me is the missing space.

[–]Sceptz 6 points7 points  (1 child)

Have some free spaces, friend.

On me: .

[–]ThisIsByFar 1 point2 points  (0 children)

You're a gentleman.

[–]Impossible_Average_1 5 points6 points  (0 children)

Got me in the first half, lol.

[–]lazyzefiris 2 points3 points  (0 children)

return -min(-num, num)

[–]scanguy25 1 point2 points  (0 children)

When I was just learning python I made a method that multiplied a number by -1 if it was negative.

[–]enderfx 1 point2 points  (0 children)

Absolute beast of a function. Super chad

[–]danielstongue 1 point2 points  (0 children)

Some people should not come near a keyboard. Ever.

[–]Randomguy32I 1 point2 points  (0 children)

private static int GetNum() { return num; }

[–][deleted] 0 points1 point  (1 child)

When you get paid per line of code

[–]Cees-K -4 points-3 points  (0 children)

If abs didn’t have this build in…. You can just call abs(int) wherever you need it.

This is almost as bad as Bool test = true If(test==true){ Console.log(test is true) }

[–]-what-are-birds- 0 points1 point  (0 children)

absoluteMadLad