Exemple #1
0
 public static Complex64 ParseImaginary(string text)
 {
     return(Complex64.MakeImaginary(double.Parse(
                                        text.Substring(0, text.Length - 1),
                                        System.Globalization.CultureInfo.InvariantCulture.NumberFormat
                                        )));
 }
Exemple #2
0
        public static Complex64 ParseComplex64(string text)
        {
            // remove no-meaning spaces
            text = text.Trim();
            if (text == string.Empty)
            {
                throw Ops.ValueError("complex() arg is an empty string");
            }
            if (text.IndexOf(' ') != -1)
            {
                throw Ops.ValueError("complex() arg is a malformed string");
            }

            try {
                int len = text.Length;

                char lastChar = text[len - 1];
                if (lastChar != 'j' && lastChar != 'J')
                {
                    return(Complex64.MakeReal(ParseFloatNoCatch(text)));
                }

                // search for sign char for the imaginary part
                int signPos = text.LastIndexOfAny(new char[] { '+', '-' });

                // it is possible the sign belongs to 'e'
                if (signPos > 1)
                {
                    char prevChar = text[signPos - 1];
                    if (prevChar == 'e' || prevChar == 'E')
                    {
                        signPos = text.Substring(0, signPos - 1).LastIndexOfAny(new char[] { '+', '-' });
                    }
                }

                if (signPos == -1)
                {
                    // special: "j"
                    return(Complex64.MakeImaginary(len == 1 ? 1 : ParseFloatNoCatch(text.Substring(0, len - 1))));
                }
                else
                {
                    // special: "+j", "-j"
                    return(new Complex64(
                               signPos == 0 ? 0 : ParseFloatNoCatch(text.Substring(0, signPos)),
                               (len == signPos + 2) ? (text[signPos] == '+' ? 1 : -1) : ParseFloatNoCatch(text.Substring(signPos, len - signPos - 1))));
                }
            } catch (OverflowException) {
                throw Ops.ValueError("complex() literal too large to convert");
            } catch {
                throw Ops.ValueError("complex() arg is a malformed string");
            }
        }
        public static object Make(
            PythonType cls,
            [DefaultParameterValueAttribute(null)] object real,
            [DefaultParameterValueAttribute(null)] object imag
            )
        {
            Conversion conv;
            Complex64  real2, imag2;

            real2 = imag2 = new Complex64();

            if (imag != null)
            {
                if (real is string)
                {
                    throw Ops.TypeError("complex() can't take second arg if first is a string");
                }
                if (imag is string)
                {
                    throw Ops.TypeError("complex() second arg can't be a string");
                }
                imag2 = Converter.TryConvertToComplex64(imag, out conv);
                if (conv == Conversion.None)
                {
                    throw Ops.TypeError("complex() argument must be a string or a number");
                }
            }

            if (real != null)
            {
                if (real is string)
                {
                    real2 = LiteralParser.ParseComplex64((string)real);
                }
                else if (real is Complex64)
                {
                    if (imag == null && cls == ComplexType)
                    {
                        return(real);
                    }
                    else
                    {
                        real2 = (Complex64)real;
                    }
                }
                else
                {
                    real2 = Converter.TryConvertToComplex64(real, out conv);
                    if (conv == Conversion.None)
                    {
                        throw Ops.TypeError("complex() argument must be a string or a number");
                    }
                }
            }

            Complex64 c = real2 + imag2 * Complex64.MakeImaginary(1);

            if (cls == ComplexType)
            {
                return(new Complex64(c.real, c.imag));
            }
            else
            {
                return(cls.ctor.Call(cls, c.real, c.imag));
            }
        }