FileDocCategorySizeDatePackage
Multiplication.javaAPI DocAndroid 1.5 API7124Wed May 06 22:41:04 BST 2009java.math

Multiplication

public class Multiplication extends Object
Static library that provides all multiplication of {@link BigInteger} methods.
author
Intel Middleware Product Division
author
Instituto Tecnologico de Cordoba

Fields Summary
static final int[]
tenPows
An array with powers of ten that fit in the type {@code int}. ({@code 10^0,10^1,...,10^9})
static final int[]
fivePows
An array with powers of five that fit in the type {@code int}. ({@code 5^0,5^1,...,5^13})
static final BigInteger[]
bigTenPows
An array with the first powers of ten in {@code BigInteger} version. ({@code 10^0,10^1,...,10^31})
static final BigInteger[]
bigFivePows
An array with the first powers of five in {@code BigInteger} version. ({@code 5^0,5^1,...,5^31})
Constructors Summary
private Multiplication()
Just to denote that this class can't be instantiated.

Methods Summary
static java.math.BigIntegermultiplyByFivePow(java.math.BigInteger val, int exp)
Multiplies a number by a power of five. This method is used in {@code BigDecimal} class.

param
val the number to be multiplied
param
exp a positive {@code int} exponent
return
{@code val * 5exp}

        // PRE: exp >= 0
        if (exp < fivePows.length) {
            return multiplyByPositiveInt(val, fivePows[exp]);
        } else if (exp < bigFivePows.length) {
            return val.multiply(bigFivePows[exp]);
        } else {// Large powers of five
            return val.multiply(bigFivePows[1].pow(exp));
        }
    
static java.math.BigIntegermultiplyByPositiveInt(java.math.BigInteger val, int factor)
Multiplies a number by a positive integer.

param
val an arbitrary {@code BigInteger}
param
factor a positive {@code int} number
return
{@code val * factor}

    
    

     
        int i;
        long fivePow = 1L;
        
        for (i = 0; i <= 18; i++) {
            bigFivePows[i] = BigInteger.valueOf(fivePow);
            bigTenPows[i] = BigInteger.valueOf(fivePow << i);
            fivePow *= 5;
        }
        for (; i < bigTenPows.length; i++) {
            bigFivePows[i] = bigFivePows[i - 1].multiply(bigFivePows[1]);
            bigTenPows[i] = bigTenPows[i - 1].multiply(BigInteger.TEN);
        }
    
        // BEGIN android-changed
        BigInt bi = val.bigInt.copy();
        bi.multiplyByPositiveInt(factor);
        return new BigInteger(bi);
        // END android-changed
    
static java.math.BigIntegermultiplyByTenPow(java.math.BigInteger val, long exp)
Multiplies a number by a power of ten. This method is used in {@code BigDecimal} class.

param
val the number to be multiplied
param
exp a positive {@code long} exponent
return
{@code val * 10exp}

        // PRE: exp >= 0
        return ((exp < tenPows.length)
        ? multiplyByPositiveInt(val, tenPows[(int)exp])
        : val.multiply(powerOf10(exp)));
    
static java.math.BigIntegerpowerOf10(long exp)
It calculates a power of ten, which exponent could be out of 32-bit range. Note that internally this method will be used in the worst case with an exponent equals to: {@code Integer.MAX_VALUE - Integer.MIN_VALUE}.

param
exp the exponent of power of ten, it must be positive.
return
a {@code BigInteger} with value {@code 10exp}.

        // PRE: exp >= 0
        int intExp = (int)exp;
        // "SMALL POWERS"
        if (exp < bigTenPows.length) {
            // The largest power that fit in 'long' type
            return bigTenPows[intExp];
        } else if (exp <= 50) {
            // To calculate:    10^exp
            return BigInteger.TEN.pow(intExp);
        } else if (exp <= 1000) {
            // To calculate:    5^exp * 2^exp
            return bigFivePows[1].pow(intExp).shiftLeft(intExp);
        }
        // "LARGE POWERS"
        /*
         * To check if there is free memory to allocate a BigInteger of the
         * estimated size, measured in bytes: 1 + [exp / log10(2)]
         */
        long byteArraySize = 1 + (long)(exp / 2.4082399653118496);
        
        if (byteArraySize > Runtime.getRuntime().freeMemory()) {
            // math.01=power of ten too big
            throw new OutOfMemoryError(Messages.getString("math.01")); //$NON-NLS-1$
        }
        if (exp <= Integer.MAX_VALUE) {
            // To calculate:    5^exp * 2^exp
            return bigFivePows[1].pow(intExp).shiftLeft(intExp);
        }
        /*
         * "HUGE POWERS"
         * 
         * This branch probably won't be executed since the power of ten is too
         * big.
         */
        // To calculate:    5^exp
        BigInteger powerOfFive = bigFivePows[1].pow(Integer.MAX_VALUE);
        BigInteger res = powerOfFive;
        long longExp = exp - Integer.MAX_VALUE;
        
        intExp = (int)(exp % Integer.MAX_VALUE);
        while (longExp > Integer.MAX_VALUE) {
            res = res.multiply(powerOfFive);
            longExp -= Integer.MAX_VALUE;
        }
        res = res.multiply(bigFivePows[1].pow(intExp));
        // To calculate:    5^exp << exp
        res = res.shiftLeft(Integer.MAX_VALUE);
        longExp = exp - Integer.MAX_VALUE;
        while (longExp > Integer.MAX_VALUE) {
            res = res.shiftLeft(Integer.MAX_VALUE);
            longExp -= Integer.MAX_VALUE;
        }
        res = res.shiftLeft(intExp);
        return res;