📜  C++ STL中的logb()函数

📅  最后修改于: 2021-05-30 12:23:14             🧑  作者: Mango

logb()是C++ STL中的内置函数,它使用FLT_RADIX作为对数的底数,返回| x |的对数。通常,FLT_RADIX的值为2,因此logb()等效于log2()(仅对于正值)。

语法

logb(val)

参数:该函数接受单个强制参数val,该参数指定要计算其logb()的val。数据类型可以是double,float,long double或int。

返回类型:函数返回| x |的对数。

下面的程序说明了上述函数:

程序1

// C++ program to illustrate
// to implement logb() function
// when data-type is integer
#include 
#include 
using namespace std;
int main()
{
    double result;
    int x = -10;
  
    result = logb(x);
    cout << "logb(" << x << ") = "
         << "log(|" << x << "|) = " << result << endl;
  
    x = 10;
  
    result = logb(x);
    cout << "logb(" << x << ") = "
         << "log(|" << x << "|) = " << result << endl;
  
    return 0;
}
输出:
logb(-10) = log(|-10|) = 3
logb(10) = log(|10|) = 3

程序2

// C++ program to illustrate
// to implement logb() function
// when data-type is double
#include 
#include 
  
using namespace std;
  
int main()
{
    double x = 70.56, result;
  
    result = logb(x);
    cout << "logb(" << x << ") = "
         << "log(|" << x << "|) = " << result << endl;
  
    x = 17.6;
  
    result = logb(x);
    cout << "logb(" << x << ") = "
         << "log(|" << x << "|) = " << result << endl;
  
    return 0;
}
输出:
logb(70.56) = log(|70.56|) = 6
logb(17.6) = log(|17.6|) = 4

程序3

// C++ program to illustrate
// to implement logb() function
// when input is 0
#include 
#include 
  
using namespace std;
  
int main()
{
    double result;
    int x = 0;
  
    result = logb(x);
    cout << "logb(" << x << ") = "
         << "log(|" << x << "|) = " << result << endl;
  
    return 0;
}
输出:
logb(0) = log(|0|) = -inf
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”