コード例 #1
0
ファイル: AtomHelper.cs プロジェクト: rickbutton/Scheme.NET
 public static NumberAtom NumberFromComplex(BigInteger val)
 {
     return(new NumberAtom(Complex.CreateExactReal(val)));
 }
コード例 #2
0
ファイル: AtomHelper.cs プロジェクト: rickbutton/Scheme.NET
        public static bool TryNumberFromString(string str, out NumberAtom result)
        {
            var match = Regex.Match(str, NumberRegex, RegexOptions.Multiline);

            if (!match.Success)
            {
                result = null;
                return(false);
            }

            string[] prefixes = Split(match.Groups[1].Value, 2);

            if (prefixes.Any(p => !ValidPrefixes.Contains(p.Substring(1))))
            {
                result = null;
                return(false);
            }

            if (prefixes.Count(p => ValidExactnessPrefixes.Contains(p.Substring(1))) > 1)
            {
                result = null;
                return(false);
            }

            if (prefixes.Count(p => ValidRadixPrefixes.Contains(p.Substring(1))) > 1)
            {
                result = null;
                return(false);
            }

            var realSign = match.Groups[2].Value;
            var realTop  = match.Groups[3].Value;
            var realDiv  = match.Groups[4].Value;
            var realBot  = match.Groups[5].Value;

            var imagSign = match.Groups[6].Value;
            var imagTop  = match.Groups[7].Value;
            var imagDiv  = match.Groups[8].Value;
            var imagBot  = match.Groups[9].Value;

            if (string.IsNullOrEmpty(realSign))
            {
                realSign = "+";
            }
            if (string.IsNullOrEmpty(realDiv))
            {
                realDiv = "/";
                realBot = "1";
            }

            if (string.IsNullOrEmpty(imagTop))
            {
                imagTop = "0";
            }

            if (string.IsNullOrEmpty(imagSign))
            {
                imagSign = "+";
            }
            if (string.IsNullOrEmpty(imagDiv))
            {
                imagDiv = "/";
                imagBot = "1";
            }

            bool?exactness = null;

            if (prefixes.Contains("#e"))
            {
                exactness = true;
            }
            else if (prefixes.Contains("#i"))
            {
                exactness = false;
            }
            else if (realDiv == "." || imagDiv == ".")
            {
                exactness = false;
            }
            else
            {
                exactness = true;
            }

            int radix = 10;

            if (prefixes.Contains("#b"))
            {
                radix = 2;
            }
            else if (prefixes.Contains("#o"))
            {
                radix = 8;
            }
            else if (prefixes.Contains("#d"))
            {
                radix = 10;
            }
            else if (prefixes.Contains("#x"))
            {
                radix = 16;
            }

            ComplexPart rr, ri;
            var         success = ParseComplexPart(realSign, realTop, realDiv, realBot, radix, out rr);

            if (!success)
            {
                result = null; return(false);
            }

            success = ParseComplexPart(imagSign, imagTop, imagDiv, imagBot, radix, out ri);
            if (!success)
            {
                result = null; return(false);
            }

            if (exactness.Value)
            {
                result = NumberFromComplex(Complex.CreateExact(rr, ri));
            }
            else
            {
                result = NumberFromComplex(Complex.CreateInExact(rr, ri));
            }
            return(true);
        }
コード例 #3
0
ファイル: AtomHelper.cs プロジェクト: rickbutton/Scheme.NET
 public static NumberAtom NumberFromComplex(Complex val)
 {
     return(new NumberAtom(val));
 }