The pow()
function in Dart is a dart:math
package function that returns a value to the power of a specified exponent. In order to use the pow()
function, you must first import the dart:math
package.
num pow (num x, num exponent)
The pow()
function requires two parameters:
x
exponent
pow()
returns x
to the power of exponent
. If x
is an int
and exponent
is a non-negative int
, the result is an int
; otherwise, both arguments are converted to doubles first, and the result is a double.
When you use double
as both x
and y
, pow(x,y)
, here is what happens:
y
is zero (0.0 or -0.0), the result is 1.0.x
is 1.0, the result is 1.0.x
or y
is NaN
, then the result is NaN
.x
is negative (but not -0.0) and y
is a finite non-integer, the result is NaN
.x
is infinity and y
is positive, the result is infinity.x
is 0.0 and y
is negative, the result is infinity.x
is 0.0 and y
is positive, the result is 0.0.x
is -infinity or -0.0 and y
is an odd integer, then the result is pow(-x,y)
.x
is -infinity or -0.0 and y
is not an odd integer, then the result is pow(-x, y)
.y
is infinity and the absolute value of x
is less than 1, the result is 0.0.y
is infinity and x
is -1, the result is 1.0.y
is infinity and the absolute value of x
is greater than 1, the result is infinity.y
is -infinity, the result is 1/pow(x, infinity)
.In the example below, we use the number and exponent as integers.
// import the math package so as to use `pow()'import "dart:math";// main function that runsvoid main() {// create integersint aNumber = 2;int aExponent = 3;// create doublesdouble bNumber = 20.00;double bExponent = 2.00;// negative integersint cNumber = -4;int cExponent = -2;// negative doublesdouble dNumber = -6.0;double dExponent = -3.0;// print valuesprint("$aNumber to the power of $aExponent is ${pow(aNumber, aExponent)}");print("$bNumber to the power of $bExponent is ${pow(bNumber, bExponent)}");print("$cNumber to the power of $cExponent is ${pow(cNumber, cExponent)}");print("$dNumber to the power of $dExponent is ${pow(dNumber, dExponent)}");}