Ejemplo n.º 1
0
        public static Complex op_Power(Complex x, Complex y)
        {
            if (x.IsZero())
            {
                if (y.Real < 0.0 || y.Imaginary() != 0.0)
                {
                    throw PythonOps.ZeroDivisionError("0.0 to a negative or complex power");
                }
                return(y.IsZero() ? Complex.One : Complex.Zero);
            }

#if FEATURE_NUMERICS
            // Special case for higher precision with real integer powers
            // TODO: A similar check may get added to CLR 4 upon resolution of Dev10 bug 863171,
            // in which case this code should go away.
            if (y.Imaginary == 0.0)
            {
                int power = (int)y.Real;
                if (power >= 0 && y.Real == power)
                {
                    Complex res = Complex.One;
                    if (power == 0)
                    {
                        return(res);
                    }
                    Complex factor = x;
                    while (power != 0)
                    {
                        if ((power & 1) != 0)
                        {
                            res = res * factor;
                        }
                        factor  = factor * factor;
                        power >>= 1;
                    }
                    return(res);
                }
            }
#endif

            return(x.Pow(y));
        }