public override int Compare(CNumber other) { if (other.MyType != MyType) { throw new NotImplementedException(); } CNumber_Integer o = (CNumber_Integer)other; int t_len = size; int o_len = o.size; if (o_len > t_len) { return(-1); } if (o_len < t_len) { return(1); } --t_len; while (t_len > 0 && o._Values[t_len] == _Values[t_len]) { --t_len; } return(Math.Sign(_Values[t_len] - o._Values[t_len])); }
//------------------------------------------------------------ // Add a result to the output TextBox // returns true if the Term was added protected bool Add(string Term) { // write to the output NewEntry(Term); try { // let the calculation begin // this will throw an exception on error MMC.Numbers.CNumber Result = Env.Evaluate(Term); if (Result == null) { throw new MMC.Calc.CTermException("could not calculate!"); } //---------------------------------------------------- // add the term to the history listbox AddToHistory(Term); //------------------------------------------------ // and write the result to the output // print for all output-bases foreach (int Base in _OutputBase.Bases) { NewSubEntry(Result.ToString((uint)Base), Color_Default); } } catch (MMC.Calc.CTermException exc) { NewSubEntry(exc.Message, Color_Error); return(false); } return(true); }
public override CNumber xor(CNumber other) { if (other.MyType != MyType) { throw new NotImplementedException(); } return(new CNumber_Double(((long)_Value) ^ ((long)((CNumber_Double)other)._Value))); }
//------------------------------------------------------------ // the little operations public override CNumber pow(CNumber other) { if (other.MyType != MyType) { throw new NotImplementedException(); } return(new CNumber_Double(Math.Pow(_Value, ((CNumber_Double)other)._Value))); }
//------------------------------------------------------------ // which one is bigger? public override int Compare(CNumber other) { if (other.MyType != MyType) { throw new NotImplementedException(); } return(Math.Sign(_Value - ((CNumber_Double)other)._Value)); }
public override bool Equals(CNumber other) { if (other.MyType != MyType) { return(false); } return(_Value == ((CNumber_Double)other)._Value); }
//------------------------------------------------------------ // general helpers // Sets the seconds since 1/1/19070 public virtual void Date(CNumber year, CNumber month, CNumber day) { DateTime dt = new DateTime(year.AsInteger, month.AsInteger, day.AsInteger); long dt_s = dt.ToFileTimeUtc(); long bgn_s = 0x019db1ded53e8000; // DateTime(1970, 1, 1).ToFileTimeUtc() long diff = dt_s - bgn_s; // diff in 100 ns diff /= 10 * 1000 * 1000; // diff in s AsInteger = (int)diff; }
public override CNumber rem(CNumber other) { if (other.MyType != MyType) { throw new NotImplementedException(); } long result; Math.DivRem((long)_Value, (long)(((CNumber_Double)other)._Value), out result); return(new CNumber_Double(result)); }
//------------------------------------------------------------ // simple divide the both terms public override MMC.Numbers.CNumber Calc(MMC.Calc.CEnvironment Env) { if (!(_bTermA && _bTermB)) { throw new CTermException("Div not initialized!"); } MMC.Numbers.CNumber B = _TermB.Calc(Env); if (B.IsZero) { throw new CTermException("Division by zero!"); } return(_TermA.Calc(Env).div(B)); }
//------------------------------------------------------------ // helper to create a new number public MMC.Numbers.CNumber NewNumber(double Value) { MMC.Numbers.CNumber res = null; switch (_NumberType) { case MMC.Numbers.CNumber.CNumberType.cnt_Double: res = new MMC.Numbers.CNumber_Double(Value); break; case MMC.Numbers.CNumber.CNumberType.cnt_Integer: res = new MMC.Numbers.CNumber_Integer(Value); break; default: break; } return(res); }
public override CNumber xor(CNumber other) { if (other.MyType != MyType) { throw new NotImplementedException(); } CNumber_Integer res = new CNumber_Integer(this); CNumber_Integer o = (CNumber_Integer)other; int o_len = o._Values.Count; // the other length int t_len = _Values.Count; // our own length int len = (t_len < o_len) ? t_len : o_len; // calc the minimum length int i = 0; // start with the lowest digit for (; i < len; i++) // first handle the common digits { res._Values[i] ^= o._Values[i]; } res.Trim(); return(res); }
//------------------------------------------------------------ // analyse the string and return the operation public MMC.Calc.CTerm_Base FindOp(ref string Term) { //-------------------------------------------------------- // make sure we have something to do if (Term != null) { int length = Term.Length; int idx = 0; while (idx < length) { char c = Term[idx]; if (c != ' ' && c != '\t' && c != '\n' && c != '\r') { break; } idx++; } Term = Term.Substring(idx); } if (string.IsNullOrEmpty(Term)) { return(null); } //-------------------------------------------------------- // find the match in the various operation maps MMC.Calc.CTerm_Base Op = _Operations.Get(Term, 0); if (Op != null) { Term = Term.Remove(0, Op.Name.Length); return(Op.Clone()); } Op = _Support.Get(Term, 0); if (Op != null) { Term = Term.Remove(0, Op.Name.Length); return(Op.Clone()); } Op = _Constants.Get(Term, 0); if (Op != null) { Term = Term.Remove(0, Op.Name.Length); return(Op); // Do not clone a constant } Op = _Variables.Get(Term, 0); if (Op != null) { Term = Term.Remove(0, Op.Name.Length); return(Op); // Do not clone a variable } //-------------------------------------------------------- // is it a number MMC.Numbers.CNumber N = NewNumber(0.0); if (N.FromString(ref Term)) { return(new MMC.Calc.Functions.CTerm_Number(N)); } //-------------------------------------------------------- // check for an assignment for (int pos = 1; pos < Term.Length; pos++) { Op = _Support.Get(Term, pos); if (Op != null && Op.Type == CTerm_Base.TTermType.Assignment) { MMC.Calc.Functions.CTerm_Variable Var = new MMC.Calc.Functions.CTerm_Variable(Term.Substring(0, pos)); Term = Term.Remove(0, pos); return(Var); } } //-------------------------------------------------------- // nothing found so something's wrong somewhere throw new CTermException("Unknown Function or Variable!"); }
public abstract CNumber mul(CNumber other);
// Sets the day in the week (0: sunday .. 6:saturday) public virtual void Day(CNumber year, CNumber month, CNumber day) { DateTime dt = new DateTime(year.AsInteger, month.AsInteger, day.AsInteger); AsInteger = (int)dt.DayOfWeek; }
public abstract int Compare(CNumber other);
public abstract CNumber and(CNumber other);
public virtual bool Equals(CNumber other) { return(Compare(other) == 0); }
public abstract CNumber or(CNumber other);
//------------------------------------------------------------ // the higher functions operations public abstract CNumber pow(CNumber a);
public abstract CNumber rem(CNumber other);
public abstract CNumber div(CNumber other);
public abstract CNumber sub(CNumber other);