Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
squareroot
communitycreator

What is sqrt() in Dart?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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)}");
}

RELATED TAGS

dart
squareroot
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring