Exemplo n.º 1
0
 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;
 }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);

        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
 public Exponential ConvertToBase(Exponential value)
 {
     return value * this.Factor;  // 1kg -> 1000 g
 }
Exemplo n.º 6
0
        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;
        }
Exemplo n.º 7
0
 public Quantity(Exponential value, Metric metric)
 {
     this.Value = value;
     this.Metric = metric;
 }
Exemplo n.º 8
0
 public Quantity(Exponential value, Unit unit)
 {
     this.Value = value;
     this.Metric = new Metric(unit);
 }
Exemplo n.º 9
0
 /// <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);
 }
Exemplo n.º 10
0
        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;
        }
Exemplo n.º 11
0
 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;
 }
Exemplo n.º 12
0
 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;
 }
Exemplo n.º 13
0
 public static Exponential Normalize(Exponential e)
 {
     Exponential result = Exponential.CopyOf(e);
     result.Normalize();
     return result;
 }
Exemplo n.º 14
0
 public static Exponential CopyOf(Exponential e)
 {
     return new Exponential(e.Significand, e.Exponent, e.Error);
 }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
 public static Exponential Exact(string s)
 {
     Exponential e = new Exponential(s);
     e.Error = 0;
     return e;
 }
Exemplo n.º 17
0
 /// <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;
 }
Exemplo n.º 18
0
        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;
        }
Exemplo n.º 19
0
 /// <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);
 }
Exemplo n.º 20
0
        /// <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; 
        }
Exemplo n.º 21
0
 public Quantity(Exponential value, Prefix prefix, Unit unit)
 {
     this.Value = value;
     this.Metric = new Metric(prefix, unit);
 }
Exemplo n.º 22
0
 public static bool Similar(Exponential a ,Exponential b)
 {
     return a.ToString() == b.ToString();
 }
Exemplo n.º 23
0
        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);
            }

        }
Exemplo n.º 24
0
        /// <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;
        }
Exemplo n.º 25
0
 public Quantity Convert(Exponential value)
 {
     Exponential output = this.method(value);
     Quantity quantity = new Quantity(output, To);
     return quantity;
 }
Exemplo n.º 26
0
 public bool Approximates(string s)
 {
     Exponential e = new Exponential(s);
     return Approximates(e);
    
 }
Exemplo n.º 27
0
 public Prefix(string name, string symbol, Exponential factor)
 {
     this.Name = name;
     this.Symbol = symbol;
     this.Factor = factor;
 }
Exemplo n.º 28
0
        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); 
            
        }
Exemplo n.º 29
0
 public Exponential ConvertFromBase(Exponential value)
 {
     return value / this.Factor;  // 1000g -> 1kg
 }
Exemplo n.º 30
0
 /// <summary>
 /// Raise value with 10^digits.
 /// </summary>
 public static Exponential Raise(Exponential value, int digits)
 {
     return value.Raised(digits);
 }