< prev index next >

src/java.base/share/classes/java/math/BigDecimal.java

Print this page




5396     }
5397 
5398     static BigDecimal scaledTenPow(int n, int sign, int scale) {
5399         if (n < LONG_TEN_POWERS_TABLE.length)
5400             return valueOf(sign*LONG_TEN_POWERS_TABLE[n],scale);
5401         else {
5402             BigInteger unscaledVal = bigTenToThe(n);
5403             if(sign==-1) {
5404                 unscaledVal = unscaledVal.negate();
5405             }
5406             return new BigDecimal(unscaledVal, INFLATED, scale, n+1);
5407         }
5408     }
5409 
5410     /**
5411      * Calculate the quotient and remainder of dividing a negative long by
5412      * another long.
5413      *
5414      * @param n the numerator; must be negative
5415      * @param d the denominator; must not be unity
5416      * @return a two-element {@long} array with the remainder and quotient in
5417      *         the initial and final elements, respectively
5418      */
5419     private static long[] divRemNegativeLong(long n, long d) {
5420         assert n < 0 : "Non-negative numerator " + n;
5421         assert d != 1 : "Unity denominator";
5422 
5423         // Approximate the quotient and remainder
5424         long q = (n >>> 1) / (d >>> 1);
5425         long r = n - q * d;
5426 
5427         // Correct the approximation
5428         while (r < 0) {
5429             r += d;
5430             q--;
5431         }
5432         while (r >= d) {
5433             r -= d;
5434             q++;
5435         }
5436 




5396     }
5397 
5398     static BigDecimal scaledTenPow(int n, int sign, int scale) {
5399         if (n < LONG_TEN_POWERS_TABLE.length)
5400             return valueOf(sign*LONG_TEN_POWERS_TABLE[n],scale);
5401         else {
5402             BigInteger unscaledVal = bigTenToThe(n);
5403             if(sign==-1) {
5404                 unscaledVal = unscaledVal.negate();
5405             }
5406             return new BigDecimal(unscaledVal, INFLATED, scale, n+1);
5407         }
5408     }
5409 
5410     /**
5411      * Calculate the quotient and remainder of dividing a negative long by
5412      * another long.
5413      *
5414      * @param n the numerator; must be negative
5415      * @param d the denominator; must not be unity
5416      * @return a two-element {@code long} array with the remainder and quotient in
5417      *         the initial and final elements, respectively
5418      */
5419     private static long[] divRemNegativeLong(long n, long d) {
5420         assert n < 0 : "Non-negative numerator " + n;
5421         assert d != 1 : "Unity denominator";
5422 
5423         // Approximate the quotient and remainder
5424         long q = (n >>> 1) / (d >>> 1);
5425         long r = n - q * d;
5426 
5427         // Correct the approximation
5428         while (r < 0) {
5429             r += d;
5430             q--;
5431         }
5432         while (r >= d) {
5433             r -= d;
5434             q++;
5435         }
5436 


< prev index next >