Exemplo n.º 1
0
        /// <summary>
        /// Returns a Unitconverter that will convert this unit to the other unit.
        /// </summary>
        /// <returns>The UnitConverter.</returns>
        /// <param name="other">The unit to build the converter to.</param>
        public UnitConverter GetConverterTo(Unit other)
        {
            if (Equals(other))
            {
                return(UnitConverter.IDENTITY);
            }

            Unit thisStandardUnit  = standardUnit;
            Unit otherStandardUnit = other.standardUnit;

            if (thisStandardUnit.Equals(otherStandardUnit))
            {
                return(other.ToStandardUnit().Inverse().Concatenate(ToStandardUnit()));
            }

            // TODO [email protected]: Clean this up
            if (!thisStandardUnit.IsCompatible(other))
            {
                throw new ArithmeticException("Cannot convert between " + this + " and " + other);
            }

            UnitConverter thisTransform  = ToStandardUnit().Concatenate(TransformOf(GetBaseUnits()));
            UnitConverter otherTransform = other.ToStandardUnit().Concatenate(TransformOf(other.GetBaseUnits()));

            return(otherTransform.Inverse().Concatenate(thisTransform));
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 4
0
 public override UnitConverter Inverse()
 {
     return(new ProductOfConvertersConverter(first.Inverse(), second.Inverse()));
 }