# Predefined log function in C++?

1. Feb 1, 2005

### Math Is Hard

Staff Emeritus
Is there a predefined function in C++ that will allow me to take LOG(base 10) of a number? If not, is there a reasonably simple work-around?

thanks,

MIH

2. Feb 1, 2005

### Hurkyl

Staff Emeritus
The change of base formula.

loga x = (logb x) / (logb a)

Now, it would surprise me if there isn't a built in base 10 log... maybe it's log10? I don't have the documentation installed on this computer, and the other one isn't hooked up.

3. Feb 1, 2005

### Math Is Hard

Staff Emeritus
even if I use C.O.B., is there a predefined log function in C++ at all?

The thing is - I can't find this in my textbook and I thought the teacher had mentioned it in class.

4. Feb 1, 2005

### Hurkyl

Staff Emeritus
Certainly. It's log. It's declared in the cmath header. (math.h if you're using C-style headers)

5. Feb 1, 2005

### Math Is Hard

Staff Emeritus
ooh! that's it! I fergot the dern include!!! THANKS!

6. Feb 1, 2005

### plover

There is a log10 function also.

7. Feb 2, 2005

### Math Is Hard

Staff Emeritus
is there? I went with c.o.b. using natural logs cause that's all I could find.

8. Feb 2, 2005

### Math Is Hard

Staff Emeritus
There's one little problem I'm still trying to solve. There's a place in the program where I want to set my int variable "num" to the remainder I get after dividing num by a power of 10, the exponent of which being determined by another int variable I have called "expo".

num = num % (pow(10, expo));

but I get this error: "'%' : illegal, right operand has type 'double'"

I was wondering if there was some way to use static_cast here to fix the problem?

9. Feb 2, 2005

### gerben

yes there is
pow() apparently returns a double, but you can change the type of the returned variable by type-casting it to an int like this:

num = num % static_cast<int>( pow(10, expo) );

10. Feb 2, 2005

### Math Is Hard

Staff Emeritus
sweet! thanks, gerben.

I was trying to use that but I must have been getting the syntax wrong.

works just peachy now.

11. Feb 2, 2005

### franznietzsche

this is why i should read first, because i just spent 15 minutes trying to figure out which header it was in (i don't use C++ much anymore, between F90 and Perl i don't need it.)

12. Feb 2, 2005

### Math Is Hard

Staff Emeritus
ah well...who knows, franz. maybe you'll need that info one day.

13. Feb 2, 2005

### franznietzsche

Well i'd used it before, so i knew it existed, its just been a while becuase i find F90 easier for calculation work, which is 75% of all the programming i do. But its perfectly possible that'll use it again at some point.