View comments | RSS feed

pow (Math.pow method)

public static pow(x:Number, y:Number) : Number

Computes and returns x to the power of y.

Availability: ActionScript 1.0; Flash Player 5 - In Flash Player 4, the methods and properties of the Math class are emulated using approximations and might not be as accurate as the non-emulated math functions that Flash Player 5 supports.

Parameters

x:Number - A number to be raised to a power.

y:Number - A number specifying a power the parameter x is raised to.

Returns

Number - A number.

Example

The following example uses Math.pow and Math.sqrt to calculate the length of a line.

this.createEmptyMovieClip("canvas_mc", this.getNextHighestDepth());
var mouseListener:Object = new Object();
mouseListener.onMouseDown = function() {
    this.origX = _xmouse;
    this.origY = _ymouse;
};
mouseListener.onMouseUp = function() {
    this.newX = _xmouse;
    this.newY = _ymouse;
    var minY = Math.min(this.origY, this.newY);
    var nextDepth:Number = canvas_mc.getNextHighestDepth();
    var line_mc:MovieClip = canvas_mc.createEmptyMovieClip("line"+nextDepth+"_mc", nextDepth);
    line_mc.moveTo(this.origX, this.origY);
    line_mc.lineStyle(2, 0x000000, 100);
    line_mc.lineTo(this.newX, this.newY);
    var hypLen:Number = Math.sqrt(Math.pow(line_mc._width, 2)+Math.pow(line_mc._height, 2));
    line_mc.createTextField("length"+nextDepth+"_txt", canvas_mc.getNextHighestDepth(), this.origX, this.origY-22, 100, 22);
    line_mc['length'+nextDepth+'_txt'].text = Math.round(hypLen) +" pixels";
};
Mouse.addListener(mouseListener);

The MovieClip.getNextHighestDepth() method used in this example requires Flash Player 7 or later. If your SWF file includes a version 2 component, use the version 2 components' DepthManager class instead of the MovieClip.getNextHighestDepth() method.


Flash CS3


Comments


No screen name said on Jul 3, 2007 at 2:36 AM :
If x is negative (<0), and y is not an integer, then this function returns NaN.
tderich said on Jul 5, 2007 at 11:09 AM :
You are correct. NaN is what the ECMAScript 3rd edition spec calls for in that particular case. From Section 15.8.2.13:

* If x<0 and x is finite and y is finite and y is not an integer, the result is NaN.
No screen name said on Jul 5, 2007 at 1:24 PM :
Okay, I realized why this happens -- because the exponent function is multiplying the "x" number repeatedly. If x is negative, then the sign will flip from negative to positive (or back to negative) with each multiplication. If x lies between two consecutive integers, the function doesn't know how to handle the sign, and returns NaN.

Here's a function that should always return a positive value, and continues the smooth function curve that is drawn when x is positive:

public static function powAbs( x:Number, exponent:Number ) :Number {
if( x >= 0 ) {
return Math.pow( x, exponent );
} else {
return Math.pow( Math.abs(x), 1/exponent );
}
}

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00001835.html