Monday, February 14, 2011

Python Basics: Built-in function abs usage

First of all Built-in functions are those functions that are available to us all the time without importing any python modules. Today we'll see how to use abs(x) function.
So, Let's get Started:
abs(x) function takes an argument that can be a integer or a floating point number. It returns you the absolute value of a given number. A argument can be complex number, in that case the magnitude is returned.
Simple Case:

Let, say if x is less than zero, i.e, x < 0, than -x is returned. Otherwise, if x is greater than or equal to zero, i.e, x >= 0, than x is returned.

Complex Case:

Let, say if x is complex, than in that case abs returns a square root of  x.imag**2 + x.real**2 (a.k.a magnitude) of a number.

We'll be discussing complex numbers in another post. But in any case the formula to compute them remains same.

Hope this Helps! Please write your comments it will help me improve.

No comments:

Post a Comment