< prev index next >

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

Print this page




1861         while (r >= dLong) {
1862             r -= dLong;
1863             q++;
1864         }
1865         // n - q*dlong == r && 0 <= r <dLong, hence we're done.
1866         return (r << 32) | (q & LONG_MASK);
1867     }
1868 
1869     /**
1870      * Calculate the integer square root {@code floor(sqrt(this))} where
1871      * {@code sqrt(.)} denotes the mathematical square root. The contents of
1872      * {@code this} are <b>not</b> 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., <i>Hacker's Delight (2nd ed.)</i> (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 <dLong, hence we're done.
1866         return (r << 32) | (q & LONG_MASK);
1867     }
1868 
1869     /**
1870      * Calculate the integer square root {@code floor(sqrt(this))} where
1871      * {@code sqrt(.)} denotes the mathematical square root. The contents of
1872      * {@code this} are <b>not</b> 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., <i>Hacker's Delight (2nd ed.)</i> (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.


< prev index next >