public void MultiplyBigDecimal()
        {
            BigDecimal multi1 = new BigDecimal(value, 5);
            BigDecimal multi2 = new BigDecimal(2.345D);
            BigDecimal result = BigMath.Multiply(multi1, multi2);

            Assert.True(result.ToString().StartsWith("289.51154260") && result.Scale == multi1.Scale + multi2.Scale,
                        "123.45908 * 2.345 is not correct: " + result);
            multi1 = BigDecimal.Parse("34656");
            multi2 = BigDecimal.Parse("-2");
            result = BigMath.Multiply(multi1, multi2);
            Assert.True(result.ToString().Equals("-69312") && result.Scale == 0, "34656 * 2 is not correct");
            multi1 = new BigDecimal(-2.345E-02);
            multi2 = new BigDecimal(-134E130);
            result = BigMath.Multiply(multi1, multi2);
            Assert.True(result.ToDouble() == 3.1422999999999997E130 && result.Scale == multi1.Scale + multi2.Scale,
                        "-2.345E-02 * -134E130 is not correct " + result.ToDouble());
            multi1 = BigDecimal.Parse("11235");
            multi2 = BigDecimal.Parse("0");
            result = BigMath.Multiply(multi1, multi2);
            Assert.True(result.ToDouble() == 0 && result.Scale == 0, "11235 * 0 is not correct");
            multi1 = BigDecimal.Parse("-0.00234");
            multi2 = new BigDecimal(13.4E10);
            result = BigMath.Multiply(multi1, multi2);
            Assert.True(result.ToDouble() == -313560000 && result.Scale == multi1.Scale + multi2.Scale,
                        "-0.00234 * 13.4E10 is not correct");
        }
        public static BigDecimal Pow(BigDecimal number, int n, MathContext mc)
        {
            // The ANSI standard X3.274-1996 algorithm
            int         m           = System.Math.Abs(n);
            int         mcPrecision = mc.Precision;
            int         elength     = (int)System.Math.Log10(m) + 1; // decimal digits in 'n'
            int         oneBitMask;                                  // mask of bits
            BigDecimal  accum;                                       // the single accumulator
            MathContext newPrecision = mc;                           // MathContext by default

            // In particular cases, it reduces the problem to call the other 'pow()'
            if ((n == 0) || ((number.IsZero) && (n > 0)))
            {
                return(Pow(number, n));
            }
            if ((m > 999999999) || ((mcPrecision == 0) && (n < 0)) ||
                ((mcPrecision > 0) && (elength > mcPrecision)))
            {
                // math.07=Invalid Operation
                throw new ArithmeticException(Messages.math07);                 //$NON-NLS-1$
            }
            if (mcPrecision > 0)
            {
                newPrecision = new MathContext(mcPrecision + elength + 1,
                                               mc.RoundingMode);
            }
            // The result is calculated as if 'n' were positive
            accum      = BigMath.Round(number, newPrecision);
            oneBitMask = Utils.HighestOneBit(m) >> 1;

            while (oneBitMask > 0)
            {
                accum = BigMath.Multiply(accum, accum, newPrecision);
                if ((m & oneBitMask) == oneBitMask)
                {
                    accum = BigMath.Multiply(accum, number, newPrecision);
                }
                oneBitMask >>= 1;
            }
            // If 'n' is negative, the value is divided into 'ONE'
            if (n < 0)
            {
                accum = Divide(BigDecimal.One, accum, newPrecision);
            }
            // The final value is rounded to the destination precision
            accum.InplaceRound(mc);
            return(accum);
        }
Exemplo n.º 3
0
 public static BigInteger operator *(BigInteger a, BigInteger b)
 {
     return(BigMath.Multiply(a, b));
 }