private Exponential multiply(Exponential a, Exponential b, int exponent) { if (exponent > 0) return Exponential.Multiply(a, b); else if (exponent < 0) return Exponential.Divide(a, b); else // (exponent == 0) return a; }
public void NotationErrors() { Exponential a; a = new Exponential(4); Assert.AreEqual(4m, a.Significand); Assert.AreEqual(0.5m, a.Error); a = new Exponential(4.0m); Assert.AreEqual(4m, a.Significand); Assert.AreEqual(0.05m, a.Error); a = new Exponential(4e3m); Assert.AreEqual(4m, a.Significand); Assert.AreEqual(3, a.Exponent); Assert.AreEqual(0.0005m, a.Error); }
public void Normalizing() { Exponential a; a = new Exponential(34, 3); Assert.AreEqual(3.4m, a.Significand); Assert.AreEqual(4, a.Exponent); a = new Exponential(0.0049m, 0); Assert.AreEqual(4.9m, a.Significand); Assert.AreEqual(-3, a.Exponent); a = new Exponential("9.1093822e-31"); Assert.AreEqual(9.1093822m, a.Significand); Assert.AreEqual(-31, a.Exponent); }
public void StringConversion() { Exponential e; e = new Exponential("23.343e8"); Assert.AreEqual(2.3343m, e.Significand); Assert.AreEqual(9, e.Exponent); Assert.AreEqual(0.00005m, e.Error); e = new Exponential("4.3"); Assert.AreEqual(4.3m, e.Significand); Assert.AreEqual(0, e.Exponent); Assert.AreEqual(0.05m, e.Error); e = new Exponential("40"); Assert.AreEqual(4.0m, e.Significand); Assert.AreEqual(1, e.Exponent); Assert.AreEqual(0.05m, e.Error); }
public Exponential ConvertToBase(Exponential value) { return value * this.Factor; // 1kg -> 1000 g }
public static ConversionMethod BuildConversion(string formula, Exponential number) { ParameterExpression param = Expression.Parameter(typeof(Exponential), "value"); Expression body = Expression.Multiply(param, Expression.Constant(number)); foreach (Unary u in Parser.ToUnaryTokens(formula).Numerics()) { Exponential factor = u.Numeric(); if (u.Exponent == 1) body = Expression.Multiply(body, Expression.Constant(factor)); else if (u.Exponent == -1) body = Expression.Divide(body, Expression.Constant(factor)); } Expression<ConversionMethod> expression = Expression.Lambda<ConversionMethod>(body, param); ConversionMethod method = expression.Compile(); return method; }
public Quantity(Exponential value, Metric metric) { this.Value = value; this.Metric = metric; }
public Quantity(Exponential value, Unit unit) { this.Value = value; this.Metric = new Metric(unit); }
/// <summary> /// Builds a quantity from an Exponential and a metric (parsed from the symbols) /// </summary> /// <param name="value">The number part of a quantity</param> /// <param name="symbols"> /// An expression containing units that are separated by a multiplication '.' or division '/' and can be followed /// by a power. <para> For example: kg.m.s-2 or kg.m/s2</para> /// </param> public Quantity Quantity(Exponential value, string symbols) { Metric metric = Metrics.ParseMetric(symbols); return new Quantity(value, metric); }
public static Exponential Substract(Exponential a, Exponential b) { Exponential result; a = rebase(a, b); result.Significand = a.Significand - b.Significand; result.Exponent = a.Exponent; result.Error = a.Error + b.Error; result.Normalize(); return result; }
private static decimal factorError(decimal significant, Exponential a, Exponential b) { decimal delta_a = (a.Significand != 0) ? (a.Error / a.Significand) : 0; decimal delta_b = (b.Significand != 0) ? (b.Error / b.Significand) : 0; decimal factor = delta_a + delta_b + (a.Error * b.Error); return significant * factor; }
private static Exponential rebase(Exponential a, Exponential b) { // base the value of a to the exponent of b Exponential result; int dex = b.Exponent - a.Exponent; // dex = decimal exponent result.Significand = Shift(a.Significand, -dex); result.Error = Shift(a.Error, -dex); result.Exponent = a.Exponent + dex; return result; }
public static Exponential Normalize(Exponential e) { Exponential result = Exponential.CopyOf(e); result.Normalize(); return result; }
public static Exponential CopyOf(Exponential e) { return new Exponential(e.Significand, e.Exponent, e.Error); }
public void Add() { Exponential a, b, c; a = new Exponential(4.0m); b = new Exponential(2.0m); c = a + b; Assert.AreEqual(6.0m, c.Significand); Assert.AreEqual(0.1m, c.Error); Assert.AreEqual(0, c.Exponent); a = new Exponential(4.0m); b = new Exponential(20.0m); c = a + b; Assert.AreEqual(2.4m, c.Significand); Assert.AreEqual(0.01m, c.Error); Assert.AreEqual(1, c.Exponent); // Normalization test: a = 8m; b = 7m; c = a + b; Assert.AreEqual(1.5m, c.Significand); Assert.AreEqual(0.1m, c.Error); Assert.AreEqual(1, c.Exponent); }
public static Exponential Exact(string s) { Exponential e = new Exponential(s); e.Error = 0; return e; }
/// <summary> /// Addes a prefix to the system /// </summary> /// <param name="name">The name of the prefix (for example "Kilo")</param> /// <param name="symbol">The symbol for the prefix (for example "k")</param> /// <param name="factor">The multiplication factor (in case of kilo, 1000)</param> /// <returns></returns> public Prefix AddPrefix(string name, string symbol, Exponential factor) { Prefix p = new Prefix(name, symbol, factor); Metrics.Prefixes.Add(p); return p; }
public static Exponential Multiply(Exponential a, Exponential b) { Exponential result; result.Significand = a.Significand * b.Significand; result.Exponent = a.Exponent + b.Exponent; result.Error = factorError(result.Significand, a, b); result.Normalize(); return result; }
/// <summary> /// Parses a value and symbol string to a Quantity /// </summary> /// <param name="value">A number as a string. May contain a decimal power (e)</param> /// <param name="symbols"> /// An expression containing units that are separated by a multiplication '.' or division '/' and can be followed /// by a power. <para> For example: kg.m.s-2 or kg.m/s2</para> /// </param> public Quantity Quantity(string value, string symbols) { Exponential number = new Exponential(value); Metric metric = Metrics.ParseMetric(symbols); return new Quantity(number, metric); }
/// <summary> /// Divides two exponentials /// </summary> /// <param name="a">numerator</param> /// <param name="b">denominator</param> /// <returns></returns> public static Exponential Divide(Exponential a, Exponential b) { Exponential result; result.Significand = a.Significand / b.Significand; result.Exponent = a.Exponent - b.Exponent; if (a.Significand == 0) { result.Error = a.Error / b.Significand; } else { result.Error = factorError(result.Significand, a, b); } result.Normalize(); return result; }
public Quantity(Exponential value, Prefix prefix, Unit unit) { this.Value = value; this.Metric = new Metric(prefix, unit); }
public static bool Similar(Exponential a ,Exponential b) { return a.ToString() == b.ToString(); }
public void AddConversion(SystemOfUnits system, string from, string formula, Exponential number) { Metric metricfrom = system.Metrics.ParseMetric(from); Metric metricto = system.Metrics.ParseMetric(Parser.ToUnaryTokens(formula).NonNumerics()); if ( (metricfrom != null) && (metricto != null) ) { ConversionMethod method = BuildConversion(formula, number); system.Conversions.Add(metricfrom, metricto, method); } }
/// <summary> /// Tests if the an number including error margin lies within the error margin of the given value. /// </summary> /// <param name="e">the value that determines the range</param> public bool Approximates(Exponential e) { bool expo = (this.Exponent == e.Exponent); bool sigp = (e.Significand + e.Error) >= (this.Significand + this.Error); bool sigm = (e.Significand - e.Error) <= (this.Significand - this.Error); return expo && sigp && sigm; }
public Quantity Convert(Exponential value) { Exponential output = this.method(value); Quantity quantity = new Quantity(output, To); return quantity; }
public bool Approximates(string s) { Exponential e = new Exponential(s); return Approximates(e); }
public Prefix(string name, string symbol, Exponential factor) { this.Name = name; this.Symbol = symbol; this.Factor = factor; }
public static Exponential Power(Exponential value, int power) { double f = Math.Pow(value.ValueToFloat(), power); double e = (value.Error != 0) ? Math.Pow(value.ErrorToFloat(), power) : 0; return new Exponential((decimal)f, 0, (decimal)e); }
public Exponential ConvertFromBase(Exponential value) { return value / this.Factor; // 1000g -> 1kg }
/// <summary> /// Raise value with 10^digits. /// </summary> public static Exponential Raise(Exponential value, int digits) { return value.Raised(digits); }