C++ 中複數的 log() 函式


在本文中我們將討論 C++ STL 中 log() 函式的工作原理、語法和示例。

log() 函式是什麼?

log() 函式是 C++ STL 中的一項內建函式,定義在 <complex> 標頭檔案中。log() 返回複數的復自然對數。math 標頭檔案中的 log() 和 complex 標頭檔案中的 log() 的區別在於,log() 用於計算複對數,而 math 標頭檔案中的 log() 計算普通對數值。

語法

template<class T> complex<T> log(const complex<T>& x);

引數

此函式接受一個引數,即我們要找到其對數的複數值。

返回值

我們要計算的 x 的對數值。

示例

Input: complex<double> C_number(-7.0, 1.0);
   log(C_number);
Output: log of (-7,1) is (1.95601,2.9997)

 線上演示

#include <bits/stdc++.h>
using namespace std;
int main() {
   complex<double> C_number(-7.0, 1.0);
   cout<<"log of "<<C_number<<" is "<<log(C_number)<< endl;
   return 0;
}

輸出

如果執行上述程式碼,它將生成以下輸出 -

log of (-7,1) is (1.95601,2.9997)

示例 

線上演示

#include <bits/stdc++.h>
using namespace std;
int main() {
   complex<double> C_number(-4.0, -1.0);
   cout<<"log of "<< C_number<< " is "<<log(C_number);
   return 0;
}

輸出

如果執行上述程式碼,它將生成以下輸出 -

log of (-4,-1) is (1.41661,-2.89661)

更新於: 23-Mar-2020

193 次瀏覽

開始你的 職業生涯

透過完成此課程獲得認證

開始
廣告