該函數用於求給定數字的自然對數(base-e 對數)。
數學上:
假設 'x' 是一個給定的數字:
logex = log(x);
用法
float log(float x);
double log(double x);
long double log(long double x);
double log(integral x);
參數
x: 是要計算自然對數的值。
返回值
以下是給定數字的返回值:
參數(x) | 返回值 |
---|---|
x>1 | Positive |
x=1 | 0 |
1>x>0 | Negative |
x=0 | -無窮大 |
x<0 | 不是數字(nan) |
例子1
當 x 的值為 1 時,讓我們看一個簡單的例子。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=1;
std::cout << "Value of x is:" <<x <<std::endl;
cout<<"Log value of x is:"<<log(x);
return 0;
}
輸出:
Value of x is:1 Log value of x is:0
在本例中,x 的值為 1。因此,函數 log() 返回正值,即 0。
例子2
讓我們看另一個簡單的例子
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=3;
std::cout << "Value of x is:" <<x <<std::endl;
cout<<"Log value of x is:"<<log(x);
return 0;
}
輸出:
Value of x is:3 Log value of x is:1.09861
在本例中,x 的值為 3。因此,函數 log() 返回正值,即 1.09861
例子3
當 x 的值為 -0.5 時,讓我們看一個簡單的例子。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x= -0.5;
std::cout << "Value of x is:" <<x <<std::endl;
cout<<"Log value of x is:"<<log(x);
return 0;
}
輸出:
Value of x is:-0.5 Log value of x is:nan
在本例中,x 的值為 -0.5。因此,函數 log() 返回 Not a Number(nan)。
示例 4
讓我們看一個簡單的例子,當 x 的值為 0 時。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x= 0;
std::cout << "Value of x is:" <<x <<std::endl;
cout<<"Log value of x is:"<<log(x);
return 0;
}
輸出:
clValue of x is:0 Log value of x is:-inf
在本例中,x 的值為 -1。因此,函數 log() 返回 nan(Not a Number)。
例 5
當 x 的值為 0.8 時,讓我們看一個簡單的例子。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=0.8;
std::cout << "Value of x is:" <<x <<std::endl;
cout<<"Log value of x is:"<<log(x);
return 0;
}
輸出:
Value of x is:0.8 Log value of x is:-0.223144
在本例中,x 的值為 0.8。因此,函數 log() 返回負值,即 -0.22
相關用法
- C++ Math log2()用法及代碼示例
- C++ Math log10()用法及代碼示例
- C++ Math logb()用法及代碼示例
- C++ Math log1p()用法及代碼示例
- C++ Math llround()用法及代碼示例
- C++ Math less()用法及代碼示例
- C++ Math lgamma()用法及代碼示例
- C++ Math lround()用法及代碼示例
- C++ Math lrint()用法及代碼示例
- C++ Math llrint()用法及代碼示例
- C++ Math scalbn()用法及代碼示例
- C++ Math acosh()用法及代碼示例
- C++ Math asinh()用法及代碼示例
- C++ Math isgreater()用法及代碼示例
- C++ Math fabs()用法及代碼示例
- C++ Math islessgreater()用法及代碼示例
- C++ Math nearbyint()用法及代碼示例
- C++ Math tan()用法及代碼示例
- C++ Math nextafter()用法及代碼示例
- C++ Math fdim()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Math log()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。