Square Root in Programming – JavaScript, Python, C++ & Free Calculator
As a developer, you'll often need square roots in algorithms,
game development, data science, and more. This guide covers
how to use square root functions across popular languages —
plus a free calculator tool for quick testing.
Why Developers Need Square Root?
- Distance formula in maps/games → √((x2-x1)² + (y2-y1)²)
- Machine Learning → RMS error calculations
- Graphics/Physics engines → vector magnitude
- Cryptography → prime number algorithms
- Data Science → standard deviation formula
Square Root in JavaScript
// Method 1 - Math.sqrt()
Math.sqrt(25); // 5
Math.sqrt(2); // 1.4142135623730951
Math.sqrt(-1); // NaN (use complex lib for negatives)
// Method 2 - Exponentiation operator
25 ** 0.5 // 5
(144) ** 0.5 // 12
// Method 3 - Custom function (without Math)
function sqrtNewton(n) {
let x = n;
let root;
while (true) {
root = 0.5 * (x + n / x);
if (Math.abs(root - x) < 1e-10) break;
x = root;
}
return root;
}
console.log(sqrtNewton(25)); // 5
Square Root in Python
import math
# Method 1 - math.sqrt()
math.sqrt(25) # 5.0
math.sqrt(2) # 1.4142135623730951
# Method 2 - Power operator
25 ** 0.5 # 5.0
# Method 3 - numpy (for arrays)
import numpy as np
np.sqrt([4, 9, 16, 25]) # array([2., 3., 4., 5.])
# Method 4 - cmath (for negative numbers)
import cmath
cmath.sqrt(-1) # 1j (complex number)
Square Root in C++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// Method 1 - sqrt()
cout << sqrt(25); // 5
cout << sqrt(2); // 1.41421
// Method 2 - pow()
cout << pow(25, 0.5); // 5
// Method 3 - Newton-Raphson (manual)
double n = 25, x = n;
for (int i = 0; i < 1000; i++)
x = 0.5 * (x + n / x);
cout << x; // 5.0000
return 0;
}
Square Root in Java
public class SqrtExample {
public static void main(String[] args) {
// Method 1 - Math.sqrt()
System.out.println(Math.sqrt(25)); // 5.0
System.out.println(Math.sqrt(2)); // 1.4142135623730951
// Method 2 - Math.pow()
System.out.println(Math.pow(25, 0.5)); // 5.0
}
}
Real Algorithm Example – Distance Between Two Points
// Used in games, maps, GPS apps
function distance(x1, y1, x2, y2) {
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
}
console.log(distance(0, 0, 3, 4)); // 5 (classic 3-4-5 triangle)
console.log(distance(1, 1, 4, 5)); // 5
Real Algorithm Example – Standard Deviation (Data Science)
import math
def std_deviation(data):
mean = sum(data) / len(data)
variance = sum((x - mean) ** 2 for x in data) / len(data)
return math.sqrt(variance) # Square root of variance!
data = [10, 20, 30, 40, 50]
print(std_deviation(data)) # 14.14
Performance Comparison
| Method | Speed | Accuracy | Best For |
|---|---|---|---|
| Math.sqrt() | Fastest | High | General use |
| ** 0.5 | Fast | High | Quick scripts |
| Newton-Raphson | Slower | High | Learning algo |
| numpy.sqrt() | Fastest | High | Arrays/Data |
Common Errors Developers Face
// Wrong - square root of negative number
Math.sqrt(-4) // NaN — use complex number library
// Wrong - string input
Math.sqrt("hello") // NaN — always validate input
// Correct - with input validation
function safeSqrt(n) {
if (typeof n !== "number" || n < 0) {
return "Invalid input";
}
return Math.sqrt(n);
}
Quick Test Without Coding
Don't want to run code right now? Use our
Free Smart Calculator Tool to instantly
calculate square roots and test your logic:
Supports decimals, large numbers, and instant results!
Conclusion
Square root is used everywhere in programming — from simple
math to complex algorithms. Now you know how to implement it
in JavaScript, Python, C++, and Java, along with real-world
use cases like distance calculation and standard deviation.
Bookmark our Free Calculator Tool
for quick calculations while coding!
Top comments (0)