Thread: Quick question on mathematical notation (translating to C)

  1. #1
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151

    Quick question on mathematical notation (translating to C)

    I came across a mathematical statement in the form of:

    (log x)^-1 log y log log z

    Is that equivalent to the C expression:

    pow(log(x), -1)*log(y)*log(log(z))
    Last edited by gardhr; 09-16-2011 at 09:07 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by gardhr View Post
    I came across a mathematical statement in the form of:

    (log x)^-1 log y log log z

    Is that equivalent to the C expression:

    pow(log(x), -1)*log(y)*log(log(z))
    Not sure, maybe it is:

    Code:
    pow(log(x), -1)*log(y*log(log(z)))
    I just can't remember the mathematical precedence of logarithms.

    EDIT: By "remember" I mean "recall", that is.
    Last edited by GReaper; 09-16-2011 at 09:32 PM.
    Devoted my life to programming...

  3. #3
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Hmm. Using Google's calculator, it resolves to the code that I posted, with one important difference: it's assuming I mean log10, not the natural logarithm. Is that a fairly standard assumption in mathematical notation?

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    According to the standard([EDIT]: Copy of C99 that I have[/EDIT]) log(x) computes log base e (natural) log of x. log10 functions return the log base 10 of x.
    Last edited by AndrewHunter; 09-16-2011 at 10:23 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    I'm pretty sure that's what the equation in question implies, too, but there does seem to be some inconsistencies in convention. For example, you often see "ln" used to distinguish natural logarithm, and now that I think of it, most hand calculators use "log" for log10. Argh!

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by gardhr View Post
    I'm pretty sure that's what the equation in question implies, too, but there does seem to be some inconsistencies in convention. For example, you often see "ln" used to distinguish natural logarithm, and now that I think of it, most hand calculators use "log" for log10. Argh!
    There have been no "inconsistencies" according to the standard, AFAIK.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by gardhr
    it's assuming I mean log10, not the natural logarithm. Is that a fairly standard assumption in mathematical notation?
    If log rather than lg or ln is used, I think that that depends on context. Perhaps the author specified the base elsewhere, or the base does not matter in the big picture (e.g., this is part of some notation for algorithmic complexity), or the author assumed the base due to convention in his/her specialisation.

    Incidentally, I would translate x^-1 as (1/x) to avoid a function call that seems overkill.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by gardhr View Post
    I'm pretty sure that's what the equation in question implies, too, but there does seem to be some inconsistencies in convention. For example, you often see "ln" used to distinguish natural logarithm, and now that I think of it, most hand calculators use "log" for log10. Argh!
    You should specify "C code" and "math code" here then.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Assuming ^ is "to power of", then p^-1 is equivalent to 1/p (if p is a real or complex value).

    As to "log y log log z" you need to be clear what that means. It is ambiguous, mathematically, as brackets or anything else that might give context (such as font sizes) have been left out. It could be log(y)*log(log(z)) or it could be log(y*log(log(x))

    Mathematically, "log" can mean common logarithm (log10() in C) or natural logarithm (log() in C). Conventionally, common logarithm is the more usual meaning. But, to be sure, you need to read the article (or whatever) that you got the formula from.
    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.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by grumpy View Post
    Assuming ^ is "to power of", then p^-1 is equivalent to 1/p (if p is a real or complex value).

    As to "log y log log z" you need to be clear what that means. It is ambiguous, mathematically, as brackets or anything else that might give context (such as font sizes) have been left out. It could be log(y)*log(log(z)) or it could be log(y*log(log(x))

    Mathematically, "log" can mean common logarithm (log10() in C) or natural logarithm (log() in C). Conventionally, common logarithm is the more usual meaning. But, to be sure, you need to read the article (or whatever) that you got the formula from.
    Thanks as always for spelling out what I failed to do.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Okay, so I guess I'll assume log == natural logarithm since that seems to be the most common usage.

    I'm pretty sure now that the correct interpretation here is log(y)*log(log(z)), too, from the context of the article.

    Thanks for the input everyone!

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by gardhr
    I'll assume log == natural logarithm since that seems to be the most common usage.
    Then when it turns out that your assumption is wrong... if the base really was not specified anywhere, is there an example of the calculation that you can refer to? Is this "pure" mathematics? Is this engineering?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Umm....Laser the base is assumed per the standard....: confused : <on purpose>
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by laserlight View Post
    Then when it turns out that your assumption is wrong... if the base really was not specified anywhere, is there an example of the calculation that you can refer to? Is this "pure" mathematics? Is this engineering?
    I'm basing it on the heuristic that most papers written about number theory seem to use that convention.

    Specifically, it's a lower bound given for the Miller-Rabin primality test, described here. I'm trying to work out an approximation using only integer math.

  15. #15
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by AndrewHunter View Post
    Umm....Laser the base is assumed per the standard....: confused : <on purpose>
    Yes, but common practical usage varies (depending on the discipline), as laserlight pointed out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. big O notation question
    By l2u in forum C++ Programming
    Replies: 7
    Last Post: 11-08-2008, 03:53 PM
  3. [OT] help translating and phrazing
    By jabka in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2007, 11:37 AM
  4. Code translating
    By Liger86 in forum C Programming
    Replies: 3
    Last Post: 12-27-2002, 09:31 AM
  5. Mathematical Question
    By rmullen3 in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2001, 09:44 PM