Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="quantity"></param>
        /// <returns></returns>
        public static TimeSpan ToTimeSpan(this IQuantity quantity)
        {
            quantity.VerifyNotNull().VerifyTimeQuantity();

            var dimension = quantity.Dimensions.SingleOrDefault();

            // ReSharper disable once PossibleNullReferenceException
            if (dimension.Equals(T.Microsecond))
            {
                var millisecondQty = quantity.ConvertTo(T.Millisecond);
                millisecondQty.Value = Math.Min(millisecondQty.Value, TimeSpan.MaxValue.TotalMilliseconds - 1);
                millisecondQty.Value = Math.Max(millisecondQty.Value, TimeSpan.MinValue.TotalMilliseconds + 1);
                return(TimeSpan.FromMilliseconds(millisecondQty.Value));
            }

            if (dimension.Equals(T.Millisecond))
            {
                return(TimeSpan.FromMilliseconds(quantity.Value));
            }

            if (dimension.Equals(T.Second))
            {
                return(TimeSpan.FromSeconds(quantity.Value));
            }

            if (dimension.Equals(T.Minute))
            {
                return(TimeSpan.FromMinutes(quantity.Value));
            }

            if (dimension.Equals(T.Hour))
            {
                return(TimeSpan.FromHours(quantity.Value));
            }

            if (dimension.Equals(T.Day))
            {
                return(TimeSpan.FromDays(quantity.Value));
            }

            // ReSharper disable once ConvertIfStatementToReturnStatement
            if (dimension.Equals(T.Week))
            {
                return(TimeSpan.FromDays(quantity.ConvertTo(T.Day).Value));
            }

            return(TimeSpan.Zero);
        }
        /// <summary>
        /// Returns the cotangent of the specified <paramref name="angle"/>.
        /// </summary>
        /// <param name="angle"></param>
        /// <returns></returns>
        /// <see cref="SiTheta.Radian"/>
        /// <see cref="UsTheta.Degree"/>
        public static IQuantity Cot(this IQuantity angle)
        {
            const double max = 360d;

            angle.VerifyAngular();

            /* The operators are still a little bit of a work in progress, handling what to do
             * about converting to/from base approaching the core calculation. For purposes of
             * trig functions, however, we actually sometimes want these in terms of degrees,
             * not Radians, for purposes of evaluation. */

            var angleDegreesValue = angle.ConvertTo(UsTheta.Degree).Value;

            // This is likely not wholely correct, but it is close enough.
            if ((angleDegreesValue % max).Equals(0d))
            {
                return(new Quantity(double.PositiveInfinity));
            }

            if ((angleDegreesValue % max).Equals(180d))
            {
                return(new Quantity(double.NegativeInfinity));
            }

            var sine   = CalculateDimensionless(angle, Math.Sin);
            var cosine = (Quantity)CalculateDimensionless(angle, Math.Cos);

            return(cosine / sine);
        }
        //private static bool IsDegree(this IQuantity angle, double expectedAngle)
        //{
        //    angle.VerifyAngular();
        //    // TODO: might be better if we had an actual IEquatable<IQuantiy> here ...
        //    return angle.ConvertTo(UsTheta.Degree).Value.Equals(expectedAngle);
        //}

        private static IQuantity CalculateDimensionless(this IQuantity angle, Func <double, double> trigonometry)
        {
            angle.VerifyAngular();

            var theta = angle.ConvertTo(SiTheta.Radian);

            var result = new Quantity(trigonometry(theta.Value));

            return(result);
        }
            internal ConversionContext ConvertTo(double expectedValue, params IDimension[] units)
            {
                _converted = _original.ConvertTo(units);

                // We do not care about exponents in this instances, only the dimensions themselves.
                Assert.That(_original.Dimensions.AreCompatible(units, true));

                Assert.That(_converted.Value, Is.EqualTo(expectedValue).Within(_epsilon));

                VerifyUnitsNotSame(_converted, units);

                return(this);
            }
Example #5
0
        public void TestKelvinPerSecondConvertedToCe_per_s()
        {
            // 2013-09-05  From CodePlex User JuricaGrcic

            // Define Celsius per second - °C/s
            Unit Ce_per_s = SI.Ce.CombineDivide(SI.s);

            // Define Kelvin per second - K/s
            Unit Kelvin_per_s = SI.K.CombineDivide(SI.s);

            // Create value in units °C/s
            Quantity valueOfCelsiusPerSecond = new Quantity(2, Ce_per_s);
            //Console.WriteLine("Base value : {0}", valueOfCelsiusPerSecond);
            // prints 2 °C/s
            string valueOfCelsiusPerSecond_str          = valueOfCelsiusPerSecond.ToString();
            string valueOfCelsiusPerSecond_str_expected = "2 °C/s";

            // Convert °C/s to K/s
            IQuantity valueOfKelvinPerSecond = valueOfCelsiusPerSecond.ConvertTo(Kelvin_per_s);
            //Console.WriteLine("Base value converted to {0} : {1}", Ce_per_s, valueOfKelvinPerSecond);
            // prints 275.15 K/s - correct conversion or not??
            // 2013-10-29  Corrected to print 2 K/s
            string valueOfKelvinPerSecond_str          = valueOfKelvinPerSecond.ToString();
            string valueOfKelvinPerSecond_str_expected = "2 K/s";

            // Convert K/s back to °C/s
            IQuantity valueOfKelvinPerSecondConvertedToCe_per_s = valueOfKelvinPerSecond.ConvertTo(Ce_per_s);

            //Console.WriteLine("{0} converted back to {1}: {2}", Kelvin_per_s, Ce_per_s, valueOfKelvinPerSecond.ConvertTo(Ce_per_s));
            // prints 1.0036476381543 °C/s - should print 2 °C/s - incorrect conversion
            string valueOfKelvinPerSecondConvertedToCe_per_s_str          = valueOfKelvinPerSecondConvertedToCe_per_s.ToString();
            string valueOfKelvinPerSecondConvertedToCe_per_s_str_expected = "2 °C/s";

            Assert.AreEqual(valueOfCelsiusPerSecond, valueOfKelvinPerSecondConvertedToCe_per_s);

            Assert.AreEqual(valueOfCelsiusPerSecond_str, valueOfCelsiusPerSecond_str_expected);
            Assert.AreEqual(valueOfKelvinPerSecond_str, valueOfKelvinPerSecond_str_expected);
            Assert.AreEqual(valueOfKelvinPerSecondConvertedToCe_per_s_str, valueOfKelvinPerSecondConvertedToCe_per_s_str_expected);
        }
        /// <summary>
        /// Returns the tangent of the specified <paramref name="angle"/>.
        /// </summary>
        /// <param name="angle"></param>
        /// <returns></returns>
        /// <see cref="SiTheta.Radian"/>
        /// <see cref="UsTheta.Degree"/>
        /// <a href="!:http://msdn.microsoft.com/en-us/library/system.math.tan.aspx" />
        /// <a href="!:http://www.quora.com/Is-tan-90%C2%B0-equal-to-infinity" />
        public static IQuantity Tan(this IQuantity angle)
        {
            /* These are a couple of corner cases that the Math library does not take into
             * account, or returned an "undefined" number, which is technically incorrect.
             * http://www.uwgb.edu/dutchs/structge/sl01.htm */

            const double max = 360d;

            angle.VerifyAngular();

            var angleDegreesValue = angle.ConvertTo(UsTheta.Degree).Value;

            if ((angleDegreesValue % max).Equals(90d))
            {
                return(new Quantity(double.PositiveInfinity));
            }

            if ((angleDegreesValue % max).Equals(270d))
            {
                return(new Quantity(double.NegativeInfinity));
            }

            return(CalculateDimensionless(angle, Math.Tan));
        }
Example #7
0
 public IQuantity Plus(IQuantity other)
 {
     if(null == other) return this;
       return new Quantity(Amount + other.ConvertTo(Units).Amount, Units);
 }
Example #8
0
 public bool IsGreaterThan(IQuantity other)
 {
     return this.Amount > other.ConvertTo(this.Units).Amount;
 }