Dart/Flutter - Calculate Number Pow Examples

This tutorial shows you how to calculate the power of a number in Dart, which also works in Flutter.

Power or often abbreviated as pow is an arithmetic operation involving two numbers, the base and the exponent or power. Creating a proper function for calculating the power of a number is not an easy thing. The common solution that uses a loop and multiplies the number multiple times according to the power value cannot handle many cases. For example, it doesn't handle the cases when the exponent is less than 1. In addition, the complexity is not optimal as well.

Dart already provides a method for that purpose. In this tutorial, I am going to explain the details of how it works, including the limitations that you may need to know.

Using Math pow

Dart's math library has a function named pow. You can use it by importing dart:math on the file where you want to use the function.

 num pow(num x, num exponent)

The usage is very simple. You need to pass the number where the power will be applied on as the first argument and the power as the second argument. For both arguments, you can pass an int or double value.

However, you may need to understand the return type of the function. If x is an integer and exponent is a non-negative integer, the return type is int. Otherwise, Dart will convert both arguments to doubles and the return type is double.

  var result = pow(2, 3);
  print('result: $result, type: ${result.runtimeType}');

  var result2 = pow(2.0, 3);
  print('result2: $result2, type: ${result2.runtimeType}');

Output:

  result: 8, type: int
  result2: 8.0, type: double

Below is the list of rules from the documentation.

  • if exponent is 0, the result is always 1.0.
  print(pow(3, 0)); /// 1
  • if x is 1.0, the result is always 1.0.
  print(pow(1, -100)); /// 1.0
  • If either x or exponent is NaN, the result is NaN.
  print(pow(2, double.nan)); /// NaN
  print(pow(double.nan, 2)); /// NaN
  • If x is negative (but not -0.0) and exponent is a finite non-integer, the result is NaN.
  print(pow(-1, 2.5)); /// NaN
  • if x is Infinity and exponent is positive, the result is Infinity.
  print(pow(double.infinity, 2)); /// Infinity
  • if x is Infinity and exponent is negative, the result is 0.0.
  print(pow(double.infinity, -2)); /// 0.0
  • if x is 0.0 and exponent is positive, the result is 0.0.
  print(pow(0, 2)); /// 0
  • if x is 0.0 and exponent is negative, the result is Infinity.
  print(pow(0, -2)); /// Infinity
  • if x is -Infinity or -0.0 and exponent is an odd integer, the result is -pow(-x ,y).
  print(pow(-double.infinity, 3)); /// -Infinity
  print(pow(-0.0, 3)); /// -0.0
  • if x is -Infinity or -0.0 and exponent is not an odd integer, the result is pow(-x , y).
  print(pow(-double.infinity, 4)); /// Infinity
  print(pow(-0.0, 4)); /// 0.0
  • if exponent is Infinity and the absolute value of x is less than 1, the result is 0.0.
  print(pow(0.5, double.infinity)); /// 0.0
  • if exponent is Infinity and x is -1, the result is 1.0.
  print(pow(-1, double.infinity)); /// 1.0
  • if exponent is Infinity and the absolute value of x is greater than 1, the result is Infinity.
  print(pow(2, double.infinity)); /// Infinity
  print(pow(-2, double.infinity)); /// Infinity
  • if exponent is -Infinity, the result is 1/pow(x, Infinity).
  print(pow(2, -double.infinity)); /// 0.0
  print(pow(0.5, -double.infinity)); /// Infinity

Another thing that you may need to know is regarding the overflow possibility. If the result is an integer, Dart only supports values between the minimum and maximum integers which are -9223372036854775808 and 9223372036854775807 respectively. Outside the range, the value will be truncated to 0.

If the return type is a double, the range is much wider since Dart's double type has minimum and maximum values of double.minPositive (5e-324) and double.maxFinite (1.7976931348623157e+308) respectively. Therefore, you may consider converting the integer value to a double if the result can be very big.

Summary

The best way to calculate the power of a number in Dart is by using Dart's pow method. It already handles various cases as explained above. The function can be used in any Dart framework including Flutter. Therefore, you don't have to implement your own function.