Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Predefined log function in C++?

  1. Feb 1, 2005 #1

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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. jcsd
  3. Feb 1, 2005 #2

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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:
     
  4. Feb 1, 2005 #3

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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.
     
  5. Feb 1, 2005 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Certainly. It's log. It's declared in the cmath header. (math.h if you're using C-style headers)
     
  6. Feb 1, 2005 #5

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    ooh! that's it! I fergot the dern include!!! THANKS!
     
  7. Feb 1, 2005 #6

    plover

    User Avatar
    Homework Helper

    There is a log10 function also.
     
  8. Feb 2, 2005 #7

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    is there? I went with c.o.b. using natural logs cause that's all I could find.
     
  9. Feb 2, 2005 #8

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    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?
     
  10. Feb 2, 2005 #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) );
     
  11. Feb 2, 2005 #10

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    sweet! thanks, gerben.

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

    works just peachy now. :smile:
     
  12. Feb 2, 2005 #11
    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.)
     
  13. Feb 2, 2005 #12

    Math Is Hard

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    ah well...who knows, franz. maybe you'll need that info one day.
     
  14. Feb 2, 2005 #13

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Predefined log function in C++?
  1. Trigama Function in C (Replies: 1)

  2. C gets function (Replies: 7)

  3. C++ Friend Functions (Replies: 2)

Loading...