示例#1
0
        public override UnitConverter Concatenate(UnitConverter converter)
        {
            if (converter is AdditionOfConvertersConverter)
            {
                var ac = converter as AdditionOfConvertersConverter;
                var f  = first.Concatenate(ac.first);
                var s  = second.Concatenate(ac.second);

                if (f.Concatenate(s).Equals(IDENTITY))
                {
                    return(new ConstantConverter(0));
                }
                else if (f.Equals(IDENTITY) && s.Equals(IDENTITY))
                {
                    return(new ConstantConverter(0));
                }
                else if (f.Equals(IDENTITY))
                {
                    return(s);
                }
                else if (s.Equals(IDENTITY))
                {
                    return(f);
                }
                else
                {
                    return(new AdditionOfConvertersConverter(f, s));
                }
            }
            else
            {
                return(base.Concatenate(converter));
            }
        }
示例#2
0
        public override UnitConverter ToStandardUnit()
        {
            UnitConverter ret = UnitConverter.IDENTITY;

            if (HasOnlyStandardUnit())
            {
                return(ret);
            }

            for (int i = 0; i < elements.Length; i++)
            {
                Element       e  = elements[i];
                UnitConverter uc = e.unit.ToStandardUnit();

                if (!uc.isLinear)
                {
                    throw new ArithmeticException("Cannot convert: " + e.unit + " is not linear");
                }

                if (e.root != 1)
                {
                    throw new ArithmeticException("Cannot convert: " + e.unit + " holds a base unit with fractional exponent");
                }

                int pow = e.pow;

                if (pow < 0)
                {
                    pow = -pow;
                    uc  = uc.Inverse();
                }

                for (int j = 0; j < pow; j++)
                {
                    ret = ret.Concatenate(uc);
                }
            }

            return(ret);
        }
示例#3
0
        private static UnitConverter TransformOf(Unit unit)
        {
            if (unit is BaseUnit)
            {
                return(UnitConverter.IDENTITY);
            }

            ProductUnit   productUnit = (ProductUnit)unit;
            UnitConverter ret         = UnitConverter.IDENTITY;

            for (int i = 0; i < productUnit.unitCount; i++)
            {
                Unit          u         = productUnit.GetUnit(i);
                UnitConverter converter = TransformOf(u);

                if (!converter.isLinear)
                {
                    throw new ArithmeticException("Cannot convert: " + unit + " is non-linear.");
                }

                if (productUnit.GetUnitRoot(i) != 1)
                {
                    throw new ArithmeticException("Cannot convert: " + productUnit + " holds a base unit with a fractional exponent.");
                }

                int pow = productUnit.GetUnitPow(i);
                if (pow < 0)
                {
                    pow       = -pow;
                    converter = converter.Inverse();
                }

                for (int j = 0; j < pow; j++)
                {
                    ret = ret.Concatenate(converter);
                }
            }

            return(ret);
        }
示例#4
0
 public override UnitConverter Concatenate(UnitConverter converter)
 {
     if (converter is RationalConverter)
     {
         RationalConverter rc  = (RationalConverter)converter;
         long   dividendLong   = dividend * rc.dividend;
         long   divisorLong    = divisor * rc.divisor;
         double dividendDouble = ((double)dividend) * rc.dividend;
         double divisorDouble  = ((double)divisor) * rc.divisor;
         if ((dividendLong != dividendDouble) || (divisorLong != divisorDouble))
         {
             return(new MultiplyConverter(dividendDouble / divisorDouble));
         }
         long gcd = Gcd(dividendLong, divisorLong);
         return(ValueOf(dividendLong / gcd, divisorLong / gcd));
     }
     else if (converter is MultiplyConverter)
     {
         return(converter.Concatenate(this));
     }
     else if (converter is ConstantConverter)
     {
         var factor = dividend * ((ConstantConverter)converter).constant / dividend;
         if (factor == 0)
         {
             return(new ConstantConverter(0));
         }
         else
         {
             return(new MultiplyConverter(factor));
         }
     }
     else
     {
         return(base.Concatenate(converter));
     }
 }