Cara menggunakan log10 python

Section Artikel

    • 0.1 Log
  • 1 Log pada Basis 2
  • 2 Log pada Basis 10
  • 3 Natural Log, atau Log di Base e
  • 4 Log pada Basisi Apapun

Log

NumPy menyediakan fungsi untuk melakukan log di basis 2, e dan 10.

Kita juga akan mempelajari bagaimana kita dapat menghitung log untuk basis apa pun dengan membuat ufunc khusus.

Semua fungsi log akan menempatkan -inf atau inf di elemen jika log tidak dapat dihitung.

Log pada Basis 2

Gunakan fungsi log2() untuk melakukan log pada basis 2.

Contoh:
Temukan log basis 2 dari semua elemen array berikut

import numpy as np

arr = np.arange(1, 10)

print(np.log2(arr))

Catatan: Fungsi arange (1, 10) mengembalikan array dengan bilangan bulat mulai dari 1 (disertakan) hingga 10 (tidak termasuk).

Log pada Basis 10

Gunakan fungsi log10() untuk melakukan log basis 10.

Contoh:
Temukan log basis 10 dari semua elemen array berikut

import numpy as np

arr = np.arange(1, 10)

print(np.log10(arr))

Natural Log, atau Log di Base e

Gunakan fungsi log() untuk melakukan log basis e.

Contoh:
Temukan log basis e dari semua elemen array berikut

import numpy as np

arr = np.arange(1, 10)

print(np.log(arr))

Log pada Basisi Apapun

NumPy tidak menyediakan fungsi yang berguna untuk menghitung log di basis apapun, tetapi kita bisa menggunakan fungsi frompyfunc() bersama dengan fungsi matematika.log() dengan dua parameter input dan satu parameter output.

Contoh:

from math import log
import numpy as np

nplog = np.frompyfunc(log, 2, 1)

print(nplog(100, 15))

Cara menggunakan log10 python

Python math.log10() function is a library method of the math module, and it is used to get the base-2 logarithm of the number; it accepts a number and returns the base-10 logarithm of the given number.

Python log10() is a built-in math library function used to get the logarithm of any given number with base 10. To use log10() function in Python, import the math library to use this function. The log10() method returns the base-10 logarithm of x for x > 0.

Syntax

math.log10(num)

The log10() function takes two arguments:

num -> whose log we want to find with base 10.

Return Value

Python log10() function returns the logarithm of base 10, particularly given number.

But this function throws a ValueError exception if any value is passed as an argument.

Programming Example

See the following code.

# Importing math library
import math

# initializing values

# positive value
num = 10
print("Logarithm with base 10 of the value ", num, " is: ", math.log10(num))

# Negative number
num = -10
print("Logarithm with base 10 of the value ", num, " is: ", math.log10(num))

Output

Logarithm with base 10 of the value  10  is:  1.0
Traceback (most recent call last):
  File "1log10.py", line 12, in 
	print("Logarithm with base 10 of the value ",num," is: ",math.log10(num))
ValueError: math domain error

In this program, we initialized the value, then calculated the logarithm of the number with base 10. In the next line, we wanted to calculate the logarithm of a negative number, but as per rule, the program threw an exception of ValueError.

Program 2

See the following program.

# app.py

# Importing math library
import math

# taking input from user values

# positive value
num = int(input("Enter a num to find log10(num): "))
print("Logarithm with base 10 of the value ", num, " is: ", math.log10(num))

Output

Enter a num to find log10(num): 15
Logarithm with base 10 of the value  15  is:  1.1760912590556813

In this program, we have taken input from the user and calculated the logarithm of base 10.

Python log10() with list and tuple

Let’s apply the log10() function to the list and tuple, and see the output. See the following code.

import math

Tup = (1, 2, 11, -4, 5)  # Tuple Declaration
Lis = [-1, 2, -11.21, -4, 5]  # List Declaration

print('The log10() value of Positive Number = %.2f' % math.log10(1))
print('The log10() value of Positive Decimal = %.2f' % math.log10(2.5))

print('The log10() value of Tuple Item = %.2f' % math.log10(Tup[2]))
print('The log10() value of List Item = %.2f' % math.log10(Lis[4]))

print('The log10() value of Multiple Number = %.2f' % math.log10(2 + 7 - 5))
print('The log10() value of String Number = ', math.log10('Locke and Key'))

Output

python3 app.py
The log10() value of Positive Number = 0.00
The log10() value of Positive Decimal = 0.40
The log10() value of Tuple Item = 1.04
The log10() value of List Item = 0.70
The log10() value of Multiple Number = 0.60
Traceback (most recent call last):
  File "app.py", line 13, in 
    print('The log10() value of String Number = ', math.log10('Locke and Key'))
TypeError: must be real number, not str

If you pass a string, it gives the TypeError.

Conclusion

Python log10() function calculates the logarithmic value of the given number of base 10. First, we find the base 10 logarithmic value of different data types and display the output.

See also

Python log(2)

Python log(x, base)

Python log1p(x)

Python exp()

Python trunc()