Exemplo n.º 1
0
        private static CBORNumber RationalToNumber(
            CBORObject o,
            int tagName)
        {
            if (o.Type != CBORType.Array)
            {
                return(null); // "Big fraction must be an array";
            }
            if (tagName == 270)
            {
                if (o.Count != 3)
                {
                    return(null); // "Extended big fraction requires exactly 3 items";
                }
                if (!IsUntaggedInteger(o[2]))
                {
                    return(null); // "Third item must be an integer";
                }
            }
            else
            {
                if (o.Count != 2)
                {
                    return(null); // "Big fraction requires exactly 2 items";
                }
            }
            if (!IsUntaggedIntegerOrBignum(o[0]))
            {
                return(null); // "Numerator is not an integer or bignum";
            }
            if (!IsUntaggedIntegerOrBignum(o[1]))
            {
                return(null); // "Denominator is not an integer or bignum");
            }
            EInteger numerator   = IntegerOrBignum(o[0]);
            EInteger denominator = IntegerOrBignum(o[1]);

            if (denominator.Sign <= 0)
            {
                return(null); // "Denominator may not be negative or zero");
            }
            ERational erat = ERational.Create(numerator, denominator);

            if (tagName == 270)
            {
                if (numerator.Sign < 0)
                {
                    return(null); // "Numerator may not be negative");
                }
                if (!o[2].CanValueFitInInt32())
                {
                    return(null); // "Invalid options";
                }
                int options = o[2].AsInt32Value();
                switch (options)
                {
                case 0:
                    break;

                case 1:
                    erat = erat.Negate();
                    break;

                case 2:
                    if (!numerator.IsZero || denominator.CompareTo(1) != 0)
                    {
                        return(null); // "invalid values");
                    }
                    erat = ERational.PositiveInfinity;
                    break;

                case 3:
                    if (!numerator.IsZero || denominator.CompareTo(1) != 0)
                    {
                        return(null); // "invalid values");
                    }
                    erat = ERational.NegativeInfinity;
                    break;

                case 4:
                case 5:
                case 6:
                case 7:
                    if (denominator.CompareTo(1) != 0)
                    {
                        return(null); // "invalid values");
                    }
                    erat = ERational.CreateNaN(
                        numerator,
                        options >= 6,
                        options == 5 || options == 7);
                    break;

                default: return(null); // "Invalid options");
                }
            }
            return(CBORNumber.FromObject(erat));
        }