Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python 프로그램의 로그 함수


이 튜토리얼에서는 수학 모듈의 대수 함수에 대해 배울 것입니다. 로그 함수에는 네 가지 변형이 있습니다. Python'은 수학 모듈에서 모든 것을 제공합니다. 하나씩 알아보도록 하겠습니다.

math.log(숫자, [기준])

math.log(숫자, [기준]) 메소드는 모든 Base의 로그를 계산하는 데 사용됩니다. . 기본 값을 지정하지 않으면 e를 기본 기본으로 사용합니다.

참고 − 메서드에 음수를 전달하면 ValueError가 발생합니다.

예시

몇 가지 예를 살펴보겠습니다.

# importing math module
import math
# logarithm with base 3
print(math.log(15, 7))

출력

위의 프로그램을 실행하면 다음과 같은 결과를 얻을 수 있습니다.

1.3916625094004957

위의 프로그램에서 원하는 기본 값을 지정할 수 있습니다. 기본 값이 없는 동일한 예를 살펴보겠습니다. 기본 기본 값은 e.입니다.

예시

# importing math module
import math
# logarithm with base e(default)
print(math.log(15))

출력

위의 코드를 실행하면 다음과 같은 결과를 얻을 수 있습니다.

2.70805020110221

예시

math.log()에 음수를 전달하면 어떻게 되는지 봅시다. 방법.

# importing math module
import math
# logarithm with negative number
print(math.log(-15))

출력

위의 프로그램을 실행하면 다음과 같은 결과를 얻을 수 있습니다.

---------------------------------------------------------------------------
ValueError                   Traceback (most recent call last)
<ipython-input-6-b686fcb806c6> in <module>
      3
      4 # logarithm with base e(default)
----> 5 print(math.log(-15))
ValueError: math domain error

math.log2(숫자)

밑이 2인 값에 대한 로그를 계산하려면 math.log2() 메서드를 사용할 수 있습니다. 위의 방법과 비슷합니다. 몇 가지 예를 살펴보겠습니다.

예시

# importing math module
import math
# logarithm with base 2
print(math.log2(15))

출력

위의 코드를 실행하면 다음과 같은 결과를 얻을 수 있습니다.

3.9068905956085187

math.log와 유사 메소드에서 math.log2에 음수를 전달하면 오류가 발생합니다. 방법. 예를 들어 봅시다.

예시

# importing math module
import math
# logarithm with base 2 & negative number
print(math.log2(-15))

출력

프로그램을 실행하여 출력을 보면 지금과 이전에 발생한 오류가 동일함을 알 수 있습니다.

---------------------------------------------------------------------------
ValueError                               Traceback (most recent call last)
<ipython-input-3-8019b45e571f> in <module>
      3
      4 # logarithm with base 2 & negative number
----> 5 print(math.log2(-15))
ValueError: math domain error

math.log10(숫자)

밑이 10 인 로그를 찾을 수 있습니다. math.log10 사용 방법. 위의 math.log2와 유사합니다. 방법. 몇 가지 예를 살펴보겠습니다.

예시

# importing math module
import math
# logarithm with base 10
print(math.log10(15))

출력

위의 프로그램을 실행하면 다음과 같은 결과를 얻을 수 있습니다.

1.1760912590556813

math.log10에 음수를 전달해 보세요. 방법. 위의 방법과 유사한 오류가 발생합니다.

예시

# importing math module
import math
# logarithm with base 10 & negative number
print(math.log10(-15))

출력

출력을 보면 다음과 같은 오류가 발생합니다.

---------------------------------------------------------------------------
ValueError                            Traceback (most recent call last)
<ipython-input-5-52ac56b802ca> in <module>
      3
      4 # logarithm with base 10 & negative number
----> 5 print(math.log10(-15))
ValueError: math domain error

math.log1p(숫자)

math.log1p(x) 메서드 log(1 + x)를 계산합니다. 기본 e 포함 . 주어진 숫자에 1을 더하여 로그를 계산합니다. 몇 가지 예를 살펴보겠습니다.

예시

# importing math module
import math
# logarithm
print(math.log1p(15)) # similar to math.log(16)

출력

위의 프로그램을 실행하면 다음과 같은 결과를 얻을 수 있습니다.

2.772588722239781

math.log1p에 음수를 전달해 보세요. 방법. 이전에 본 것처럼 오류가 발생하리라 확신합니다.

예시

# importing math module
import math
# logarithm
print(math.log1p(-15))

# 수학 모듈 가져오기 import 수학 # logarithm print(math.log1p(-15))

출력

메서드에 전달한 음수 때문에 다음 오류가 발생합니다.

---------------------------------------------------------------------------
ValueError                      Traceback (most recent call last)
<ipython-input-15-26016884cb23> in <module>
      3
      4 # logarithm
----> 5 print(math.log1p(-15))
ValueError: math domain error

결론

우리는 수학 모듈에서 총 4개의 로그 메서드를 보았습니다. 튜토리얼에서 본 로그 방법 중 하나에 음수를 전달하면 오류가 발생합니다. 또한 부동 숫자를 메서드에 전달할 수도 있습니다. 이 튜토리얼에서 본 예제를 부동 숫자로 실행해 보십시오.