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
yis zero (0.0 or -0.0), the result is 1.0. - When
xis 1.0, the result is 1.0. - If either
xoryisNaN, then the result isNaN. - When
xis negative (but not -0.0) andyis a finite non-integer, the result isNaN. - When x is infinity and y is negative, the result becomes 0.0.
- When
xis infinity andyis positive, the result is infinity. - When
xis 0.0 andyis negative, the result is infinity. - When
xis 0.0 andyis positive, the result is 0.0. - When
xis -infinity or -0.0 andyis an odd integer, then the result ispow(-x,y). - When
xis -infinity or -0.0 andyis not an odd integer, then the result ispow(-x, y). - When
yis infinity and the absolute value ofxis less than 1, the result is 0.0. - When
yis infinity andxis -1, the result is 1.0. - When
yis infinity and the absolute value ofxis greater than 1, the result is infinity. - When
yis -infinity, the result is1/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 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)}");}