【问题标题】:write a logarithmic function in c++ [closed]用c ++编写对数函数[关闭]
【发布时间】:2018-03-27 17:58:14
【问题描述】:

我想在 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;
}

【问题讨论】:

标签: c++ algorithm visual-c++ logarithm


【解决方案1】:

如果你想在不使用标准库的情况下编写对数函数,最简单的方法是使用Binary logarithm

// 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) 复杂度计算二进制对数, 并使用此公式计算其他对数。

logb a = logc a / logc 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

【讨论】:

  • OP 想要“创建一个函数来计算一个以 any 为底的数字的对数”(强调我的)。所以这并没有太大帮助。正如我对 OP 的评论,don't include &lt;bits/stdc++.h&gt;.
  • @user4581301 我现在更新我的代码。再检查一遍
  • log(0) 看起来像是 std::range_error 的候选者。抛出字符串文字有点不寻常。但这是一个合理的第一次尝试。对于更认真的尝试(以及值得 StackOverflow 的答案),有四舍五入的问题。您的 log2 函数向下舍入,而您的 log 将这些舍入结果用于整数除法,该整数除法也向下舍入。特别是,log(7*7*7, 7) 应该产生 3,但将 8/2==4 计算为 256
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2022-01-02
  • 1970-01-01
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
相关资源
最近更新 更多