C math log() function misbehaves...

This is a discussion on C math log() function misbehaves... within the C Programming forums, part of the General Programming Boards category; Hi! , here's my code Code: #include <math.h> #define M_PI 3.14159265358979323846 // Calculate Constant double constant() { const double figure ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    C math log() function misbehaves...

    Hi! , here's my code

    Code:
    #include <math.h>
    #define M_PI 3.14159265358979323846
    
    // Calculate Constant
    double constant()
    {
    	const double figure = 7.9454E-18;
    	double number = log (figure);
    	return -10 * number ;
    }
    The value in number should be -17.099... But i get -39.379... What's the problem here?

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    5
    The log function uses base e and you want base 10. The conversion is log(x) / log(10)

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably want log10(), not log(), the latter gives you the natural logarithm, and e^-39 is about 7.9E-18.

    If you don't have log10() in your math library, log(x)/log(10) will be the same.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You are looking for the base 10 log but the log() function returns the natural (base e) log.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    And the wastage of brain power continues - http://www.daniweb.com/forums/thread125552.html

    http://www.catb.org/~esr/faqs/smart-...ons.html#forum
    Don't ask trivial questions on multiple forums. All you're going to get is a bunch of near identical answers on all of them.

    It's fine to ask the same question on another forum if one doesn't provide the answer in a couple of days. But to broadcast it all over the place at the same time just smacks of http://www.catb.org/~esr/faqs/smart-...ns.html#urgent
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Salem View Post
    And the wastage of brain power continues - http://www.daniweb.com/forums/thread125552.html

    http://www.catb.org/~esr/faqs/smart-...ons.html#forum
    Don't ask trivial questions on multiple forums. All you're going to get is a bunch of near identical answers on all of them.

    It's fine to ask the same question on another forum if one doesn't provide the answer in a couple of days. But to broadcast it all over the place at the same time just smacks of http://www.catb.org/~esr/faqs/smart-...ns.html#urgent
    Thanx for the info even though not helpful to my question... but i guess you learn few 'other' things by posing questions, and don't mind being reprimanded...
    Last edited by csonx_p; 05-23-2008 at 12:38 AM.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by fredb View Post
    The log function uses base e and you want base 10. The conversion is log(x) / log(10)
    You are looking for the base 10 log but the log() function returns the natural (base e) log.
    Know, i'm not looking for the base 10, i know in math.h the function log10() exist... I used the calculator to test the log of 7.9454E-18... Now as you all noticed from Daniweb forum some1 spoke about common algorithm and natural algorithm, how does log() in math.h relate to log function in the calculator and the log function from Excel spreadsheet? The -17.099 results as i said were found using scientific calculator and the LOG() function in excel 2003....

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, logarithms exist in "every base" - all of them work the same, x = base**logbase(x). One of those is the natural logarithm (with base e, where e = ~ 2.72). On your calculator, you probably have a "ln" button, which will give you the natural logarithm. And as explained earlier, log of baseX can be converted to log of baseY by dividing log baseX(value) / log baseX(baseY).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Right, logarithms exist in "every base" - all of them work the same, x = base**logbase(x). One of those is the natural logarithm (with base e, where e = ~ 2.72). On your calculator, you probably have a "ln" button, which will give you the natural logarithm. And as explained earlier, log of baseX can be converted to log of baseY by dividing log baseX(value) / log baseX(baseY).

    --
    Mats
    Hi matsp, is it correct to say ln() in a calculator is the log() function in C, and log() in calculator is the log10() function in C....

    If so, then something is not clear on how excel log functions work... LOG() & LOG10() both always yield same results... both LOG(7.9454E-18) & LOG10(7.9454E-18) yield to -17.099... Thing is i've been given a spreadsheet with all data & calculations/formulars and have to translate it into a program in C. Now what i did is i presumed whenever i see LOG() in spreadsheet i should use
    log() in C and LOG10 should be log10(), which of cause is not necessary true in this case.... Learnt something though

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Excel does have two log function, the LOG() function would, in C++ style be defined as:
    Code:
    double log(double x, double base = 10);
    The log10() is shorthand for Log(x, 10).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Excel does have two log function, the LOG() function would, in C++ style be defined as:
    Code:
    double log(double x, double base = 10);
    The log10() is shorthand for Log(x, 10).

    --
    Mats
    Think you didn't get me well.. Do you mind testing both Excel LOG(7.9454E-18) & LOG10(7.9454E-18) and see if you get different results? That's where my problem is, i get same results...

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Think you didn't get me well.. Do you mind testing both Excel LOG(7.9454E-18) & LOG10(7.9454E-18) and see if you get different results? That's where my problem is, i get same results...
    If I do, I suspect that means that I should send a bug report to Microsoft (unless the difference is in the 11th decimal point or so).

    What I was trying to say is that log(x) == log10(x) in excel, but it also allows two parameters to log, e.g. log(8, 2), which would give you log2(8) [which is three]. So if you give only one parameter, you get log10, if you give two parameters, log(x, y) you get logy(x).

    Does that make more sense?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    If I do, I suspect that means that I should send a bug report to Microsoft (unless the difference is in the 11th decimal point or so).

    What I was trying to say is that log(x) == log10(x) in excel, but it also allows two parameters to log, e.g. log(8, 2), which would give you log2(8) [which is three]. So if you give only one parameter, you get log10, if you give two parameters, log(x, y) you get logy(x).

    Does that make more sense?

    --
    Mats
    Oh! eish... it's friday so forgive me..

    Thnx

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    If I do, I suspect that means that I should send a bug report to Microsoft (unless the difference is in the 11th decimal point or so).

    What I was trying to say is that log(x) == log10(x) in excel, but it also allows two parameters to log, e.g. log(8, 2), which would give you log2(8) [which is three]. So if you give only one parameter, you get log10, if you give two parameters, log(x, y) you get logy(x).

    Does that make more sense?

    --
    Mats
    Interesting this

    The Log base 10....

    In C/C++: log10(x)... Excel: LOG10(x) == LOG(x) == LOG(x, 10),... Sci Calc: log

    Log base e....

    In C/C++: log(x) ... Excel: LN(x)... Sci Calc: ln

    Can be confusing if using all to test a function...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21