private static void TestStore(Double value) { Double ai = value; BigNum ab = ai; // implicit conversion String bi = ai.ToString(); String bn = ab.ToString(); Console.WriteLine("{0,18} : {1,18} -> {2}", ai, bn, bi == bn ? "Pass" : "Fail"); }
public static BigNum Sin(BigNum theta) { BigNumFactory f = theta.Factory; // calculate sine using the taylor series, the infinite sum of x^r/r! but to n iterations BigNum retVal = f.Zero; // first, reduce this to between 0 and 2Pi if (theta > f.TwoPi || theta < f.Zero) { theta = theta % f.TwoPi; } Boolean subtract = false; // using bignums for sine computation is too heavy. It's faster (and just as accurate) to use Doubles #if DoubleTrig Double thetaDbl = Double.Parse(theta.ToString(), Cult.InvariantCulture); for (Int32 r = 0; r < 20; r++) // 20 iterations is enough, any more just yields inaccurate less-significant digits { Double xPowerR = Math.Pow(thetaDbl, 2 * r + 1); Double factori = BigMath.Factorial((double)(2 * r + 1)); Double element = xPowerR / factori; Double addThis = subtract ? -element : element; BigNum addThisBig = f.Create(addThis); retVal += addThisBig; subtract = !subtract; } #else for (Int32 r = 0; r < _iterations; r++) { BigNum xPowerR = theta.Power(2 * r + 1); BigNum factori = Factorial(2 * r + 1); BigNum element = xPowerR / factori; retVal += subtract ? -element : element; subtract = !subtract; } #endif // TODO: This calculation generates useless and inaccurate trailing digits that must be truncated // so truncate them, when I figure out how many digits can be removed retVal.Truncate(10); return(retVal); }
private static void EvaluateExpression(String expression) { try { Expression expr = new Expression(expression, Factory); BigNum ret = expr.Evaluate(_symbols); Console.WriteLine("Result: " + ret.ToString()); } catch (Exception ex) { PrintException(ex); } }
private static Boolean PromptUser() { Console.Write((++_count).ToString()); Console.Write(">"); String s = Console.ReadLine(); if (s == "q") { return(false); } if (s.StartsWith("add ", StringComparison.OrdinalIgnoreCase)) { String name = s.Substring(4, s.IndexOf('=') - 5).Trim(); String expr = s.Substring(s.IndexOf('=') + 1).Trim(); try { Expression xp = new Expression(expr, Factory); BigNum ret = xp.Evaluate(_symbols); if (_symbols.ContainsKey(name)) { _symbols[name] = ret; } else { _symbols.Add(name, ret); } Console.WriteLine("Added: " + name + " = " + ret.ToString()); } catch (Exception ex) { PrintException(ex); } } else if (s.StartsWith("rem ", StringComparison.OrdinalIgnoreCase)) { String name = s.Substring(4); _symbols.Remove(name); Console.WriteLine("Removed: " + name); } else if (String.Equals(s, "help", StringComparison.OrdinalIgnoreCase)) { PrintHelp(); } else if (String.Equals(s, "test", StringComparison.OrdinalIgnoreCase)) { // BigNumTests.Test(); } else { Int32 startIndex; Function func = IsFunction(s, out startIndex); if (func != Function.None) { EvaluateFunction(func, s.Substring(startIndex)); } else { EvaluateExpression(s); } } return(true); }