Ejemplo n.º 1
0
        private void Normalize()
        {
            var power10   = PrefixHelpers.Power10(Quantity);
            var newPrefix = PrefixHelpers.FindClosestPrefix(power10 / Power);

            if (newPrefix != 0)
            {
                Prefix = newPrefix;
                var pow = -((int)newPrefix * Power);
                Quantity *= Math.Pow(10, pow);
            }
        }
Ejemplo n.º 2
0
 public static Unit operator -(Unit u1, Unit u2)
 {
     if (u1.IsComparable(u2))
     {
         var averagePrefix = PrefixHelpers.AveragePrefix(u1.Prefix, u2.Prefix);
         var quantity      = u1.Quantity * Math.Pow(10, u1.Power10Difference(averagePrefix)) - u2.Quantity * Math.Pow(10, u2.Power10Difference(averagePrefix));
         var newUnit       = new Unit(quantity, averagePrefix, u1.BaseUnit, u1.Power);
         return(newUnit);
     }
     else
     {
         throw new GrandmothersAndFrogsException(u1, u2, "You can't mix them. You just can't");
     }
 }
Ejemplo n.º 3
0
 public static Unit operator *(Unit u1, Unit u2)
 {
     if (u1.BaseUnit == u2.BaseUnit && u1.BaseUnit != 0)
     {
         var averagePrefix = PrefixHelpers.AveragePrefix(u1.Prefix, u2.Prefix);
         var power10       = u1.Power10Difference(averagePrefix) + u2.Power10Difference(averagePrefix);
         var quantity      = u1.Quantity * u2.Quantity * Math.Pow(10, power10);
         var newUnit       = new Unit(quantity, averagePrefix, u1.BaseUnit, u1.Power + u2.Power);
         return(newUnit);
     }
     else
     {
         return(Unit.Multiply(u1, u2));
     }
 }
Ejemplo n.º 4
0
        private IDictionary <Unit, Unit> FindPossibleDerivedUnits()
        {
            var dict = new ConcurrentDictionary <Unit, Unit>();

            DerivedUnits.Where(x => x.Units.Count <= Units.Count).AsParallel().ForAll(derivedUnit =>
            {
                int factor = this.HasFactor(derivedUnit);

                if (factor != 0)
                {
                    Unit remain  = this / derivedUnit.Pow(factor);
                    var pow10    = PrefixHelpers.Power10(remain.Quantity);
                    var prfx     = PrefixHelpers.FindClosestPrefix(pow10 / factor);
                    var units    = derivedUnit.Units.SelectMany(x => x.Pow(1)).ToArray();
                    var quantity = remain.Quantity / Math.Pow(10, (int)prfx * factor);
                    Unit d       = new Unit(quantity, prfx, derivedUnit.UnitName, factor, units);
                    dict.AddOrUpdate(d, remain, (x, y) => y);
                }
            });
            return(dict);
        }