A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Logarithm problem

  1. #1
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,182

    Logarithm problem

    Hi guys,

    I've realy lost all me Math skills and now I can't solve the folowing calculation:
    p = f^s

    Where 'f' is the unknown value I'd like to get. I know I should be using Logarithms but I totaly forgot how to use them. Also could someone tell me how to get the answer to this working in Flash?

    Any help would be appreciated.

    Thanks,
    ~Sph

  2. #2
    Junior Member
    Join Date
    Nov 2006
    Posts
    9
    You don't (shouldn't) actually need to use logs. The easiest way to calculate f for p = f^s is to get the s-th root of p. You would do this by doing Math.pow(p, 1/s); However the Math.pow implementation in flash is not mathematically correct. It is buggy in the way that if you do Math.pow(-1, 1/3) it returns NaN. It should return the third root of -1 (which is -1). To correct this you will need to code your own Math.pow function and fix the bug, or hope that no one ever enters a negative value for p (not advisable).

    You can also do it by using logs. The log formula is f = e^((ln p)/s) or in flash var f:Number = Math.pow(Math.E, Math.log(p)/s); When coding this there are special circumstances you will need to take care of here too though, such as when p is negative you will need to do var f:Number = -1 * Math.pow(Math.E, Math.log(-1*p)/s); instead, as you cannot get the log of a negative number. Similarly you need to be careful of when s is zero as you're dividing by s, and when p is 0 as you cannot get the log of 0.

    For future reference, the way I calculated the log formula was:
    Take natural logs of both sides, this gives: ln p = ln (f^s)
    Then use the fact that ln (a^b) = b ln a to get ln p = s ln f
    Then just use that log_a b = c is the same as a^c = b to get what we have. There is a Math.E because I choose to use base E. You could use any base you like, however the only built in log function in flash is log to the base E, so you would have to rebase the calculations to base E if you wanted to use any other base.

    HTH

  3. #3
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,182
    Thanks a lot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center