What is the difference between x.pow(n) and pow(x, n)?

dart

Solution

Update: int/num/double.pow() currently exist only in the VM, and will be removed. Use math.pow() instead.

The signature for int.pow() is:

int pow(int exponent)

So the following example fails in checked mode as 1/2 does not evaluate to an integer.

int i = 5;
i.pow(1/2);

So calling int.pow() expects an integer exponent (Only checked in checked mode), and returns an integer.

And, calling math.pow(num x, num exponent), does not require an integer exponent, and may return a double, if x is an integer and exponent is a double.

Here is a link to the math.pow() documentation. Here are links to the int.pow() and math.pow() source code so you can see what happens under the hood.

Problem

I'm wondering because when I run my code in checked mode there seems to be some discrepancies. For example: ``` List<List> getFactors(int n) { List<List> factors = [[1, n]]; double top = pow(n,1/2); int test = 2; while (test <= top) { if (n % test == 0) factors.add([test, n ~/ test]); test++; } return factors; } ``` works as is, but when I change the `pow(n,1/2)` to `n.pow(1/2)` it returns an error in checked mode. The only fix is to change the type of `n` to a double. Why is this? Also the general differences between the two would be nice to know. Thanks!

Original source