Any math junkies out there?

  • Greetings. Please consider this simple math problem...

    a = 7, b = 3, c = a^b = 343

    Log(c)/Lob(a) = b

    If given b and c, how do I find a?

    There was probably a time long ago that I knew how to do it, but I do not have access to any math books right now, and I am trying to put this in TSQL, and I can't find the answer. Also, math never was my stong point.

    DECLARE @base INT

    DECLARE @exponent INT

    DECLARE @answer INT

    SELECT @base = 7

    SELECT @exponent = 3

    SELECT @answer = POWER(@base,@exponent) -- 343

    -- Given @answer and @base, it is easy to solve for @exponent, as is shown in BOL.

    SELECT '@exponent = ' + CONVERT(VARCHAR,LOG(@answer)/LOG(@base))

    -- If given @answer and @exponent, how would I solve for @base?

    Greg
    _________________________________________________________________________________________________
    The glass is at one half capacity: nothing more, nothing less.

  • Ok, I knew that pesky e would come in handy some day.

    X^3 = 343

    3 * Ln(x) = Ln(343)

    Ln(x) = 1.945910149

    7 = e^1.945910149

    Problem solved. I will admit straight up that it was simply trial and error with the Log, Ln, and e^x buttons on my calculator, and not any measure of math prowess in any way. Anyhow, now to translate to TSQL. Thanks all.

    Greg
    _________________________________________________________________________________________________
    The glass is at one half capacity: nothing more, nothing less.

  • Maybe I'm just rambling to crickets chirping here, but there does not seem to be a natural log function in SQL Server. LOG10 works just as well though.

    -- Given @answer and @base, solve for @exponent

    SELECT '@exponent = ' + CONVERT(VARCHAR,LOG(@answer)/LOG(@base))

    -- Given @answer and @exponent, solve for @base

    SELECT '@base = ' + CONVERT(VARCHAR,POWER(10,LOG10(@answer)/@exponent))

    Greg
    _________________________________________________________________________________________________
    The glass is at one half capacity: nothing more, nothing less.

  • Looks like LOG() is the natural log. LOG10 is log base 10.

    Keep in mind that LOGnnn(Y) = LOG(Y)/LOG(nnn)

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Matt Miller (#4) (7/7/2011)


    Looks like LOG() is the natural log. LOG10 is log base 10.

    Keep in mind that LOGnnn(Y) = LOG(Y)/LOG(nnn)

    DOH! I was looking for a 'Ln' function like on my calculator. Thanks for the reply Matt.

    Greg
    _________________________________________________________________________________________________
    The glass is at one half capacity: nothing more, nothing less.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply