Esempio n. 1
0
        public Number(BigInteger numerator, BigInteger denominator)
        {
            if (denominator == BigInteger.Zero)
            {
                numerator   = 0;
                denominator = 0;
                State       = NumberState.NaN;
            }
            BigInteger num = NumberAlgorithms.Gcd(numerator, denominator);

            if (num == BigInteger.Zero)
            {
                State = NumberState.PositiveInfinity;
            }
            else if (denominator < BigInteger.Zero)
            {
                Numerator   = -numerator / num;
                Denominator = -denominator / num;
            }
            else
            {
                Numerator   = numerator / num;
                Denominator = denominator / num;
            }
        }
Esempio n. 2
0
 public static Number Create(byte[] bytes, int scale, NumberState state)
 {
     if (state == NumberState.None)
     {
         // This inlines common numbers to save a bit of memory.
         if (scale == 0 && bytes.Length == 1)
         {
             if (bytes[0] == 0)
             {
                 return(Zero);
             }
             if (bytes[0] == 1)
             {
                 return(One);
             }
         }
         return(new Number(bytes, scale, state));
     }
     if (state == NumberState.NegativeInfinity)
     {
         return(NegativeInfinity);
     }
     if (state == NumberState.PositiveInfinity)
     {
         return(PositiveInfinity);
     }
     if (state == NumberState.NotANumber)
     {
         return(NaN);
     }
     throw new ApplicationException("Unknown number state.");
 }
Esempio n. 3
0
 internal Number(NumberState numberState, BigDecimal bigDecimal)
 {
     this.numberState = numberState;
     if (numberState == NumberState.None)
     {
         SetBigDecimal(bigDecimal);
     }
 }
Esempio n. 4
0
 private Number(byte[] buf, int scale, NumberState state)
 {
     numberState = state;
     if (numberState == NumberState.None)
     {
         BigInteger bigint = new BigInteger(buf);
         SetBigDecimal(new BigDecimal(bigint, scale));
     }
 }
Esempio n. 5
0
        public Lexer()
        {
            EmptyState    = new EmptyState(this);
            NumberState   = new NumberState(this);
            OperatorState = new OperatorState(this);


            CurrentState = EmptyState;
        }
Esempio n. 6
0
 /// <summary>
 /// Constructor.
 /// Set event handlers and creates the label for viewing the Number.
 /// </summary>
 /// <param name="val">Value for the number</param>
 public Number(int val)
 {
     OnRemove += BlankRemove;
     StopEventColumn += EmptyStopHandler;
     StopEventRow += EmptyStopHandler;
     value = val;
     Display = new Label();
     Display.AutoSize = true;
     Display.Text = this.ToString();
     currentState = NumberState.stopped;
 }
Esempio n. 7
0
        public PrimalityTestChoose(NumberState numberState, BigInteger number)
        {
            InitializeComponent();

            _number  = number;
            _token   = new CancellationTokenSource();
            _options = new Options();
            Init(numberState);

            DataContext = _options;

            TestButton.Click += TestButton_Click;
        }
Esempio n. 8
0
        public IToken Next(IScanner scanner)
        {
            var state   = new NumberState(scanner);
            var current = scanner.Current;

            if (current == CharacterTable.FullStop)
            {
                return(state.Dot());
            }
            else if (current == CharacterTable.Zero)
            {
                return(state.Zero());
            }

            return(state.Digit());
        }
Esempio n. 9
0
        private void setNewState(char character)
        {
            switch (character)
            {
            case '.':
                state = NumberState.DECIMAL_PART;
                break;

            case 'e':
            case 'E':
                state = NumberState.E_PART;
                break;

            default:
                break;
            }
        }
Esempio n. 10
0
        private void Init(NumberState numberState)
        {
            switch (numberState)
            {
            case NumberState.Fermat:
            {
                PepinRadio.IsEnabled       = true;
                LucasLehmerRadio.IsEnabled = false;
                PepinText.Text            += "(recommend)";
                _options.Type = PrimalityTestType.Pepin;
            }
            break;

            case NumberState.Mersenne:
            {
                PepinRadio.IsEnabled       = false;
                LucasLehmerRadio.IsEnabled = true;
                LucasLehmerText.Text      += "(recommend)";
                _options.Type = PrimalityTestType.LucasLehmer;
            }
            break;

            case NumberState.Proth:
            {
                PepinRadio.IsEnabled       = false;
                LucasLehmerRadio.IsEnabled = false;

                MessageBox.Show("This number is a Proth number, " +
                                "so best best primality test for you is Proth test.",
                                "Result", MessageBoxButton.OK);
            }
            break;

            case NumberState.None:
                PepinRadio.IsEnabled       = false;
                LucasLehmerRadio.IsEnabled = false;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(numberState), numberState, null);
            }
        }
Esempio n. 11
0
        public NumberParser(NumberState state)
        {
            _state = state;
            switch (state)
            {
            case NumberState.IsZero:
                NextChar(StructuralChar.DecimalSeparator, () => new NumberParser(NumberState.ReadyForFraction));
                NextChar(StructuralChar.ScientificNotationSeparator, () => new NumberParser(NumberState.ReadyForExponentStart));
                break;

            case NumberState.ReadyForFirstDigit:
                NextChar(StructuralChar.LeadingIntegerDigit, () => new NumberParser(NumberState.SecondaryDigit));
                NextChar(StructuralChar.OnlyZero, () => new NumberParser(NumberState.IsZero));
                break;

            case NumberState.SecondaryDigit:
                NextChar(StructuralChar.FollowingIntegerDigit, () => new NumberParser(NumberState.SecondaryDigit));
                NextChar(StructuralChar.DecimalSeparator, () => new NumberParser(NumberState.ReadyForFraction));
                NextChar(StructuralChar.ScientificNotationSeparator, () => new NumberParser(NumberState.ReadyForExponentStart));
                break;

            case NumberState.ReadyForFraction:
                NextChar(StructuralChar.FollowingIntegerDigit, () => new NumberParser(NumberState.ReadyToContinueFraction));
                break;

            case NumberState.ReadyToContinueFraction:
                NextChar(StructuralChar.FollowingIntegerDigit, () => new NumberParser(NumberState.ReadyToContinueFraction));
                NextChar(StructuralChar.ScientificNotationSeparator, () => new NumberParser(NumberState.ReadyForExponentStart));
                break;

            case NumberState.ReadyForExponentStart:
                NextChar(StructuralChar.FollowingIntegerDigit, () => new NumberParser(NumberState.ReadyToContinueExponent));
                break;

            case NumberState.ReadyToContinueExponent:
                NextChar(StructuralChar.FollowingIntegerDigit, () => new NumberParser(NumberState.ReadyToContinueExponent));
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(state), state, null);
            }
        }
Esempio n. 12
0
        public NumberValidityChecker(string input)
        {
            this.index           = 0;
            this.input           = input;
            this.state           = NumberState.INTEGER_PART;
            this.valid           = false;
            this.automataState   = 0;
            this.errorMessage    = string.Empty;
            this.decimalPart     = 0;
            this.integerPart     = 0;
            this.ePart           = 0;
            this.value           = -1;
            this.negativeInteger = false;
            this.negativeE       = false;

            valid = checkString();
            if (valid)
            {
                value = setValue();
            }
        }
Esempio n. 13
0
        protected override object CastObjectTo(object value, DataType destType)
        {
            var n = (Number)value;

            var sqlType = destType.SqlType;

            switch (sqlType)
            {
            case (SqlType.Bit):
            case (SqlType.Boolean):
                return(n.ToBoolean());

            case (SqlType.TinyInt):
            case (SqlType.SmallInt):
            case (SqlType.Integer):
                return((Number)n.ToInt32());

            case (SqlType.BigInt):
                return((Number)n.ToInt64());

            case (SqlType.Float):
            case (SqlType.Real):
            case (SqlType.Double):
                double      d     = n.ToDouble();
                NumberState state = NumberState.None;
                if (Double.IsNaN(d))
                {
                    state = NumberState.NotANumber;
                }
                else if (Double.IsPositiveInfinity(d))
                {
                    state = NumberState.PositiveInfinity;
                }
                else if (Double.IsNegativeInfinity(d))
                {
                    state = NumberState.NegativeInfinity;
                }
                return(new Number(state, new BigDecimal(d)));

            case (SqlType.Numeric):
            // fall through
            case (SqlType.Decimal):
                return(Number.Parse(n.ToString()));

            case (SqlType.Char):
                return(new StringObject(CastUtil.PaddedString(n.ToString(), ((StringType)destType).MaxSize)));

            case (SqlType.VarChar):
            case (SqlType.LongVarChar):
                return(new StringObject(n.ToString()));

            case (SqlType.Date):
            case (SqlType.Time):
            case (SqlType.TimeStamp):
                return(ToDate(n.ToInt64()));

            case (SqlType.Blob):
            // fall through
            case (SqlType.Binary):
            // fall through
            case (SqlType.VarBinary):
            // fall through
            case (SqlType.LongVarBinary):
                return(new BinaryObject(n.ToByteArray()));

            case (SqlType.Null):
                return(null);

            default:
                throw new InvalidCastException();
            }
        }
Esempio n. 14
0
 /**
  * Creates a BigNumber from the given data.
  */
 public static BigNumber Create(byte[] buf, int scale, NumberState state)
 {
     if (state == NumberState.None) {
         // This inlines common numbers to save a bit of memory.
         if (scale == 0 && buf.Length == 1) {
             if (buf[0] == 0) {
                 return Zero;
             } else if (buf[0] == 1) {
                 return One;
             }
         }
         return new BigNumber(buf, scale, state);
     } else if (state == NumberState.NegativeInfinity) {
         return NegativeInfinity;
     } else if (state == NumberState.PositiveInfinity) {
         return PositiveInfinity;
     } else if (state == NumberState.NotANumber) {
         return NaN;
     } else {
         throw new ApplicationException("Unknown number state.");
     }
 }
Esempio n. 15
0
 internal Number(NumberState state)
 {
     Numerator   = 0;
     Denominator = 0;
     State       = state;
 }
Esempio n. 16
0
 /// <summary>
 ///  Constructs the number.
 /// </summary>
 /// <param name="numberState"></param>
 /// <param name="bigDecimal"></param>
 internal BigNumber(NumberState numberState, BigDecimal bigDecimal)
 {
     this.numberState = numberState;
     if (numberState == NumberState.None)
         SetBigDecimal(bigDecimal);
 }
Esempio n. 17
0
 public void SetNumberState(NumberState state) =>
 _state = state;
Esempio n. 18
0
 private BigNumber(byte[] buf, int scale, NumberState state)
 {
     numberState = state;
     if (numberState == NumberState.None) {
         BigInteger bigint = new BigInteger(buf);
         SetBigDecimal(new BigDecimal(bigint, scale));
     }
 }
Esempio n. 19
0
        private static Token GetToken(string json, ref int i)
        {
            while (i < json.Length && char.IsWhiteSpace(json[i]))
            {
                i++;
            }

            Token  token = new Token();
            string sub   = json.Substring(i);

            if (json[i] == '{')
            {
                token.type = TokenType.LBrace;
                i          = i + 1;
            }
            else if (json[i] == '}')
            {
                token.type = TokenType.RBrace;
                i          = i + 1;
            }
            else if (json[i] == '[')
            {
                token.type = TokenType.LBracket;
                i          = i + 1;
            }
            else if (json[i] == ']')
            {
                token.type = TokenType.RBracket;
                i          = i + 1;
            }
            else if (json[i] == ':')
            {
                token.type = TokenType.Colon;
                i          = i + 1;
            }
            else if (json[i] == ',')
            {
                token.type = TokenType.Comma;
                i          = i + 1;
            }
            else if (sub.StartsWith("true"))
            {
                token.type = TokenType.True;
                i          = i + 4;
            }
            else if (sub.StartsWith("false"))
            {
                token.type = TokenType.False;
                i          = i + 4;
            }
            else if (sub.StartsWith("null"))
            {
                token.type = TokenType.Null;
                i          = i + 4;
            }
            else if (json[i] == '"')        //string
            {
                StringBuilder sb = new StringBuilder();
                i++;

                while (i < json.Length)
                {
                    int len = 0;
                    while (i + len < json.Length && json[i + len] != '"' && json[i + len] != '\\')
                    {
                        len++;
                    }

                    sb.Append(json.Substring(i, len));

                    if (i + len >= json.Length)
                    {
                        throw new Exception("未找到匹配的引号");
                    }
                    else if (json[i + len] == '"')
                    {
                        i += len + 1;
                        break;
                    }

                    i += len + 1;
                    switch (json[i])
                    {
                    case '"':
                        sb.Append('"');
                        i++;
                        break;

                    case '\\':
                        sb.Append('\\');
                        i++;
                        break;

                    case '/':
                        sb.Append('/');
                        i++;
                        break;

                    case 'b':
                        sb.Append('\b');
                        i++;
                        break;

                    case 'f':
                        sb.Append('\f');
                        i++;
                        break;

                    case 'n':
                        sb.Append('\n');
                        i++;
                        break;

                    case 'r':
                        sb.Append('\r');
                        i++;
                        break;

                    case 't':
                        sb.Append('\t');
                        i++;
                        break;

                    case 'u':
                        string hex  = json.Substring(i + 1, 4);
                        ushort code = Convert.ToUInt16(hex, 16);
                        char   c    = (char)code;
                        sb.Append(c);
                        i += 5;
                        break;
                    }
                }

                token.value = sb.ToString();
                token.type  = TokenType.String;
            }
            else if (json[i] == '-' || char.IsNumber(json[i]))    //nubmer
            {
                dynamic value              = 0;
                bool    isMinus            = false;
                int     frac               = 10;
                bool    isExponentNegative = false;
                int     exponent           = 0;

                NumberState state = NumberState.Start;

                while (state != NumberState.End)
                {
                    switch (state)
                    {
                    case NumberState.Start:
                        if (json[i] == '-')
                        {
                            isMinus = true;

                            state = NumberState.MinusRead;
                        }
                        else if (json[i] == '0')
                        {
                            state = NumberState.ZeroRead;
                        }
                        else if (char.IsNumber(json[i]))
                        {
                            value = CharToInt(json[i]);
                            state = NumberState.InInt;
                        }
                        else
                        {
                            throw new Exception("不能识别的字符‘" + json[i] + "’");
                        }
                        i++;
                        break;

                    case NumberState.MinusRead:
                        if (json[i] == '0')
                        {
                            state = NumberState.ZeroRead;
                        }
                        else if (char.IsNumber(json[i]))
                        {
                            value = CharToInt(json[i]);
                            state = NumberState.InInt;
                        }
                        else
                        {
                            throw new Exception("不能识别的字符‘" + json[i] + "’");
                        }
                        i++;
                        break;

                    case NumberState.InInt:
                        if (char.IsNumber(json[i]))
                        {
                            value *= 10;
                            value += CharToInt(json[i]);
                        }
                        else if (json[i] == '.')
                        {
                            state = NumberState.DotRead;
                        }
                        else if (json[i] == 'e' || json[i] == 'E')
                        {
                            state = NumberState.ERead;
                        }
                        else
                        {
                            i--;
                            state = NumberState.End;
                        }
                        i++;
                        break;

                    case NumberState.ZeroRead:
                        if (json[i] == '.')
                        {
                            state = NumberState.DotRead;
                        }
                        else if (json[i] == 'e' || json[i] == 'E')
                        {
                            state = NumberState.ERead;
                        }
                        else
                        {
                            i--;
                            state = NumberState.End;
                        }
                        i++;
                        break;

                    case NumberState.DotRead:
                        if (char.IsNumber(json[i]))
                        {
                            value += (double)CharToInt(json[i]) / frac;
                            frac  *= 10;
                            state  = NumberState.InFraction;
                        }
                        else
                        {
                            throw new Exception("不能识别的字符‘" + json[i] + "’");
                        }
                        i++;
                        break;

                    case NumberState.InFraction:
                        if (char.IsNumber(json[i]))
                        {
                            value += (double)CharToInt(json[i]) / frac;
                            frac  *= 10;
                        }
                        else if (json[i] == 'e' || json[i] == 'E')
                        {
                            state = NumberState.ERead;
                        }
                        else
                        {
                            i--;
                            state = NumberState.End;
                        }
                        i++;
                        break;

                    case NumberState.ERead:
                        if (char.IsNumber(json[i]))
                        {
                            exponent = CharToInt(json[i]);
                            state    = NumberState.InExponent;
                        }
                        else if (json[i] == '+')
                        {
                            state = NumberState.InExponent;
                        }
                        else if (json[i] == '-')
                        {
                            isExponentNegative = true;
                            state = NumberState.InExponent;
                        }
                        else
                        {
                            throw new Exception("不能识别的字符‘" + json[i] + "’");
                        }
                        i++;
                        break;

                    case NumberState.InExponent:
                        if (char.IsNumber(json[i]))
                        {
                            exponent *= 10;
                            exponent  = CharToInt(json[i]);
                        }
                        else
                        {
                            i--;
                            state = NumberState.End;
                        }
                        i++;
                        break;
                    }
                    if (i >= json.Length)
                    {
                        state = NumberState.End;
                    }
                }

                if (exponent != 0)
                {
                    if (isExponentNegative)
                    {
                        value = value * Math.Pow(10, -exponent);
                    }
                    else
                    {
                        value = value * Math.Pow(10, exponent);
                    }
                }
                if (isMinus)
                {
                    token.value = -value;
                }
                else
                {
                    token.value = value;
                }
                token.type = TokenType.Number;
            }
            else
            {
                throw new Exception("不能识别的字符‘" + json[i] + "’");
            }

            while (i < json.Length && char.IsWhiteSpace(json[i]))
            {
                i++;
            }
            return(token);
        }
Esempio n. 20
0
 private Number(NumberState state, NumberKind kind)
 {
     this.State = state;
     this.Kind  = kind;
 }