A Flash Developer Resource Site

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

1. 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?

CharlesFK  Reply With Quote

2. 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?

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   Reply With Quote

3. 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.  Reply With Quote

4. ## 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  Reply With Quote

#### Posting Permissions

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

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 