在C 中写入对数函数
write a logarithmic function in c++
本文关键字:对数函数 更新时间:2023-10-16
我想编写一个函数来计算一个数字的对数,以c
中的任何基础此功能应该能够计算的对数
#include <bits/stdc++.h>
using namespace std;
int main()
{
int number,base;
int i=0;//this is the counter
double value=0; //the value of the power
cout<<"enter the number : "<<endl;
cin>>number;
cout<<"enter the base : "<<endl;
cin>>base;
while (value<number){//if the value of the power <the number the loop will be continue
value=pow(base,i);
if (value==number) //this if statment to check if the result is correct or not
{
break;
}i+=1;
}cout<<"the result is : "<<i<<endl;//print the result on the screen
return 0;
}
如果要编写对数函数而不使用std libs,则最简单的方法是使用二进制对数
// function to evaluate Binary logarithm
uint16_t log2(uint32_t n) {
if (n == 0) //throw ...
{
//Here we have error
//you can use exception to handle this error or return Zero
throw new exception(std::out_of_range("fault we don't have log2 0"));
}
uint16_t logValue = -1;
while (n) {//
logValue++;
n >>= 1;
}
return logValue;
}
log2函数计算o(log n(复杂性中的二进制对数,并使用此公式计算其他对数。
log b a = log c a/log c b
和您想要的功能(计算任何基本对数(:
// function to evaluate logarithm a base-b
uint32_t log(uint32_t a, uint32_t b)
{
return log2(a) / log2(b);
}
CPP测试的主要功能
#include <math.h>
#include <iostream>
using namespace std;
// driver program to test the above function
int main()
{
uint32_t a, b;
a = 625;
b = 5;
cout << "The logarithm value(base-" << b <<") of " << a
<< " is " << log(a,b) << endl;
a = 1000;
b = 10;
cout << "The logarithm value(base-" << b << ") of " << a
<< " is " << log(a, b) << endl;
a = 243;
b = 3;
cout << "The logarithm value(base-" << b << ") of " << a
<< " is " << log(a, b) << endl;
return 0;
}
输出:
The logarithm value(base-5) of 625 is 4
The logarithm value(base-10) of 1000 is 3
The logarithm value(base-3) of 243 is 7
在math.h
中:
Syntax for returning natural logarithm:
result = log(x)
Syntax for returning logarithm (base-10 logarithm) of the argument.
result = log10(x)
参数可以是任何数据类型的,例如int,double或float或long double。
log((函数根据以下条件返回值:
a) if x>1 then positive
b) if 0<x<1 returns a negative value
c) if x=1 then it returns 0
d) if x=0 then it returns -inf
e) if x<0 then it returns NaN(not a number)
CPP程序以实现log((函数
#include <math.h>
#include <iostream>
using namespace std;
// function to evaluate natural logarithm base-e
double valueE(double d)
{
return log(d);
}
// function to evaluate logarithm base-10
double value10(double d)
{
return log10(d);
}
// driver program to test the above function
int main()
{
double d = 10;
cout << "The logarithm value(base-e) of " << d
<< " is " << valueE(d) << endl;
cout << "The logarithm value(base-10) of " << d
<< " is " << value10(d) << endl;
return 0;
}
输出:
The logarithm value(base-e) of 10 is 2.30259
The logarithm value(base-10) of 10 is 1
相关文章:
- 如何使用布尔函数按降序对数组进行排序?
- 根据比较函数C++对数组进行排序
- C++中一个函数对数组进行搅乱和平方运算的问题
- 在 c++ 中计算对数和 exp 函数
- 如何使用模板函数对数组项进行计数C++同时允许空数组
- 在C 中写入对数函数
- 将对数组的引用传递给返回对数组的引用的函数
- 当我尝试对数组的内容使用函数时,控制台中没有显示任何内容
- 使用C++函数对数组进行排序
- 使用memset函数时,对数组的引用是不明确的错误
- 调用selectionsort函数只对数组的一部分进行排序
- C++中对数正态分布的累积函数的逆
- 为什么构造函数没有得到对数组中第二个对象的调用
- C++使用单独的函数按降序对数组进行排序
- 使用一组规则对数组进行洗牌,并从函数返回值
- 存储在静态常量整数 (C++) 中的对数函数的错误返回值
- 调用对数组引用两次的函数
- C++函数返回对数组的引用
- 向具有可变模板的函数传递对数组的可变引用数
- 用于输出列中对数函数结果的合适循环(c++)