Thread: calculate the ln of a number?

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    calculate the ln of a number?

    hey, i was wondering if there was a function you could use to find the ln of a number ( such as: ln(2) ). i've done some searching on google about it, but either i've been using the wrong keywords or i didn't look deep enough, lol. i also tried declaring a double and having this:
    Code:
    lntwo = ln(2);
    which..doesn't work. lol thanks
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    lntwo = log(2);

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Code:
    log(2) != ln(2)
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Then you need to base convert it.
    Code:
    log(2) / log(2.7182)
    http://mathforum.org/library/drmath/view/55568.html
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    http://www.rt.com/man/log.3.html
    The log() function returns the natural logarithm of x.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
       double lntwo = log(2);
       printf("lntwo = %g\n", lntwo);
       return 0;
    }
    
    /* my output
    lntwo = 0.693147
    */
    Last edited by Dave_Sinkula; 04-04-2005 at 02:43 PM. Reason: Added code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Ha...maybe you don't. My bad.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    awesome thank you
    Registered Linux User #380033. Be counted: http://counter.li.org

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Hehe I was wondering if you'd fail for that

    Remember in higher mathematics when you say log you are really saying ln not log10

  9. #9
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Thantos
    Hehe I was wondering if you'd fail for that

    Remember in higher mathematics when you say log you are really saying ln not log10
    Well it depends, many universitybooks log for base e (2.71...) denote like ln while log is actuallly log10. That is on also used on windows calculator. But in programming world and because of programming world log denots ln not log10. For example in MATLAB, C and C++ (and many others prog languages) log means ln.

    And just in case you wondering about pianorain's formula consider this problem.
    How to calculate logarithm log4(15) (base is 4)?
    Simple
    Let log4(15) is equal x so x = log4(15). Now that means 4^x = 15.
    Now if we take logarithm (base e ln ) we have ln(4^x) = ln(15)=>
    x*ln(4) = ln(15) (logarithms property)=> x=ln(15)/ln(4). And remeber now what x is?
    x is log4(15). That means we got a formula for logarithms converting: log4(15) = ln(15)/ln(4).
    On you0re calaculator you usually don't have opportunity to calculate logarithms for different bases (usually e and 10) so knowing this makes calculation of logarithm of any base simple.
    I don't know why I wrote this.
    Maybe someone will find it useful.

    Micko
    Last edited by Micko; 04-04-2005 at 03:59 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well it depends, many universitybooks log for base e (2.71...) denote like ln while log is actuallly log10. That is on also used on windows calculator. But in programming world and because of programming world log denots ln not log10. For example in MATLAB, C and C++ (and many others prog languages) log means ln.
    I haven't see log in my text books since trig or so. You know why log is ln in programming langauges? Because they were created by mathematicans

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM