Hafiz - 10 days ago 6

C++ Question

I want to calculate the value of ** log x(n)** where

I have tried to do like that but it says "log x was not declared".

`int x,n;`

cin>>x>>n;

cout<<logx(n);

x=1,2,3,4,5,6,7,8........ and

n= Any positive integer.

Answer

Is there any library function in c++ to do the operation?

There is no such function in the standard library. However, it is easy to implement and therefore an implementation may exist in another library.

Or i need to do it manually?

You *can* do it manually.

If I need to do it manually how can i do that?

Using the magic of maths. The following equivalence makes the implementation easy, using the natural logarithm function that is provided by the c++ standard library.

log

_{b}(x) = log_{e}(x) / log_{e}(b)

Where `x`

is any number greater than 0, log_{b} is logarithm in base `b`

, and log_{e} is the natural logarithm.

I have tried to do like that but it says "log x was not declared".

That is a likely result of trying to guess a name of a function. I would recommend to instead check the reference or a book for what functions are available.

Source (Stackoverflow)

Comments