当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ logb()用法及代码示例


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 <cmath> 
#include <iostream> 
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 <cmath> 
#include <iostream> 
  
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 <cmath> 
#include <iostream> 
  
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


相关用法


注:本文由纯净天空筛选整理自IshwarGupta大神的英文原创作品 logb() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。