Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Math processing
Expertise: Beginner
Mar 20, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

How to calculate sqrt()

How do you calculate sqrt()?

Of course the best way to compute square roots is to use Math.sqrt(), but I assume your question is academic. I'm not sure of the best way to find square roots, but historically, Newton's Method is the most significant.

Newton's Method tells us how to approximate an x-intercept of a differentiable curve described by the equation y = f(x). How does this help us to find the square root of 42? Well, the x-intercepts of the parabola y = x^2 - 42 occur at sqrt(42) and -sqrt(42).

The x-intercept of a curve y = f(x) occurs at a point x = a such that f(a) = 0. So, Newton's Method iterates a sequence of progressively better guesses until f(guess) is acceptably close to 0:

guess = 1;
   while (delta < |f(guess)|)  // delta small
      guess = improve(guess);
   return guess;
How do we improve a bad guess? Newton's idea is this:
Draw a line tangent to the curve at the point (guess, f(guess)).
Using calculus, it's pretty easy to get the equation for this line:
y - f(guess) = slope * (x - guess)
where slope = f'(guess) = the derivative of f at x = guess.

Newton reasoned that this line crudely approximates the curve y = f(x) near the point of tangency, and therefore the x-intercept of this line crudely approximates the x-intercept of y = f(x).

It's easy to calculate the x-intercept of this line. Just set y = 0 and solve for x:

x = guess - f(guess)/f'(guess)
improve(guess) = guess - f(guess)/f'(guess)
>From an implementation standpoint, the only complication is computing f'(x). Again we dust off our calculus book, where we discover
f'(x) = limit (f(x + delta) - f(x))/delta as delta tends to 0
Dropping the limit and choosing delta to be suitably small should give us an acceptable approximation of f'(x).

Here's a Java implementation with a crude GUI. It can be used to find the n-th root of any real number a where 0 <= a and 0 <= n.

import java.awt.*;

// A system for approximating n-th root of a
class Solver {

   private int n;
   private double a;

   private static double delta = 0.00000001; // controls accuracy
   private static double initGuess = 1;

   // constructs a solver for x^n - a
   public Solver(int x, double y) { n = x; a = y; }

   // your basic iterator for the improve method
   public double solve() {
      double guess = initGuess;
         guess = improve(guess);
      return guess;

   // f(x) = x^n - a = the function we're solving
   private double f(double x) {
      return Math.pow(x, n) - a;

   // df(x) = f'(x), approximately
   private double df(double x) {
      return (f(x + delta) - f(x))/delta;

   // 0 <= |f(guess)| <= delta?
   private boolean goodEnough(double guess) {
      return (Math.abs(f(guess)) <= delta);

   // Newton's Method
   private double improve(double guess) {
      return guess - f(guess)/df(guess);

// a primitive GUI
public class Main extends Frame {

   private TextField root;
   private TextField base;
   private TextField result;

   public Main() {

      setTitle("Root Tester");

      setLayout(new FlowLayout());

      root = new TextField("root", 15);
      base = new TextField("base", 15);
      result = new TextField("result", 15);


   public boolean action(Event e, Object o) {

      if (e.target == base) {
         double d = Double.valueOf(base.getText()).doubleValue();
         int i = Integer.valueOf(root.getText()).intValue();
         if (0 <= i && 0 <= d) {
            Solver s = new Solver(i, d); // make a solver for x^i - d
            result.setText("+/- " + String.valueOf(s.solve()));
         else result.setText("0"); // for now

      return true;

   public static void main(String args[]) {
      Frame m = new Main();
      m.resize(300, 200);

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date