Signup/Sign In
LAST UPDATED: NOVEMBER 1, 2019

Python Math Log functions - log(), log1p(), log2(), log10()

    In this post, we will discuss the below-mentioned math module functions.

    1. log

    2. log1p

    3. log2

    4. log10

    The above-mentioned methods are built-in and can be found in the math module. They can be accessed and used after the math module is imported and referencing it with the dot operator.


    1. The log method

    It takes two arguments and returns the log (logarithmic value) of the first argument, taking the second argument as the base. If the second parameter hasn't been mentioned (basically, if only one argument has been mentioned), it takes the natural log and calculates the value.

    Following is the syntax of the log method,

    math.log(argument_1, argument_2)

    If no value has been provided to the log method, it throws TypeError.


    Time for an example:

    import math
     
    print (math.log(12, 3)) 
    print (math.log(12)) 
    print (math.log())

    Output:

    2.2618595071429146
    2.4849066497880004
    Traceback (most recent call last):
    
      File "<ipython-input-208-9f53061ffe7e>", line 5, in <module>
        print (math.log())
    
    TypeError: log expected at least 1 arguments, got 0



    2. The log1p method

    This method takes one parameter and returns the logarithm of the (number+1), i.e log(1+parameter). The argument passed to the log1p method should be a positive value, else it raises a ValueError. In case no parameter is passed to the function, it returns TypeError. Below is a demonstration of the same:

    print(math.log1p()) # Execute each print statement separately 
    
    print (math.log1p(-12))  # Execute each print statement separately

    Output:

    TypeError: log1p() takes exactly one argument (0 given)
    
    ValueError: math domain error

    Following is the syntax of the log1p method,

    math.log1p(argument)

    Time for an example:

    import math
    
    # Cross-check by calling the log method with the parameter 13. 
    # This will b equivalent to calling log1p on 12
    print(math.log1p(12))
    print(math.log1p(12.12)) 
    print (math.log1p(-12))

    Output:

    2.5649493574615367
    2.574137783515943
    Traceback (most recent call last):
    
      File "<ipython-input-216-6651906a499f>", line 5, in <module>
        print (math.log1p(-12))
    
    ValueError: math domain error



    3. The log2 method

    This method takes one argument and calculates the logarithm of that value with base 2. Your next question could be why not use the log method and pass 2 as the second parameter. The reason is that the log2 method gives a much more accurate result. As always, if no value is passed as a parameter to this method, it throws a TypeError.

    Following is the syntax of the log2 method,

    math.log2(argument)

    Time for an example:

    The below example demonstrates why using log2 instead of log and passing 2 as the second value makes a difference in the precision of the result,

    import math
    
    print(math.log(17, 2))
    print(math.log2(17)) 
    print(math.log2())

    Output:

    4.08746284125034
    4.087462841250339
    TypeError: log2() takes exactly one argument (0 given)



    4. The log10 method

    This method takes one argument and returns the logarithmic value of that argument with base 10. The argument should be a positive value. This method is better in comparison to using the log method and passing 10 as the second parameter since log10 gives better precision.

    Following is the syntax of the log10 method,

    math.log10(argument)

    Time for an example:

    import math
    
    print (math.log(12, 10))   # Using log method only and passing 10 as the second parameter
    print (math.log10(12))     # Using the log10 method 
    print (math.log10(12.45)) 
    print (math.log10(-12))

    Output:

    1.0791812460476247
    1.0791812460476249
    1.095169351431755
    Traceback (most recent call last):
    
      File "<ipython-input-231-b90d942ea213>", line 6, in <module>
        print (math.log10(-12))
    
    ValueError: math domain error

    Note: This log10 method can be used to find out the number of digits present in a number.




    Conclusion:

    In this post, we understood three more built-in methods from the math module namely log(), log1p(), log2() and log10().
    These methods come in handy while manipulating numbers. Don't forget to execute these Python programs on your IDE. Meddle with the input to see how the output changes.

    Similar Posts:

    Math Functions - isinf(), isfinite() and isnan()

    Math Functions - ceil(), floor(), fabs() and copysign()

    Trigonometric Functions - sin, cos, tan, etc

    math.remainder() Funciton in Python

    Math functions- factorial(), fmod(), and frexp()

    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