C/C++ Predefined log function in C++?

  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    Function Log
AI Thread Summary
In C++, the function to calculate the logarithm base 10 of a number is `log10`, which is included in the `cmath` header (or `math.h` for C-style headers). The discussion highlights the change of base formula for logarithms, confirming that `log` can also be used for natural logarithms. A user encountered an issue with using the modulus operator `%` on a double returned by the `pow` function, which calculates powers. The solution involves type-casting the result of `pow` to an integer using `static_cast<int>`, allowing the modulus operation to work correctly. The conversation emphasizes the importance of including the correct header files and understanding type casting in C++.
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,650
Reaction score
39
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
 
Technology news on Phys.org
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. :frown:
 
even if I use C.O.B., is there a predefined log function in C++ at all?

(just saw your edit)

The thing is - I can't find this in my textbook and I thought the teacher had mentioned it in class.
 
Certainly. It's log. It's declared in the cmath header. (math.h if you're using C-style headers)
 
ooh! that's it! I fergot the dern include! THANKS!
 
There is a log10 function also.
 
is there? I went with c.o.b. using natural logs cause that's all I could find.
 
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?
 
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
gerben said:
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) );
sweet! thanks, gerben.

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

works just peachy now. :smile:
 
  • #11
Hurkyl said:
Certainly. It's log. It's declared in the cmath header. (math.h if you're using C-style headers)


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
ah well...who knows, franz. maybe you'll need that info one day.
 
  • #13
Math Is Hard said:
ah well...who knows, franz. maybe you'll need that info one day.


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.
 

Similar threads

Back
Top