Availability: |
| ||||||||
Property/method value type: | Number primitive | ||||||||
JavaScript syntax: | - | Math.pow(aValue1, aValue2) | |||||||
Argument list: | aValue1 | Some meaningful numeric value | |||||||
aValue2 | Some meaningful numeric value |
This function returns the result of raising the first argument to the power of the second.
Special boundary conditions that affect the results are:
Argument1 | Argument2 | Result |
---|---|---|
+0 | < 0 | +infinity |
+infinity | < 0 | 0 |
+infinity | > 0 | +infinity |
-0 | < 0 and is an even integer | +infinity |
-0 | < 0 and is an odd integer | -infinity |
-infinity | < 0 | 0 |
-infinity | > 0 and is an even integer | +infinty |
-infinity | > 0 and is an odd integer | -infinty |
0 | > 0 | 0 |
1 | Any value | 1 |
< 0 and finite | finite but non integer | NaN |
abs(arg) < 1 | +infinity | 0 |
abs(arg) < 1 | -infinity | +infinity |
abs(arg) == 1 | +infinity | NaN |
abs(arg) == 1 | -infinity | NaN |
abs(arg) > 1 | +infinity | +infinity |
abs(arg) > 1 | -infinity | 0 |
Any value | 0 | 1 |
Any value | NaN | NaN |
NaN | non zero | NaN |
The exact value yielded by this function may vary slightly from implementation to implementation due to differences in the underlying precision of the implementations, math routines, and the specific algorithm selected to evaluate this function.
The example shows a simple binary number converter which exhibits some instability in the most significant bit on some platforms.
There are many boundary conditions that make this function hard to understand and therefore hard to diagnose if it goes wrong. Check both of the input arguments in case of doubt. They may have evaluated out to a strange boundary condition that yields unexpected results.
Using MSIE on the Macintosh exhibits a instability when you raise 2 to the power 31 and test the resulting value in a bitwise expression. Netscape works correctly in this circumstance. This is demonstrated in the example.
<HTML> <HEAD> </HEAD> <BODY> <SCRIPT> document.write(-2 + " - " + binary32(-2) + "<BR>"); document.write(-1 + " - " + binary32(-1) + "<BR>"); document.write(0 + " - " + binary32(0) + "<BR>"); document.write(1 + " - " + binary32(1) + "<BR>"); document.write(2 + " - " + binary32(2) + "<BR>"); document.write(3 + " - " + binary32(3) + "<BR>"); function binary32(aValue) { myArray = new Array(32); for(myEnum=0; myEnum<32; myEnum++) { if(aValue & Math.pow(2, myEnum)) { myArray[31-myEnum] = "1"; } else { myArray[31-myEnum] = "0"; } } return myArray.join(""); } </SCRIPT> </BODY> </HTML>
See also: | Math object, Math.sqrt(), Power function, Zero value |
Prev | Home | Next |
Math.PI | Up | Math.random() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |