Esempio n. 1
0
        public void Render(INumeric numeric)
        {
            var sp = new StackPanel();

            sp.Orientation = Orientation.Horizontal;
            for (int i = 0; i < numeric.Number; i++)
            {
                var r = new Rectangle();
                r.Style = (Style)FindResource("NumericRectangleStyle");
                sp.Children.Add(r);
            }
            if (numeric.Suffix != string.Empty)
            {
                var tb = new TextBlock();
                tb.Style = (Style)FindResource("NumericSuffixTextStyle");
                tb.Text  = numeric.Suffix;
                sp.Children.Add(tb);
            }
            Grid.SetRow(sp, numeric.Row);
            Grid.SetColumn(sp, numeric.Column);
            Grid.SetRowSpan(sp, numeric.Height);
            Grid.SetColumnSpan(sp, numeric.Width);
            ClearGrid(numeric.Row, numeric.Column, numeric.Width, numeric.Height);
            PreviewGrid.Children.Add(sp);
        }
Esempio n. 2
0
        /// <summary>
        /// fluent.  makes larger by numShifts orders of magnitude
        /// </summary>
        /// <param name="thisNumber"></param>
        /// <param name="numShifts"></param>
        /// <returns></returns>
        public static INumeric ShiftRight(this INumeric thisNumber, INumeric numShifts)
        {
            if (thisNumber == null)
                throw new ArgumentNullException("thisNumber");

            if (numShifts == null)
                throw new ArgumentNullException("numShifts");

            if (!thisNumber.IsEmpty())
            {
                numShifts.PerformThisManyTimes(c =>
                {
                    thisNumber.HasShift().ShiftRight();
                });
            }
            return thisNumber;
        }
Esempio n. 3
0
        public override IMatched <IObject> Execute(Machine machine, INumeric x)
        {
            var count = x.AsInt32();
            var stack = new Stack <IObject>();

            for (var i = 0; i < count; i++)
            {
                if (machine.Pop().If(out var obj, out var exception))
                {
                    stack.Push(obj);
                }
                else
                {
                    return(failedMatch <IObject>(exception));
                }
            }

            var array     = stack.ToArray();
            var arguments = new Arguments(array);

            return(arguments.Matched <IObject>());
        }
Esempio n. 4
0
        public static IMatched <IObject> Evaluate(INumeric x)
        {
            switch (x)
            {
            case Int i:
                return(Int.IntObject(-i.Value).Matched());

            case Float f:
                return(Float.FloatObject(-f.Value).Matched());

            case Long l:
                return(Long.LongObject(-l.Value).Matched());

            case Complex c:
                return(c.Negate().Matched());

            case Rational r:
                return(r.Negate().Matched());

            default:
                return(failedMatch <IObject>(notNumeric((IObject)x)));
            }
        }
Esempio n. 5
0
 public override IMatched <IObject> Execute(Machine machine, INumeric x) => Evaluate(x);
Esempio n. 6
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="number1"></param>
        /// <param name="number2"></param>
        /// <param name="setValueIndicated">this is true if the return value is a clone and not the 
        /// same instance as number1</param>
        /// <returns></returns>
        private static INumeric Add(INumeric number1, INumeric number2)
        {
            if (number1 == null)
            {
                return number2;
            }
            if (number2 == null)
                return number1;

            //     Debug.WriteLine("adding {0} {1}", number1.SymbolsText,
            //number2.SymbolsText);

            INumeric rv = null;

            //determine the longer number
            bool? num1IsLonger = Numeric.AbsoluteValueCompare(number1.GetInnermostNumeric(),
                number2.GetInnermostNumeric());

            //note: below we clone the 2nd arg if it is being modified as part of the operation
            //we don't clone the 1st arg as it is
            if (number1.IsPositive && number2.IsPositive)
            {
                rv = Increment(number1, number2);
            }
            else if (number1.IsPositive && number2.IsPositive == false)
            {
                switch (num1IsLonger)
                {
                    case null:
                        rv = Decrement(number1, number2);
                        break;
                    case true:
                        rv = Decrement(number1, number2);
                        break;
                    case false:
                        rv = Decrement(number2, number1);
                        rv.GetInnermostNumeric().IsPositive = false;
                        break;
                }
            }
            else if (number1.IsPositive == false && number2.IsPositive)
            {
                switch (num1IsLonger)
                {
                    case null:
                        rv = Decrement(number1, number2);
                        break;
                    case true:
                        rv = Decrement(number1, number2);
                        break;
                    case false:
                        rv = Decrement(number2, number1);
                        rv.GetInnermostNumeric().IsPositive = true;
                        break;
                }
            }
            else if (number1.IsPositive == false && number2.IsPositive == false)
            {
                rv = Increment(number1, number2);
                rv.GetInnermostNumeric().IsPositive = false;
            }

            return rv;
        }
Esempio n. 7
0
        /// <summary>
        /// treats numbers as signless, and increase the value of the host number by the amount
        /// </summary>
        /// <param name="number"></param>
        /// <param name="number2"></param>
        private static INumeric Increment(INumeric number1, INumeric number2)
        {
            if (number1 == null)
                throw new ArgumentNullException("number1");

            if (number2 == null)
                throw new ArgumentNullException("number2");

            //the add process
            var addNode1 = number1.ZerothDigit;
            var addNode2 = number2.ZerothDigit;

            //add after the decimal
            while (addNode2 != null)
            {
                addNode1.Add(addNode2.NodeValue.Symbol);

                addNode2 = addNode2.NextNode as IDigitNode;
                if (addNode2 != null)
                    addNode1 = addNode1.LoadNextDigit();
            }

            //add before the decimal
            addNode2 = number2.ZerothDigit.PreviousDigit();

            if (addNode2 != null)
                addNode1 = (number1.ZerothDigit).LoadPreviousDigit();

            while (addNode2 != null)
            {
                addNode1.NodeValue.Add(addNode2.NodeValue.Symbol);

                addNode2 = addNode2.PreviousDigit();
                if (addNode2 != null)
                    addNode1 = addNode1.LoadPreviousDigit();
            }
            return number1;
        }
        public void Subtract(INumeric numeric)
        {
            if (numeric == null)
                return;

            //if (!this.HasCompatibleNumberSystem(numeric))
            //    throw new InvalidOperationException("incompatible number system");

            numeric.As<Numeric>().SwitchSign();
            bool isClone = false;
            var val = Add(this, numeric, out isClone);
            numeric.As<Numeric>().SwitchSign(); //we don't want to return a modified arg

            if (isClone)
                this.As<Numeric>().SetValue(val);
        }
Esempio n. 9
0
        public void Subtract(INumeric numeric)
        {
            if (numeric == null)
                return;

            //if (!this.HasCompatibleNumberSystem(numeric))
            //    throw new InvalidOperationException("incompatible number system");

            var numeric1 = this.GetInnermostNumeric().Clone() as Numeric;
            var numeric2 = numeric.GetInnermostNumeric().Clone() as Numeric;
            numeric2.SwitchSign();

            //apply whatever decorations this cake has to the clones
            //we do this so that node hooks can be applied, for instance
            var num2 = this.CloneDecorationCake(numeric2) as INumeric;
            var num1 = this.CloneDecorationCake(numeric1) as INumeric;

            var val = Add(num1, num2);
            this.SetValue(val);
        }
Esempio n. 10
0
 public override IMatched <IObject> Execute(INumeric x, INumeric y)
 {
     return(((Rational)(x.AsBigInteger(), y.AsBigInteger())).Matched <IObject>());
 }
Esempio n. 11
0
        public PrecisionNumericDecoration(object decorated,
            INumeric decimalPlaces,
            string cakeName = null)
            : base(decorated, cakeName)
        {
            if (decimalPlaces == null)
                throw new ArgumentNullException("decimalPlaces");

            this.DecimalPlaces = decimalPlaces.GetInnermostNumeric();
        }
Esempio n. 12
0
 public override IMatched <IObject> Execute(Machine machine, INumeric x) => Boolean.BooleanObject(x.IsNegative).Matched();
Esempio n. 13
0
 public int CompareTo(INumeric other)
 {
     return(IntValue.CompareTo(other.IntValue));
 }
Esempio n. 14
0
 public static PrecisionNumericDecoration HasPrecision(this object decorated,
     INumeric decimalPlaces,
     string cakeName = null)
 {
     return PrecisionNumericDecoration.New(decorated, decimalPlaces, cakeName);
 }
Esempio n. 15
0
 public override void SetValue(INumeric number)
 {
     number.TruncateToDecimalPlaces(this.DecimalPlaces);
     base.SetValue(number);
 }
Esempio n. 16
0
 public static PrecisionNumericDecoration New(object decorated,
     INumeric decimalPlaces,
     string cakeName = null)
 {
     return new PrecisionNumericDecoration(decorated, decimalPlaces, cakeName);
 }
Esempio n. 17
0
 public abstract IMatched <IObject> Execute(INumeric x, INumeric y);
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            // Set the global variables
            drawIf = attribute as DrawIfAttribute;

            // Whether the condition has been met
            bool conditionMet = false;

            // If we are doing a property comparison
            if (drawIf.predicate == PredicateMode.PropertyComparison)
            {
                comparedField = property.serializedObject.FindProperty(drawIf.comparedPropertyName);
                // Get the value of the compared field
                object comparedFieldValue = comparedField.GetValue <object>();
                // References to the values as numeric types
                INumeric numericComparedFieldValue = null;
                INumeric numericComparedValue      = null;

                // Try to set the numeric types
                try
                {
                    numericComparedFieldValue = new INumeric(comparedFieldValue);
                    numericComparedValue      = new INumeric(drawIf.comparedValue);
                }
                catch (NumericTypeExpectedException)
                {
                    if (drawIf.comparison != ComparisonType.Equals && drawIf.comparison != ComparisonType.NotEqual)
                    {
                        StratusDebug.Error("The only comparsion types available to type '" + comparedFieldValue.GetType() + "' are Equals and NotEqual. (On object '" + property.serializedObject.targetObject.name + "')", null, true);
                        return;
                    }
                }
                // Compare the values to see if the condition has been met
                switch (drawIf.comparison)
                {
                case ComparisonType.Equals:
                    if (comparedFieldValue.Equals(drawIf.comparedValue))
                    {
                        conditionMet = true;
                    }
                    break;

                case ComparisonType.NotEqual:
                    if (!comparedFieldValue.Equals(drawIf.comparedValue))
                    {
                        conditionMet = true;
                    }
                    break;

                case ComparisonType.Greater:
                    if (numericComparedFieldValue > numericComparedValue)
                    {
                        conditionMet = true;
                    }
                    break;

                case ComparisonType.Lesser:
                    if (numericComparedFieldValue < numericComparedValue)
                    {
                        conditionMet = true;
                    }
                    break;

                case ComparisonType.LesserOrEqual:
                    if (numericComparedFieldValue <= numericComparedValue)
                    {
                        conditionMet = true;
                    }
                    break;

                case ComparisonType.GreaterOrEqual:
                    if (numericComparedFieldValue >= numericComparedValue)
                    {
                        conditionMet = true;
                    }
                    break;
                }
            }
            // Else if we are checking a predicate
            else if (drawIf.predicate == PredicateMode.Predicate)
            {
                //var booly = property.serializedObject..GetProperty<bool>(DrawIf.predicateName);
                //SerializedProperty predicateProperty = property.serializedObject.FindProperty(DrawIf.predicateName);
                //if (predicateProperty.propertyType == SerializedPropertyType.Boolean)
                //  conditionMet = predicateProperty.boolValue;

                MonoBehaviour mb = property.serializedObject.targetObject as MonoBehaviour;
                MethodInfo    predicateMethod = mb.GetType().GetMethod(drawIf.predicateName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                if (predicateMethod != null)
                {
                    conditionMet = (bool)predicateMethod.Invoke(mb, null);
                }
                else
                {
                    PropertyInfo predicateProperty = mb.GetType().GetProperty(drawIf.predicateName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                    if (predicateProperty != null)
                    {
                        conditionMet = (bool)predicateProperty.GetValue(mb, null);
                    }
                    else
                    {
                        throw new System.Exception("The component is missing the predicate" + drawIf.predicateName);
                    }
                }



                //// Make sure that the right component is present
                //Component component = Selection.activeGameObject.GetComponent(drawIf.type);
                //if (component == null)
                //  throw new System.Exception("The component of type " + drawIf.type.Name + " is missing from the selected GameObject");
                //
                //// We can now safely invoke the method on the component
                //if (drawIf.isProperty)
                //  conditionMet = (bool)drawIf.predicateProperty.GetValue(component, null);
                //else
                //  conditionMet = (bool)drawIf.predicateMethod.Invoke(component, null);
            }

            // The height of the property should be defaulted to the default height
            propertyHeight = EditorGUI.GetPropertyHeight(property);
            //propertyHeight = base.GetPropertyHeight(property, label);

            // If the condition is met, draw the field
            if (conditionMet)
            {
                //EditorGUILayout.PropertyField(property);
                EditorGUI.PropertyField(position, property, true);
            }
            // Otherwise use the default ebhavior
            else
            {
                if (drawIf.defaultBehavior == PropertyDrawingType.ReadOnly)
                {
                    UnityEngine.GUI.enabled = false;
                    //EditorGUILayout.PropertyField(property);
                    EditorGUI.PropertyField(position, property, true);
                    UnityEngine.GUI.enabled = true;
                }
                else
                {
                    propertyHeight = 0f;
                }
            }
        }
 private static IBindable <TElement> MinInternal <TElement, TAverage>(IBindableCollection <TElement> source, INumeric <TElement, TAverage> numeric)
 {
     source.ShouldNotBeNull("source");
     return(Aggregate <TElement, TElement>(source, sources => numeric.Min(sources)));
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="number1"></param>
        /// <param name="number2"></param>
        /// <param name="setValueIndicated">this is true if the return value is a clone and not the 
        /// same instance as number1</param>
        /// <returns></returns>
        private static INumeric Add(INumeric number1, INumeric number2, out bool isReplace)
        {
            //the default value is false, as most cases don't need a clone operation
            isReplace = false;

            if (number1 == null)
            {
                isReplace = true;
                return number2;
            }
            if (number2 == null)
                return number1;

            INumeric rv = null;

            //determine the longer number
            bool? num1IsLonger = Numeric.AbsoluteValueCompare(number1.As<Numeric>(),
                number2.As<Numeric>());

            //note: below we clone the 2nd arg if it is being modified as part of the operation
            //we don't clone the 1st arg as it is
            if (number1.IsPositive && number2.IsPositive)
            {
                rv = Increment(number1, number2);
            }
            else if (number1.IsPositive && number2.IsPositive == false)
            {
                switch (num1IsLonger)
                {
                    case null:
                        rv = Decrement(number1, number2);
                        break;
                    case true:
                        rv = Decrement(number1, number2);
                        break;
                    case false:
                        isReplace = true;
                        var cloneNum2 = number2.Clone();
                        rv = Decrement(cloneNum2, number1);
                        rv.As<Numeric>().IsPositive = false;
                        break;
                }
            }
            else if (number1.IsPositive == false && number2.IsPositive)
            {
                switch (num1IsLonger)
                {
                    case null:
                        rv = Decrement(number1, number2);
                        break;
                    case true:
                        rv = Decrement(number1, number2);
                        break;
                    case false:
                        isReplace = true;
                        rv = Decrement(number2.Clone(), number1);
                        rv.As<Numeric>().IsPositive = true;
                        break;
                }
            }
            else if (number1.IsPositive == false && number2.IsPositive == false)
            {
                rv = Increment(number1, number2);
                rv.As<Numeric>().IsPositive = false;
            }

            return rv;
        }
Esempio n. 21
0
 private static IBindable <TAverageResult> AverageInternal <TResult, TAverageResult>(IBindableCollection <TResult> source, INumeric <TResult, TAverageResult> numeric)
 {
     source.ShouldNotBeNull("source");
     return(Aggregate(source, sources => numeric.Average(sources)));
 }
Esempio n. 22
0
 public int CompareTo(INumeric other)
 {
     return(DoubleValue.CompareTo(other.DoubleValue));
 }
Esempio n. 23
0
 public abstract IMatched <IObject> Execute(Machine machine, INumeric x);
        public void Add(INumeric numeric)
        {
            if (numeric == null)
                return;

            //if (!this.HasCompatibleNumberSystem(numeric))
            //    throw new InvalidOperationException("incompatible number system");

            bool isClone = false;
            var val = Add(this, numeric, out isClone);

            if (isClone)
                this.As<Numeric>().SetValue(val);
        }