Java sqr sqrt(long val)

Here you can find the source of sqrt(long val)

Description

sqrt

License

Apache License

Declaration

public static long sqrt(long val) 

Method Source Code

//package com.java2s;
/*//from  w  w  w . j av  a2  s .  c o m
 * Copyright 2016 Jerom van der Sar.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static long sqrt(long val) {
        // relative error tolerance
        double epsilon = 1e-15;
        // estimate of the square root
        double estimate = val;

        // repeatedly apply Newton update step until desired precision is achieved
        while (Math.abs(estimate - val / estimate) > epsilon * estimate) {
            estimate = (val / estimate + estimate) / 2l;
        }

        return (long) estimate;
    }
}

Related

  1. sqrt(float a)
  2. sqrt(float a)
  3. sqrt(float f)
  4. sqrt(int n)
  5. sqrt(int val)
  6. sqrt(long x)
  7. sqrt(Short a)
  8. sqrt(short value)
  9. sqrt2(float x)