A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Math.pow does not accept number under zero ?

  1. #1
    Senior Front End Developper
    Join Date
    Dec 2000
    Location
    Montréal
    Posts
    568
    Is there a way to use number under zero in the .pow function?

    Here what I tried...

    var temp = Math.pow ( 5, 2 ) ;
    var temp2 = Math.pow (-5, 2) ;
    trace(temp)
    trace(temp2)


    and the output is:

    25
    NaN


    the only way to use it with any number is to do this:

    var temp2 = Math.pow (Math.abs(-5), 2);

    but this is not natural...

    anyone can explain why this function don't work with number under zero?

    Thanks in advance
    CharlesFK


  2. #2
    Denied ACCESSORIES
    Join Date
    Dec 2000
    Location
    Rio de Janeiro
    Posts
    208
    Originally posted by CharlesFK
    Is there a way to use number under zero in the .pow function?

    Here what I tried...

    var temp = Math.pow ( 5, 2 ) ;
    var temp2 = Math.pow (-5, 2) ;
    trace(temp)
    trace(temp2)


    and the output is:

    25
    NaN


    the only way to use it with any number is to do this:

    var temp2 = Math.pow (Math.abs(-5), 2);

    but this is not natural...

    anyone can explain why this function don't work with number under zero?

    Thanks in advance
    CharlesFK

    I would also like to know the answer to this as well as ny work around.... so far if using .abs and if the number was rased to an odd number it would be a negative in which case ..... still need a fix

  3. #3
    Senior Member
    Join Date
    Jun 2000
    Posts
    911
    Macromedia updated their players just recently and they fixed that bug. However, if you still want a work around this is something I wrote when I found out:
    Code:
    // Calculates a^p
    function power (a, p) 
    {
    	if (a < 0 && p > 0 && Math.floor (p / 2) == p / 2)
    		return Math.pow (a * -1, p);
    	else if (a < 0 && p > 0 && (Math.floor (Math.floor (p) / 2) != Math.floor (p) / 2))
    		return Math.pow (a * -1, p) * -1;
    	else if (a < 0 && p < 0 && Math.floor (p / 2) == p / 2)
    		return Math.pow (a * -1, p);
    	else if (a < 0 && p < 0)
    		return Math.pow (a * -1, p) * -1;
    	else if (a > 0)
    		return Math.pow (a, p);
    	else
    		return 0;
    }
    It works...it may not be the best way, but it works.

    Good luck.

  4. #4
    Junior Member
    Join Date
    Jan 2002
    Posts
    6

    use this

    Math.pow2 = function( nBase, nExp )
    {
    var v = Math.exp(nExp*Math.log(Math.abs(nBase)));
    v *= nBase<0&&!(nExp%2) ? 1 : -1;
    v *= !(nBase>0) ? 1 : -1;
    return v;
    }

    kyunghoonkang@hotmail.com

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