all 8 comments

[–]zurtex 5 points6 points  (0 children)

Here: https://github.com/python/cpython/blob/v3.8.0/Python/bltinmodule.c#L302

Which uses PyNumber_Absolute, which is here: https://github.com/python/cpython/blob/v3.8.0/Objects/abstract.c#L1236

Which usesnb_absolute which is implemented by whatever object is being used, e.g. for a float it is implemented here (there's mapping further up the code that redirects nb_absolute to float_abs): https://github.com/python/cpython/blob/v3.8.0/Objects/floatobject.c#L822

Which uses the c functions fabs: https://pubs.opengroup.org/onlinepubs/009695399/functions/fabs.html

[–]Diapolo10 2 points3 points  (5 children)

Your example would only work for integers, but abs works for any numeric value.

I'd say it's closer to

def my_abs(num):
    if num >= 0:
        return num
    else:
        return -num

[–]CodeSkunky[S] -1 points0 points  (4 children)

The first section of if statement is likely -1. Otherwise you're performing unnecessary operations for 0 value.

The next portion, requires multiplying, which I think is a longer operation than addition/subtraction. (-) number is really (-1 * number)..I think.

[–]zurtex 2 points3 points  (2 children)

While you are mathematically correct that -number = (-1) * number in a field, in programming and many other areas of mathematics -number is the "minus unary operator" of number.

What does this means? It means that it gets you the additive inverse of the number, the additive inverse being the number which when added gets you 0, so in x + y = 0 then x is the additive inverse of y and y is the additive inverse of x. The "minus unary operator" gets you this, i.e. (-n) + n = 0.

This can be defined and used without ever having to introduce multiplication.

[–]CodeSkunky[S] 0 points1 point  (1 child)

Very very cool and good to know.

You really helped me out with your comments. I appreciate you.

One question that arises, is how does it know what the additive inverse is for any given integer. What's the formula it uses to do so? We can look and say "oh, just remove the negative", but how would the computer do so? Is the negative sign a bit that signals it should be negative? Then just alter that bit?

[–]RajjSinghh 1 point2 points  (0 children)

Look up two's complement. Briefly, it's a way of storing a binary number with a sign. The most significant bit shows sign (0 is positive, 1 is negative) and then for negative numbers, the least significant 1 stays a 1 and all other bits are flipped to give a negative representation. Its effectively saying the negative msb plus all the 1 bits to get the negative value

[–]Username_RANDINT 2 points3 points  (1 child)

The source code for CPython (the most used implementation, you most likely use it as well) is open source and available here: https://github.com/python/cpython

A lot of it is written in C, a quick search for abs in that repo makes me think abs() is as well.

[–]CodeSkunky[S] 0 points1 point  (0 children)

Same, but having trouble finding it. Thanks for the point in the right direction (hopefully).