Determining Floating Point Square Root
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Computing the square root of a floating-point number is one of the most fundamental numerical operations. Modern CPUs have dedicated hardware instructions (like x86's fsqrt or ARM's vsqrt), but understanding the algorithms behind them — Newton's method (Babylonian method), the fast inverse square root trick, and bit-manipulation initial guesses — is essential for embedded systems, GPU shaders, and understanding numerical precision. The standard library sqrt() function uses hardware instructions when available and falls back to software implementations.
Newton's Method (Babylonian Method)
The most widely used algorithm. Given a number S, find x such that x^2 = S:
Each iteration roughly doubles the number of correct digits (quadratic convergence).
Better Initial Guess Using IEEE 754
A smarter initial guess reduces the number of iterations from ~20 to ~4:
Fast Inverse Square Root
The famous Quake III algorithm computes 1/sqrt(x) using bit-level tricks:
Python equivalent:
Binary Search Method
Simple and works for integer or fixed-point square roots:
Convergence is linear (one bit of precision per iteration), much slower than Newton's quadratic convergence.
Integer Square Root
For exact integer square roots (floor of sqrt):
Using Standard Library
Common Pitfalls
- Negative input:
sqrt(-1)raisesValueErrorin Python'smath.sqrt. Usecmath.sqrtfor complex results. NumPy'snp.sqrt(-1)returnsnanwith a warning. - Floating-point precision:
sqrt(2)**2does not equal exactly2.0due to IEEE 754 rounding. Usemath.isclose(result**2, S)for comparisons, not==. - Poor initial guess: Newton's method converges from any positive guess, but a bad guess (like
x0 = 1e15forsqrt(4)) requires many more iterations. Use the exponent-halving trick for a good starting point. - Integer overflow in bit tricks: The fast inverse square root trick assumes 32-bit float and 32-bit int. Using it with 64-bit doubles requires a different magic constant (
0x5fe6eb50c7b537a9). - Division by zero in Newton's iteration: If the initial guess is 0,
S / xcauses division by zero. Always check forS == 0before entering the iteration loop.
Summary
- Newton's method (
x = (x + S/x) / 2) converges quadratically — the standard algorithm for sqrt - Use IEEE 754 bit manipulation for a good initial guess, reducing iterations to 3-4 for full precision
- The fast inverse square root (
0x5f3759dftrick) is historical — modern CPUs have dedicated sqrt instructions math.sqrt()uses hardware instructions and is always the best choice for production code- For integer square roots, use
math.isqrt()(Python 3.8+) for exact floor results
Related reading
- Determining if an unordered vectorT has all unique elements
- Determining if two rays intersect
- Determining the big-O runtimes of these different loops?
- Determining the complexities given codes
- Determining if a sphere intersects an object or not
- deterministic and reversible permutation function to store a message
- Determining whether or not a directed or undirected graph is a tree
- Determining which integer is closest to the kth root of n without using floating point arithmetic?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.