/// <summary>
        /// Adding quantities with different storage types
        /// </summary>
        /// <typeparam name="Q">Second Quantity Type</typeparam>
        /// <param name="firstQuantity"></param>
        /// <param name="secondQuantity"></param>
        /// <returns></returns>
        public static AnyQuantity <T> Add <Q>(AnyQuantity <T> firstQuantity, AnyQuantity <Q> secondQuantity)
        {
            if (firstQuantity.Equals(secondQuantity))
            {
                AnyQuantity <T> AQ = null;
                try
                {
                    AQ = QuantityDimension.QuantityFrom <T>(firstQuantity.Dimension);
                    //exception happen when adding two derived quantities together
                }
                catch (QuantityNotFoundException)
                {
                    //keep the first quantity configuration.
                    AQ = (AnyQuantity <T>)firstQuantity.Clone();
                }


                T firstVal = (firstQuantity.Value);

                Q secondVal = (secondQuantity.Value);

                //correct the values according to left unit or first unit.
                //the resulted quantity has the values of the first unit.

                if (firstQuantity.Unit != null && secondQuantity.Unit != null)
                {
                    //factor from second unit to first unit
                    UnitPathStack stof = secondQuantity.Unit.PathToUnit(firstQuantity.Unit);

                    secondVal = MultiplyScalarByGeneric <Q>(stof.ConversionFactor, secondVal);
                }


                var expr = Expression.Add(Expression.Constant(firstVal), Expression.Constant(secondVal));

                // Construct Lambda function which return one object.
                Expression <Func <T> > cq = Expression.Lambda <Func <T> >(expr);

                // compile the function
                Func <T> aqf = cq.Compile();

                // execute the function
                T result = aqf();

                if (firstQuantity.Unit != null && secondQuantity.Unit != null)
                {
                    //assign the unit of first quantity to the result.
                    AQ.Unit = firstQuantity.Unit;
                }


                AQ.Value = result;

                return(AQ);
            }
            else
            {
                throw new QuantitiesNotDimensionallyEqualException();
            }
        }
        public static AnyQuantity <T> Mod(AnyQuantity <T> firstQuantity, AnyQuantity <T> secondQuantity)
        {
            AnyQuantity <T> qresult = (AnyQuantity <T>)firstQuantity.Clone();


            if (typeof(T) == typeof(decimal) || typeof(T) == typeof(double) || typeof(T) == typeof(float) || typeof(T) == typeof(int) || typeof(T) == typeof(short))
            {
                qresult.Value = (T)(object)(((double)(object)firstQuantity.Value) % ((double)(object)secondQuantity.Value));
            }
            else
            {
                qresult.Value = ModuloGenericByGeneric(firstQuantity.Value, secondQuantity.Value);
            }

            return(qresult);
        }
        public static AnyQuantity <T> Subtract(AnyQuantity <T> firstQuantity, AnyQuantity <T> secondQuantity)
        {
            if (firstQuantity.Equals(secondQuantity))
            {
                AnyQuantity <T> AQ = null;
                try
                {
                    AQ = QuantityDimension.QuantityFrom <T>(firstQuantity.Dimension);

                    //exception happen when adding two derived quantities together
                }
                catch (QuantityNotFoundException)
                {
                    //keep the first quantity configuration.
                    AQ = (AnyQuantity <T>)firstQuantity.Clone();
                }


                if (typeof(T) == typeof(decimal) || typeof(T) == typeof(double) || typeof(T) == typeof(float) || typeof(T) == typeof(int) || typeof(T) == typeof(short))
                {
                    //use direct calculations

                    double firstVal = (double)(object)firstQuantity.Value;

                    double secondVal = (double)(object)secondQuantity.Value;

                    //correct the values according to left unit or first unit.
                    //the resulted quantity has the values of the first unit.

                    if (firstQuantity.Unit != null && secondQuantity.Unit != null)
                    {
                        //factor from second unit to first unit
                        UnitPathStack stof = secondQuantity.Unit.PathToUnit(firstQuantity.Unit);

                        secondVal = stof.ConversionFactor * secondVal;
                    }



                    ////sum the values

                    double result = firstVal - secondVal;

                    if (firstQuantity.Unit != null && secondQuantity.Unit != null)
                    {
                        //assign the unit of first quantity to the result.
                        AQ.Unit = (Unit)firstQuantity.Unit.Clone();
                    }


                    AQ.Value = (T)(object)result;

                    return(AQ);
                }
                else
                {
                    T firstVal  = (firstQuantity.Value);
                    T secondVal = (secondQuantity.Value);

                    //correct the values according to left unit or first unit.
                    //the resulted quantity has the values of the first unit.

                    if (firstQuantity.Unit != null && secondQuantity.Unit != null)
                    {
                        //factor from second unit to first unit
                        UnitPathStack stof = secondQuantity.Unit.PathToUnit(firstQuantity.Unit);

                        secondVal = MultiplyScalarByGeneric(stof.ConversionFactor, secondVal);
                    }


                    var expr = Expression.Subtract(Expression.Constant(firstVal), Expression.Constant(secondVal));

                    // Construct Lambda function which return one object.
                    Expression <Func <T> > cq = Expression.Lambda <Func <T> >(expr);

                    // compile the function
                    Func <T> aqf = cq.Compile();

                    // execute the function
                    T result = aqf();


                    if (firstQuantity.Unit != null && secondQuantity.Unit != null)
                    {
                        //assign the unit of first quantity to the result.
                        AQ.Unit = firstQuantity.Unit;
                    }

                    AQ.Value = result;

                    return(AQ);
                }
            }
            else
            {
                throw new QuantitiesNotDimensionallyEqualException();
            }
        }