Predefined log function in C++?

  • Context: C/C++ 
  • Thread starter Thread starter Math Is Hard
  • Start date Start date
  • Tags Tags
    Function Log
Click For Summary

Discussion Overview

The discussion revolves around the availability of predefined logarithmic functions in C++, specifically focusing on the base 10 logarithm. Participants explore the existence of such functions, potential workarounds, and related programming issues.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant inquires about a predefined function for base 10 logarithm in C++ and suggests the possibility of a workaround.
  • Another participant introduces the change of base formula for logarithms and speculates about the existence of a built-in base 10 log function, possibly named log10.
  • There is a confirmation that log is available in the cmath header, with a note on using math.h for C-style headers.
  • Some participants express confusion about the availability of logarithmic functions and their documentation.
  • Participants discuss an issue related to using the pow function, where the result is a double, and how to handle type casting to resolve an error with the modulus operator.
  • There are multiple confirmations regarding the use of static_cast to convert the result of pow to an integer for modulus operations.

Areas of Agreement / Disagreement

Participants generally agree on the existence of the log function and its declaration in the appropriate header. However, there is some uncertainty about the availability of a base 10 logarithm function and the correct usage of logarithmic functions in C++. The discussion includes multiple viewpoints on the best practices for handling logarithmic calculations and type casting.

Contextual Notes

Some participants mention difficulties in finding information in textbooks and documentation, indicating potential limitations in available resources or personal familiarity with C++. There is also a reference to varying experiences with different programming languages, which may affect participants' understanding of C++ functions.

Who May Find This Useful

This discussion may be useful for C++ programmers, particularly those interested in mathematical functions, type casting, and handling errors related to data types in programming.

Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
Messages
4,663
Reaction score
36
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 because 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

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
12
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K