0

I want to write a function to calculate the logarithm of a number to any base in c++
this function should be able to calculate the logarithm of any numbers in any base

#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;
}
amin saffar
  • 1,953
  • 3
  • 22
  • 34
kilany
  • 21
  • 1
  • 1
  • 3
  • 4
    And your question is? – NathanOliver Mar 27 '18 at 17:59
  • 2
    What research have you done? Do you know how to do the calculations on paper? Start with that. – Some programmer dude Mar 27 '18 at 18:00
  • 2
    Oh, and [don't include ``](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Some programmer dude Mar 27 '18 at 18:01
  • 1
    Combine the existence of [`std::log`](http://en.cppreference.com/w/cpp/numeric/math/log), with your math (log) knowledge, to get the result you want. Hint: The solution takes up 1 line. – Algirdas Preidžius Mar 27 '18 at 18:01
  • 1
    Lastly, welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Mar 27 '18 at 18:09
  • If you can calculate the natural logarithm then you can calculate the logarithm in any base since : `log_a(x) = ln(x)/ln(a)` – Omar Aflak Mar 27 '18 at 19:38
  • If you can calculate the log in ANY base, then it is possible to calculate the log in any other base. If `log_foo()` is the log in base `foo` and `log_a` is the base in base `a`, then `log_a(x) = log_foo(x)/log_foo(a)`. And the standard library has `log()` which calculates natural logarithm as well as `log10()` which calculates common log (aka log to base `10`). [assuming bases are positive values, not equal to 1] – Peter Mar 27 '18 at 19:59
  • see [Building a logarithm function in C without using float type](https://stackoverflow.com/a/42108287/2521214) and the links in there it might shine some light on the topic. if you can compute `log2` you can convert it to any base by simple multiplication by constant ... – Spektre Mar 28 '18 at 07:49

1 Answers1

3

If you want to write the logarithm function without using the std libs,the simplest way is useing 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 function calculate Binary logarithm in O(log n) complexity, and use this formula to calculate other logarithms.

logb a = logc a / logc b

And here function you want (calculate any base logarithms):

// function to evaluate logarithm a base-b
uint32_t log(uint32_t a, uint32_t b)
{
    return log2(a) / log2(b);
}

CPP main function for testing

#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;
}

Output:

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

In math.h:

Syntax for returning natural logarithm:
result = log(x)

Syntax for returning logarithm (base-10 logarithm) of the argument.
result = log10(x)

The parameters can be of any data-type like int, double or float or long double.

Log() function returns value according to the following conditions:

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 program to implement log() function

#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;
}

Output:

The logarithm value(base-e) of 10 is 2.30259
The logarithm value(base-10) of 10 is 1
amin saffar
  • 1,953
  • 3
  • 22
  • 34
  • 1
    The OP wants to "make a function to calculate the logarithm of a number to **any** base" (emphasis mine). So this doesn't really help that much. And as I commented to the OP, [don't include ``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Some programmer dude Mar 27 '18 at 18:24
  • @user4581301 I update my code now. check it again – amin saffar Mar 27 '18 at 19:04
  • `log(0)` looks like a candidate for `std::range_error`. Throwing string literals is a bit unusual. But this is a reasonable first try. For a more serious attempt (and a StackOverflow-worthy answer) there's the matter of rounding. Your `log2` function rounds down, and your `log` uses these rounded results in an integer division which also rounds down. In particular, `log(7*7*7, 7)` should produce 3 but calculates `8/2==4` as 256<343<512 and 4<7<8. – MSalters Mar 28 '18 at 08:04