What is sqrt() in Dart?
In Dart, the sqrt() function returns the positive square root of a value. It can be used when the dart:math package is imported.
Syntax
double sqrt(num x)
Parameters
sqrt() converts x to a double and returns the positive square root of the value.
Return Value
The sqrt() method returns the positive square root of the value. It returns -0.0 if x is -0.0, and NaN if x is otherwise negative or NaN.
Code
The following code demonstrates how to use the sqrt() function in Dart.
// import the math package that has the sqrt() methodimport 'dart:math';void main() {// create our number valuesdouble no1 = 4.00;int no2 = 16;int no3 = 15;int no4 = 1600;// print the squre rootsprint("square root of ${no1} is ${sqrt(no1)}");print("square root of ${no2} is ${sqrt(no2)}");print("square root of ${no3} is ${sqrt(no3)}");print("square root of ${no4} is ${sqrt(no4)}");}
Negative values
When we pass negative values, we get a NaN value.
// import the math package that has the sqrt() methodimport 'dart:math';void main() {// create our number valuesdouble no1 = -0.0;int no2 = -16;int no3 = -15;int no4 = -1600;// print the squre rootsprint("square root of ${no1} is ${sqrt(no1)}");print("square root of ${no2} is ${sqrt(no2)}");print("square root of ${no3} is ${sqrt(no3)}");print("square root of ${no4} is ${sqrt(no4)}");}