2

I want to calculate the value of log x(n) where x is base and n is any integer. Is there any library function in c++ to do the operation? Or i need to do it manually? If I need to do it manually how can i do that? I have tried to do like that but it says "log x was not declared".

int x,n;
cin>>x>>n;
cout<<logx(n);

enter image description here

x=1,2,3,4,5,6,7,8........ and
n= Any positive integer.

  • https://en.wikipedia.org/wiki/Logarithm see Change of base – 463035818_is_not_an_ai Nov 23 '16 at 14:18
  • 7
    `log(n) / log(x)`? Fore instance `log10(n) == log(n) / log(10)` – Dmitry Bychenko Nov 23 '16 at 14:18
  • logx(n) here x is base and n is any integer. –  Nov 23 '16 at 14:20
  • btw your code looks a bit odd. How is the compiler supposed to know what is the value of `x` from this call alone :`logx(n)` ? If such a function exists it would need to take 2 arguments – 463035818_is_not_an_ai Nov 23 '16 at 14:22
  • yeah. it was my question. if there any function exist? I just give what i tried to do. i know it is wrong. i just want to what is correct. thanks –  Nov 23 '16 at 14:25
  • 1
    If you know its wrong, why include a really big screenshot of it? – Borgleader Nov 23 '16 at 14:30
  • sorry. i thought it is a site of finding the solve of a given problem. But all i see is arguing for the style of the post. // i gave the screen shot because stack overflow didn't allowed me to give a post without a proof of doing some thing. –  Nov 23 '16 at 14:35
  • 1
    Welcome to Stack Overflow! Your image of text [isn't very helpful](//meta.unix.stackexchange.com/q/4086). It can't be read aloud or copied into an editor, and it doesn't index very well, meaning that other users with the same problem are less likely to find the answer here. Please [edit] your post to incorporate the relevant text directly (preferably using copy+paste to avoid transcription errors). – Toby Speight Nov 23 '16 at 15:07

2 Answers2

3

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.

logb(x) = loge(x) / loge(b)

Where x is any number greater than 0, logb is logarithm in base b, and loge 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.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
eerorika
  • 232,697
  • 12
  • 197
  • 326
3

You will have to change the base manually. C++ provides these logarithmic functions:

You can use the Change of Base trick to find your value, because of optimizations you'll want to use log2 to do this.

So for example if you want to do log13(42) you could do:

log2(42) / log2(13)
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • Yeah. i got my answer. Thanks :) –  Nov 23 '16 at 14:41
  • Can you explain why you would use `log2()` rather than `log()` to do the change of base? Is one implemented in hardware and the other in software on some platforms? (If so, which platforms?) – Toby Speight Nov 23 '16 at 15:17
  • @TobySpeight Since all the numbers that we work with are stored in base *2* on the computer, this is as simple as getting the position of the Most-Significant-Bit. That's a single cycle instruction: http://stackoverflow.com/a/994709/2642059 – Jonathan Mee Nov 23 '16 at 15:29
  • Sorry, I forgot this was all in integers - when I see mathematical functions, I tend to assume floating-point inputs of some kind. I should read the question more carefully! – Toby Speight Nov 23 '16 at 16:13