Rod Derakhshanian
Rod Derakhshanian

Reputation: 51

how to get absolute value without 'abs' function?

def my_abs(value):
"""Returns absolute value without using abs function"""
    if value < 5 :
        print(value * 1)
    else:
        print(value * -1)
print(my_abs(3.5))

that's my code so far but the quiz prints, for example -11.255 and 200.01 and wants the opposite for example it wants 11.255 back and -200.01

Upvotes: 0

Views: 20909

Answers (7)

dev_light
dev_light

Reputation: 4116

The answers here so far focuses on real numbers. For complex numbers, the docs says if the argument of the builtin abs(x) is a complex number, its magnitude is returned.

To find the absolute value of a complex number z = a + bi, take the square root of the sum of the squares of the real parts i.e. sqrt(a^2 + b^2). This is the magnitude of the complex number. Implementing this without using ANY builtin will look like this:

def comp_abs(y, x=None):
    """ 
    Returns the magnitude of a complex number. 
    To find the absolute value of a complex number z = x + yi:
    Take the square root of sum of the squares of the real parts 
    i.e. sqrt(x^2 + y^2). This is the magnitude of the complex number.
    """
    if not x:
        return (y**2)**0.5
    return (x**2 + y**2)**0.5

Printing the function calls will give the magnitude as described:

print(comp_abs(-3, 8))    # same as abs(-3+8j)
print(comp_abs(0))        # same as abs(0j)
print(comp_abs(1))        # same as abs(1j)
print(comp_abs(-5,-6))    # same as abs(-5-6j)

Output:

8.54400374531753
0.0
1.0
7.810249675906654

Upvotes: -1

Tin Tran
Tin Tran

Reputation: 6202

A fun one. if (number < 0) returns a boolean but when you do math with it it's a 1 or 0. Below written as (a<0)

When the number is less than 0, we can get its positive value by adding 2 times the negative of itself. or just subtract itself twice written as (-a-a)

so when our condition fails it returns 0 which when multiplied by anything is zero so we don't add anything to our original number.

if our condition passes, we'll get a 2 times the positive so we can add to our original.

here's the code, the number you're trying to get absolute value is a in this case

a = a + (a<0) * (-a-a)

This runs slower than the built in abs() call. I thought it was faster but it was buggy in my code when I timed it. the fastest seem to be the if (a<0) then a = -a

Upvotes: 0

Alex_Fierro2301
Alex_Fierro2301

Reputation: 1

num = float(input("Enter any number: "))
if num < 0 :
    print("Here's the absolute value: ", num*-1)
elif num == 0 :
    print("Here's the absolute value: 0")
elif num > 0 :
    print("Here's the absolute value: ", num)

Upvotes: -2

gaspar
gaspar

Reputation: 1078

The solutions so far don't take into account signed zeros. In all of them, an input of either 0.0 or -0.0 will result in -0.0.

Here is a simple and (as far as I see) correct solution:

def my_abs(value):
    return (value**2)**(0.5)

Upvotes: 3

Aswin Murugesh
Aswin Murugesh

Reputation: 11080

Why do you want to check if value < 5?

Anyways, to replicate the abs function:

def my_abs(value):
    return value if value >=0 else -1 * value 

Upvotes: 0

janbrohl
janbrohl

Reputation: 2656

A simple solution for rational numbers would be

def my_abs(value):
    if value<0:
        return -value
    return value

Upvotes: 0

DeepSpace
DeepSpace

Reputation: 81684

What does 5 have to do with absolute value?

Following your logic:

def my_abs(value):
    """Returns absolute value without using abs function"""
    if value <= 0:
        return value * -1
    return value * 1

print(my_abs(-3.5))
>> 3.5
print(my_abs(3.5))
>> 3.5

Other, shorter solutions also exist and can be seen in the other answers.

Upvotes: 3

Related Questions