What is pow() in Dart?

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.

Syntax

num pow (num x, num exponent)

Parameters

The pow() function requires two parameters:

  • A number, x
  • An exponent, exponent

Return Value

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:

  • When y is zero (0.0 or -0.0), the result is 1.0.
  • When x is 1.0, the result is 1.0.
  • If either x or y is NaN, then the result is NaN.
  • When x is negative (but not -0.0) and y is a finite non-integer, the result is NaN.
  • When x is infinity and y is negative, the result becomes 0.0.
  • When x is infinity and y is positive, the result is infinity.
  • When x is 0.0 and y is negative, the result is infinity.
  • When x is 0.0 and y is positive, the result is 0.0.
  • When x is -infinity or -0.0 and y is an odd integer, then the result is pow(-x,y).
  • When x is -infinity or -0.0 and y is not an odd integer, then the result is pow(-x, y).
  • When y is infinity and the absolute value of x is less than 1, the result is 0.0.
  • When y is infinity and x is -1, the result is 1.0.
  • When y is infinity and the absolute value of x is greater than 1, the result is infinity.
  • When y is -infinity, the result is 1/pow(x, infinity).

Example

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 runs
void main() {
// create integers
int aNumber = 2;
int aExponent = 3;
// create doubles
double bNumber = 20.00;
double bExponent = 2.00;
// negative integers
int cNumber = -4;
int cExponent = -2;
// negative doubles
double dNumber = -6.0;
double dExponent = -3.0;
// print values
print("$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)}");
}

Free Resources