log2(x)函数详解

首页 / C++入门教程 / log2(x)函数详解

该函数计算给定数字的以2为底的对数。

log2(x) = log2x;

log2 - 语法

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

Note: return_type可以是float,double或long double。

log2 - 参数

x :要计算其对数的值。

log2 - 返回值

参数 返回值
x>1 Positive
x=1 Zero
1>x> 0 Negative
x= 0 -infinity
x<0 Not a Number(nan)

log2 - 例子1

让我们看一个简单的例子,当x的值大于1时。

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

输出:

Value of x is : 2
log2(x) = 1

在此示例中,当x的值大于1时,log2()函数计算以2为底的对数值

log2 - 例子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<<"log2(x) = "<<log2(x);
     return 0;
}.

输出:

Value of x is : 1
log2(x) = 0

在此示例中,当x的值等于1时,log2()函数计算以2为底的对数值。

log2 - 例子3

让我们看一个简单的示例,其中x的值介于0和1之间。

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-math-log2-function.html

来源:LearnFk无涯教程网

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

输出:

Value of x is : 0.2
log2(x) = -2.32193

在此示例中,当x的值等于0.2时,log2()函数计算以2为底的对数值。

log2 - 例子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<<"log2(x) = "<<log2(x);
     return 0;
}

输出:

Value of x is : 0
log2(x) = -inf

在此示例中,当x的值等于0时,log2()函数计算以2为底的对数值。

log2 - 例子5

让我们看一个简单的例子,当x的值小于零时。

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

输出:

Value of x is : -1.5
log2(x) = nan

在此示例中,当x的值小于零时,log2()函数计算以2为底的对数值。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

软件测试52讲 -〔茹炳晟〕

微信小程序全栈开发实战 -〔李艺〕

用户体验设计实战课 -〔相辉〕

物联网开发实战 -〔郭朝斌〕

说透区块链 -〔自游〕

大数据经典论文解读 -〔徐文浩〕

全链路压测实战30讲 -〔高楼〕

B端产品经理入门课 -〔董小圣〕

结构写作力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)