Thread: (un)natural logarithm function using the wrong base

  1. #1
    Registered User Gerling's Avatar
    Join Date
    Aug 2010
    Posts
    4

    (un)natural logarithm function using the wrong base

    The following code produces logarithms apparently using base 2.71826 instead of base 10. What could I be doing wrong in a function so simple?
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    { double d = 100.0;
      double num;
        printf("Ready\n");
        scanf("%g", &num);
        printf("log (%g) = %g\n", num, log(num));
        printf("log (%g) = %g\n", d, log(d));
        return (0);
    }
    Input to scanf:
    4
    Output
    Code:
    Ready
    4  
    log (1.21742e-306) = -704.394
    log (100) = 4.60517
    Last edited by Gerling; 08-12-2010 at 09:25 AM. Reason: Forgot input and ouput

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Which compiler are you using?

    What results do you get?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I think you're looking for log10.
    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

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    log - C++ Reference


    ^ is the natural logarithm

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Just FYI, you can calculate the logarithm of a number to any base using log(number)/log(base).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User Gerling's Avatar
    Join Date
    Aug 2010
    Posts
    4
    Wow. Thanks for telling me about log10. I got confused and thought log base 10 was natural. Now I remember what e is.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Common logarithm is to base 10. Natural logarithm is to base e (approximately 2.71828...).

    Mathematically the natural log of x is often described using the notation ln(x) - goes to show that the people who specified the C standard library were not actually mathematicians.

    For those familiar with calculus: the term "natural" comes the fact that the derivative of ln(x) with respect to x is 1/x.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. On the wrong track for a Function Prototype
    By Mohammed in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2003, 01:16 PM

Tags for this Thread