< prev index next >

## jdk/src/java.base/share/classes/java/math/MutableBigInteger.java

 ``` ``````1861 while (r >= dLong) { 1862 r -= dLong; 1863 q++; 1864 } 1865 // n - q*dlong == r && 0 <= r not changed. The value of {@code this} is assumed 1873 * to be non-negative. 1874 * 1875 * @implNote The implementation is based on the material in Henry S. Warren, 1876 * Jr., Hacker's Delight (2nd ed.) (Addison Wesley, 2013), 279-282. 1877 * 1878 * @throws ArithmeticException if the value returned by {@code bitLength()} 1879 * overflows the range of {@code int}. 1880 * @return the integer square root of {@code this} 1881 * @since 1.9 1882 */ 1883 MutableBigInteger sqrt() { 1884 // Special cases. 1885 if (this.isZero()) { 1886 return new MutableBigInteger(0); 1887 } else if (this.value.length == 1 1888 && (this.value[0] & LONG_MASK) < 4) { // result is unity 1889 return ONE; 1890 } 1891 1892 if (bitLength() <= 63) { 1893 // Initial estimate is the square root of the positive long value. 1894 long v = new BigInteger(this.value, 1).longValueExact(); 1895 long xk = (long)Math.floor(Math.sqrt(v)); 1896 1897 // Refine the estimate. 1898 do { 1899 long xk1 = (xk + v/xk)/2; 1900 1901 // Terminate when non-decreasing. ``` ``` ``````1861 while (r >= dLong) { 1862 r -= dLong; 1863 q++; 1864 } 1865 // n - q*dlong == r && 0 <= r not changed. The value of {@code this} is assumed 1873 * to be non-negative. 1874 * 1875 * @implNote The implementation is based on the material in Henry S. Warren, 1876 * Jr., Hacker's Delight (2nd ed.) (Addison Wesley, 2013), 279-282. 1877 * 1878 * @throws ArithmeticException if the value returned by {@code bitLength()} 1879 * overflows the range of {@code int}. 1880 * @return the integer square root of {@code this} 1881 * @since 9 1882 */ 1883 MutableBigInteger sqrt() { 1884 // Special cases. 1885 if (this.isZero()) { 1886 return new MutableBigInteger(0); 1887 } else if (this.value.length == 1 1888 && (this.value[0] & LONG_MASK) < 4) { // result is unity 1889 return ONE; 1890 } 1891 1892 if (bitLength() <= 63) { 1893 // Initial estimate is the square root of the positive long value. 1894 long v = new BigInteger(this.value, 1).longValueExact(); 1895 long xk = (long)Math.floor(Math.sqrt(v)); 1896 1897 // Refine the estimate. 1898 do { 1899 long xk1 = (xk + v/xk)/2; 1900 1901 // Terminate when non-decreasing. ```