Predefined log function in C++?

  • C/C++
  • Thread starter Math Is Hard
  • Start date
  • Tags
    Function Log
In summary, the conversation discusses the use of the LOG function in C++ and a work-around for taking LOG(base 10) of a number. The change of base formula is mentioned and it is suggested to use the log function in the cmath header. The use of static_cast to fix an error involving the type of the returned variable from the pow function is also discussed. Finally, the conversation ends with the possibility of needing this information in the future.
  • #1
Math Is Hard
Staff Emeritus
Science Advisor
Gold Member
4,652
37
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
  • #2
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:
 
  • #3
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.
 
  • #4
Certainly. It's log. It's declared in the cmath header. (math.h if you're using C-style headers)
 
  • #5
ooh! that's it! I fergot the dern include! THANKS!
 
  • #6
There is a log10 function also.
 
  • #7
is there? I went with c.o.b. using natural logs cause that's all I could find.
 
  • #8
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
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.
 

What is the predefined log function in C++?

The predefined log function in C++ is a mathematical function that calculates the natural logarithm (base e) of a given number. It is part of the cmath library and can be used to perform logarithmic calculations in a C++ program.

How is the log function used in C++?

The log function in C++ is used to calculate the logarithm of a given number. It takes in one argument, the number whose logarithm is to be calculated, and returns the result as a double data type. For example, log(10) would return the value 2.302585, as 10 is the power to which e (approximately 2.71828) must be raised to get 10.

What is the difference between log and log10 in C++?

The log function in C++ calculates the natural logarithm (base e) of a given number, while the log10 function calculates the logarithm (base 10) of a given number. In other words, log(x) is equivalent to log10(x)/log10(e).

Can the log function in C++ handle negative numbers?

Yes, the log function in C++ can handle negative numbers. It follows the mathematical definition of a logarithm and can calculate the logarithm of any non-zero real number. However, attempting to calculate the logarithm of 0 will result in an error.

Are there any limitations to using the log function in C++?

There are a few limitations to using the log function in C++. Firstly, it can only handle real numbers, so attempting to use it on complex numbers will result in an error. Additionally, it may not always provide an exact result due to the limitations of floating-point arithmetic. Finally, the log function may not be the most efficient way to calculate logarithms, as it involves multiple calculations and may be slower compared to other methods.

Similar threads

  • Programming and Computer Science
Replies
8
Views
168
  • Programming and Computer Science
Replies
2
Views
294
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • General Math
2
Replies
44
Views
3K
  • Materials and Chemical Engineering
Replies
1
Views
3K
  • Linear and Abstract Algebra
Replies
1
Views
634
  • Set Theory, Logic, Probability, Statistics
Replies
3
Views
815
  • Feedback and Announcements
Replies
3
Views
847
Back
Top