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() method
import 'dart:math';
void main() {
// create our number values
double no1 = 4.00;
int no2 = 16;
int no3 = 15;
int no4 = 1600;
// print the squre roots
print("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() method
import 'dart:math';
void main() {
// create our number values
double no1 = -0.0;
int no2 = -16;
int no3 = -15;
int no4 = -1600;
// print the squre roots
print("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)}");
}
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources