當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Math log10()用法及代碼示例

該函數計算給定數字的常用算法(基數為 10)。

數學上:

假設一個數字是 'x':

log10x = log10(x);

用法

float log10(float x);
double log10(double x);
long double log10(long double x);
double log10(integral x);

注意:return_type 可以是 float、double 或 long double。

參數

x: 要計算其常用對數的值。

返回值

以下是給定數字的返回值:

參數(x) 返回值
x>1 Positive
x=1 0
1>x>0 Negative
x=0 -無窮大
x<0 不是數字

例子1

我們來看一個簡單的例子,當 x 的值大於 1 時。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    int x=5;
    std::cout << "Value of x is:" <<x <<std::endl;
    cout<<"Log value of x is:"<<log10(x);
    return 0;
}

輸出:

Value of x is:5
Log value of x is:0.69897

在本例中,x 的值為 5。因此,函數 log10() 返回正值,即 0.69。

例子2

讓我們看一個簡單的例子,當 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:"<<log10(x);
    return 0;
}

輸出:

Value of x is:1
Log value of x is:0

在此示例中,x 的值為 1。因此,函數 log10() 返回值零。

例子3

當 x 的值為 0.3 時,讓我們看一個簡單的例子。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    float x=0.3;
    std::cout << "Value of x is:" <<x <<std::endl;
    cout<<"Log value of x is:"<<log10(x);
    return 0;
}

輸出:

Value of x is:0.3
Log value of x is:-0.522879

在本例中,x 的值為 0.3。因此,函數 log10() 返回負值,即 -0.52。

示例 4

當 x 的值為零時,讓我們看一個簡單的例子。

#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:"<<log10(x);
    return 0;
}

輸出:

Value of x is:0
Log value of x is:-inf

在此示例中,x 的值為零。因此,函數 log10() 返回負無窮大值。

例 5

讓我們看一個簡單的例子,當 x 的值為 -4 時

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
    float x= -4;
    std::cout << "Value of x is:" <<x <<std::endl;
    cout<<"Log value of x is:"<<log10(x);
    return 0;
}

輸出:

Value of x is:-4
Log value of x is:nan

在本例中,x 的值為 -4。因此,函數 log10() 返回 Not a Number(nan)。






相關用法


注:本文由純淨天空篩選整理自 C++ Math log10()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。