Signup/Sign In
LAST UPDATED: SEPTEMBER 9, 2021

Python Math Library functions - exp(), expm1(), pow(), and sqrt()

    In this post, we will cover the following math functions:

    1. exp

    2. expm1

    3. pow

    4. sqrt

    Remember to import the math library and reference it using the dot operator before accessing any of the above functions.

    1. Python exp() method

    The exp() method is a built-in Python method that calculates the power of a number, i.e the exponential of a number which is passed as an argument to the function. It returns a floating-point number after calculating the number raised to a specific power.

    Syntax of exp() method

    math.exp(number)

    Time for an example:

    import math   
    
    neg_val = -12 
    print (math.exp(12.65)) 
    print (math.exp(neg_val)) 
    print (math.exp(-12.89)) 
    print (math.exp(math.pi)) # constant
    print (math.exp(math.e))  # constant

    Output:

    311763.4480807426
    6.14421235332821e-06
    2.5231561490262545e-06
    23.140692632779267
    15.154262241479262

    If an argument other than a number is passed to the exp() method, it throws a TypeError. Below is a demonstration of the same.

    import math    
    
    print (math.exp("12"))  # Passing a string

    Output:

    TypeError: must be real number, not str

    Note: This exp() method is different in comparison to the numpy.exp method.


    2. Python expm1() method

    The expm1() method takes in one argument and gives the value of exp(argument)-1(which means exponential of a number minus 1). This is a very specific operation which is used widely in many mathematical and scientific formulae. In addition to this, the expm1() method gives a much more accurate value when the passed argument is a very small value.

    Syntax of the expm1() method

    math.expm1(value)

    Time for an example:

    import math 
    
    pos_int = 12
    neg_int = -12
    
    print(math.expm1(pos_int))
    print(math.expm1(neg_int))

    Output:

    162753.79141900392
    -0.9999938557876467


    Comparing the exp() and expm1() methods:

    Following is a code example to show you the difference between the exp() and expm1() methods.

    import math 
      
    pos_int = 12
    neg_int = -12
    
    print(math.exp(pos_int))
    print(math.exp(neg_int))

    Output:

    162754.79141900392
    6.14421235332821e-06


    3. Python pow() method

    It is a built-in Python method that returns the power of a number raised to the second number given as an argument. It can have three arguments out of which one of them is optional. The return type of this method depends on the type and number of arguments passed to it.

    Syntax of pow() method

    math.pow(x,y)
    # or
    math.pow(x,y,z)

    When 2 arguments (x and y) are passed, this method returns the value of x raised to the power y, which is equivalent to x**y. When the third argument (z) is passed, the pow method returns (x raised to y) modulus z.

    There are certain rules which need to be adhered to while using the pow method with negative numbers. They have been listed below:

    1. When the first argument (x) is negative or non-negative and the second argument (y) is a non-negative number, the third argument can be present or absent.

    2. When the first argument (x) is negative or non-negative and the second argument (y) is a negative number, the third argument should not be present, i.e it should be absent.

    Based on the presence of arguments, and their type, below are the return types:

    • Non-negative argument (x), Non-negative argument (y) returns an integer value.

    • Non-negative argument (x), Negative argument (y) returns a floating-point value.

    • Negative argument (x), Non-negative argument (y) returns an integer value.

    • Negative argument (x), Negative argument (y) returns an integer value.

    • Non-negative or negative argument (x), Non-negative argument (y), Non-negative or negative argument (z) returns an integer value.

    Time for an example:

    We will first look at examples of using 2 arguments.

    print(pow(2, 3))  # x and y positive
    print(pow(-2, 3))  # negative x, positive y
    print(pow(2, -3))  # positive x, negative y
    print(pow(-2, -3))  # negative x, negative y

    Output:

    8
    -8
    0.125
    -0.125


    Passing three arguments to the pow() method

    This will cause the operation to work like (pow(x,y)) % z. The result of pow(x,y) is computed and then divided by z to find the remainder.

    x = 4
    y = 12
    z = 2
    
    print(pow(x, y, z))

    Output:

    0


    4. Python sqrt() method

    The built-in Python method sqrt() calculates the second root of the number which is passed as an argument to the method. The argument passed should be a positive value which is greater than or equal to zero, no less than that. If a negative value is passed as an argument, it returns a ValueError.

    Syntax of the sqrt() method

    math.sqrt(value>=0)

    Time for an example:

    import math
    
    print(math.sqrt(0))
    print(math.sqrt(16))
    print(math.sqrt(12))
    print(math.sqrt(12.45))

    Output:

    0.0
    4.0
    3.4641016151377544
    3.52845575287547

    When a value less than 0 is passed, it results in a python runtime error:

    import math
    
    print(math.sqrt(-12))

    Output:

    ValueError: math domain error


    Conclusion:

    In this post, we understood the significance and usage of the Python built-in functions - exp, expm1, pow, and sqrt. Don't forget to execute the codes on your IDE and try different inputs.

    You may also like:

    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS