I have two values one for base that is X and one for power N, how can a get X to the power of N ans.
any code will be appreciated.
if x is -Infinity or -0.0 and y is an odd integer, then the result is -pow(-x ,y) . if x is -Infinity or -0.0 and y is not an odd integer, then the result is the same as pow(-x , y) . if y is Infinity and the absolute value of x is less than 1, the result is 0.0.
In Dart, the sqrt() function returns the positive square root of a value. It can be used when the dart:math package is imported.
You are looking for this: https://api.dartlang.org/stable/2.5.0/dart-math/pow.html so:
pow(X,N)
If you want to implement it, you can have a look at here: https://coflutter.com/challenges/dart-how-to-implement-exponential-function-power/ This boils down to this loop:
int power(int x, int n) {
int retval = 1;
for (int i = 0; i < n; i++) {
retval *= x;
}
return retval;
}
This only works well for integer n-s.
For all of these examples with pow
you need the following import:
import 'dart:math';
final answer = pow(8, 2); // 64
Notes:
If you are only squaring, then it's probably easier to do this:
final answer = 8 * 8;
answer
is inferred to be of type num
, which could be an int
or double
at runtime. In this case the runtime type is int
, but in the following two examples it is double
.
final answer = pow(256, 1/4); // 4.0
final answer = pow(0.2, -3); // 124.99999999999999
That's basically the same as five cubed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With