public NumericValue Root(NumericValue rootObj) { VerifyNoUnits(); var root = (int)rootObj.GetInteger; var f = GetFloat; if (root % 2 == 1) // Odd roots { return(new NumericValue(f >= 0 ? Math.Pow(f, 1.0 / root) : -Math.Pow(-f, 1.0 / root), Units)); } else if (f >= 0) // Even roots, val >= 0 { return(new NumericValue(Math.Pow(f, 1.0 / root), Units)); } throw new Exception("Invalid root"); }
public static string Reduce(NumericValue numerator, NumericValue denominator) { if ((numerator.Units.HasUnits) || (numerator.Units.HasUnits)) { throw new Exception("Inputs cannot have units"); } var num = numerator.Abs().GetInteger; var den = denominator.Abs().GetInteger; if ((num == 0) || (den == 0)) { throw new Exception("Factors cannot be 0"); } var gcf = Helpers.GCF(num, den); return($"{num / gcf}/{den / gcf}"); }
public NumericValue Exp(NumericValue exponentVal) { exponentVal.VerifyNoUnits("Exponent cannot have units."); if (!exponentVal.IsInteger) { VerifyNoUnits("Base can only have units for positive integer exponents."); return(new NumericValue(Math.Pow(GetFloat, exponentVal.GetFloat))); } var exponent = exponentVal.IntValue; object result; if ((exponent >= 0) && (IsInteger)) { result = BigInteger.Pow(GetInteger, exponent); } else { result = Math.Pow(GetFloat, exponent); } return(new NumericValue(result, Units ^ exponent)); }
public override object VisitMethod(ExpressionParser.MethodContext context) { var method = context.method.Text; var paramList = context.e().Select(c => Visit(c)).ToList(); switch (method.ToLowerInvariant()) { case "abs": return(GetNumeric(paramList[0]).Abs()); case "acos": return(GetNumeric(paramList[0]).Acos()); case "asin": return(GetNumeric(paramList[0]).Asin()); case "atan": return(paramList.Count == 2 ? NumericValue.Atan2(GetNumeric(paramList[1]), GetNumeric(paramList[0])) : GetNumeric(paramList[0]).Atan()); case "cos": return(GetNumeric(paramList[0]).Cos()); case "date": return(new NumericValue(new DateTimeOffset(DateTime.Now.Date, DateTimeOffset.Now.Offset).UtcTicks, "ticks")); case "directoryname": return(GetDirectoryName(GetString(paramList[0]))); case "eval": return(new NEExpression(GetString(paramList[0])).InternalEvaluate(variables, row, unit)); case "extension": return(GetExtension(GetString(paramList[0]))); case "factor": return(GetNumeric(paramList[0]).Factor()); case "filename": return(GetFileName(GetString(paramList[0]))); case "filenamewithoutextension": return(GetFileNameWithoutExtension(GetString(paramList[0]))); case "fromdate": return(FromDate(GetString(paramList[0]))); case "fromwords": return(NumericValue.FromWords(GetString(paramList[0]))); case "gcf": return(NumericValue.GCF(paramList.Select(val => GetNumeric(val)).ToList())); case "lcm": return(NumericValue.LCM(paramList.Select(val => GetNumeric(val)).ToList())); case "len": return(GetString(paramList[0]).Length); case "ln": return(GetNumeric(paramList[0]).Ln()); case "log": return(paramList.Count == 1 ? GetNumeric(paramList[0]).Log() : GetNumeric(paramList[0]).Log(GetNumeric(paramList[2]))); case "max": return(paramList.Select(param => GetNumeric(param)).Max()); case "min": return(paramList.Select(param => GetNumeric(param)).Min()); case "multiple": return(NumericValue.Multiple(GetNumeric(paramList[0]), GetNumeric(paramList[1]))); case "now": return(new NumericValue(DateTimeOffset.Now.UtcTicks, "ticks")); case "random": return(NumericValue.Random(GetNumeric(paramList[0]), GetNumeric(paramList[1]))); case "reciprocal": return(GetNumeric(paramList[0]).Reciprocal()); case "reduce": return(NumericValue.Reduce(GetNumeric(paramList[0]), GetNumeric(paramList[1]))); case "root": return(GetNumeric(paramList[0]).Root(GetNumeric(paramList[1]))); case "sin": return(GetNumeric(paramList[0]).Sin()); case "sqrt": return(GetNumeric(paramList[0]).Root(GetNumeric(2))); case "strformat": return(string.Format(GetString(paramList[0]), paramList.Skip(1).Select(param => Simplify(param)).ToArray())); case "tan": return(GetNumeric(paramList[0]).Tan()); case "tmax": return(paramList.Select(param => GetString(param)).Max()); case "tmin": return(paramList.Select(param => GetString(param)).Min()); case "todate": return(ToDate(GetNumeric(paramList[0]), paramList.Count > 1 ? GetNumeric(paramList[1]) : null)); case "toutcdate": return(ToDate(GetNumeric(paramList[0]), new NumericValue(0, "ticks"))); case "towords": return(GetNumeric(paramList[0]).ToWords()); case "type": return(paramList[0].GetType()); case "utcdate": return(new NumericValue(DateTimeOffset.Now.UtcDateTime.Date.Ticks, "ticks")); case "valideval": try { new NEExpression(GetString(paramList[0])).InternalEvaluate(variables, row, unit); return(true); } catch { return(false); } case "validre": return(ValidRE(GetString(paramList[0]))); default: throw new ArgumentException($"Invalid method: {method}"); } }
public static NumericValue Atan2(NumericValue xOfs, NumericValue yOfs) { xOfs.VerifyNoUnits(); yOfs.VerifyNoUnits(); return(new NumericValue(Math.Atan2(xOfs.GetFloat, yOfs.GetFloat), "rad")); }
public NumericValue Log(NumericValue newBase) { VerifyNoUnits(); return(new NumericValue(Math.Log(GetFloat, newBase.GetFloat), Units)); }
public static NumericValue FromWords(string words) { var ones = new List <string> { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", null, "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; var tens = new List <string> { null, "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; var magnitudes = new List <string> { null, "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion", "quindecillion", "sexdecillion", "septdecillion", "octodecillion", "novemdecillion", "vigintillion", "unvigintillion", "duovigintillion", "trevigintillion", "quattuorvigintillion", "quinvigintillion", "sexvigintillion", "septvigintillion", "octovigintillion", "novemvigintillion", "trigintillion", "untrigintillion", "duotrigintillion", "googol", "tretrigintillion", "quattuortrigintillion", "quintrigintillion", "sextrigintillion", "septtrigintillion", "octotrigintillion", "novemtrigintillion", "quadragintillion", "unquadragintillion", "duoquadragintillion", "trequadragintillion", "quattuorquadragintillion", "quinquadragintillion", "sexquadragintillion", "septquadragintillion", "octoquadragintillion", "novemquadragintillion", "quinquagintillion", "unquinquagintillion", "duoquinquagintillion", "trequinquagintillion", "quattuorquinquagintillion", "quinquinquagintillion", "sexquinquagintillion", "septquinquagintillion", "octoquinquagintillion", "novemquinquagintillion", "sexagintillion", "unsexagintillion", "duosexagintillion", "tresexagintillion", "quattuorsexagintillion", "quinsexagintillion", "sexsexagintillion", "septsexagintillion", "octosexagintillion", "novemsexagintillion", "septuagintillion", "unseptuagintillion", "duoseptuagintillion", "treseptuagintillion", "quattuorseptuagintillion", "quinseptuagintillion", "sexseptuagintillion", "septseptuagintillion", "octoseptuagintillion", "novemseptuagintillion", "octogintillion", "unoctogintillion", "duooctogintillion", "treoctogintillion", "quattuoroctogintillion", "quinoctogintillion", "sexoctogintillion", "septoctogintillion", "octooctogintillion", "novemoctogintillion", "nonagintillion", "unnonagintillion", "duononagintillion", "trenonagintillion", "quattuornonagintillion", "quinnonagintillion", "sexnonagintillion", "septnonagintillion", "octononagintillion", "novemnonagintillion", "centillion" }; words = Regex.Replace(Regex.Replace(words.ToLowerInvariant(), "[^0-9.a-z]", " "), "\\s+", " ").Trim(); var tokens = words.Split(' ').Where(str => !string.IsNullOrWhiteSpace(str)).ToList(); double current = 0; var result = new NumericValue(BigInteger.Zero); bool negative = false; using (var itr = tokens.GetEnumerator()) { while (true) { var token = itr.MoveNext() ? itr.Current : null; if (token == "negative") { negative = !negative; continue; } double tokenValue; if ((token != null) && (double.TryParse(token, out tokenValue))) { current += tokenValue; continue; } var onesIndex = token == null ? -1 : ones.IndexOf(token); if (onesIndex != -1) { current += onesIndex; continue; } var tensIndex = token == null ? -1 : tens.IndexOf(token); if (tensIndex != -1) { current += tensIndex * 10; continue; } if (token == "hundred") { current *= 100; continue; } var magnitudesIndex = token == null ? 0 : magnitudes.IndexOf(token); if ((token == null) || (magnitudesIndex != -1)) { var value = new NumericValue(current); var magFactor = new NumericValue(1000); for (var ctr = 0; ctr < magnitudesIndex; ++ctr) { value *= magFactor; } result += value; current = 0; if (token == null) { break; } continue; } throw new Exception("Invalid input"); } } if (negative) { result = -result; } return(result); }
public static NumericValue Random(NumericValue minValue, NumericValue maxValue) => new NumericValue(random.Next((int)minValue.GetInteger, (int)maxValue.GetInteger + 1));
public NumericValue ShiftRight(NumericValue val2) { val2.VerifyNoUnits(); return(new NumericValue(GetInteger >> val2.IntValue, Units)); }
static NumericValue GetSameUnits(NumericValue value, NumericValue unitsValue) => value.Units.Equals(unitsValue.Units) ? value : value.ConvertUnits(unitsValue.Units);
public NumericValue IntDiv(NumericValue divisor) => new NumericValue(GetInteger / divisor.GetInteger, Units / divisor.Units);