Java Square Calculate squareRoot(Number num)

Here you can find the source of squareRoot(Number num)

Description

Compute an approximate square root of num.

License

LGPL

Declaration

public static double squareRoot(Number num) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**// w w w  .ja v a2  s .co m
     * Compute an approximate square root of num.
     * 
     * @pre num > 1.0
     * @post the square of the result is within .01 of num.
     */
    public static double squareRoot(Number num) {
        double n = num.doubleValue();
        double lower = 1.0;
        double upper = n;
        while (upper - lower > 0.0001) {
            double mid = upper / 2.0 + lower / 2.0;
            if (mid * mid < n)
                lower = mid;
            else
                upper = mid;
        } // while
        return (upper + lower) / 2.0;
    }
}

Related

  1. squaredLoss(double[] x, double[] y, double w_0, double w_1)
  2. squaredNorm(final double[] vec)
  3. squareEntries(double[][] m)
  4. squarenessRatio(final double w, final double h)
  5. squareOfSumFirstN(long n)
  6. squareSign(double value)
  7. squareSum(double... doubles)