/// <summary>
        ///     <para>
        ///     Compare equality to another AmountOfSubstance within the given absolute or relative tolerance.
        ///     </para>
        ///     <para>
        ///     Relative tolerance is defined as the maximum allowable absolute difference between this quantity's value and
        ///     <paramref name="other"/> as a percentage of this quantity's value. <paramref name="other"/> will be converted into
        ///     this quantity's unit for comparison. A relative tolerance of 0.01 means the absolute difference must be within +/- 1% of
        ///     this quantity's value to be considered equal.
        ///     <example>
        ///     In this example, the two quantities will be equal if the value of b is within +/- 1% of a (0.02m or 2cm).
        ///     <code>
        ///     var a = Length.FromMeters(2.0);
        ///     var b = Length.FromInches(50.0);
        ///     a.Equals(b, 0.01, ComparisonType.Relative);
        ///     </code>
        ///     </example>
        ///     </para>
        ///     <para>
        ///     Absolute tolerance is defined as the maximum allowable absolute difference between this quantity's value and
        ///     <paramref name="other"/> as a fixed number in this quantity's unit. <paramref name="other"/> will be converted into
        ///     this quantity's unit for comparison.
        ///     <example>
        ///     In this example, the two quantities will be equal if the value of b is within 0.01 of a (0.01m or 1cm).
        ///     <code>
        ///     var a = Length.FromMeters(2.0);
        ///     var b = Length.FromInches(50.0);
        ///     a.Equals(b, 0.01, ComparisonType.Absolute);
        ///     </code>
        ///     </example>
        ///     </para>
        ///     <para>
        ///     Note that it is advised against specifying zero difference, due to the nature
        ///     of floating point operations and using System.Double internally.
        ///     </para>
        /// </summary>
        /// <param name="other">The other quantity to compare to.</param>
        /// <param name="tolerance">The absolute or relative tolerance value. Must be greater than or equal to 0.</param>
        /// <param name="comparisonType">The comparison type: either relative or absolute.</param>
        /// <returns>True if the absolute difference between the two values is not greater than the specified relative or absolute tolerance.</returns>
        public bool Equals(AmountOfSubstance other, double tolerance, ComparisonType comparisonType)
        {
            if (tolerance < 0)
            {
                throw new ArgumentOutOfRangeException("tolerance", "Tolerance must be greater than or equal to 0.");
            }

            double thisValue             = (double)this.Value;
            double otherValueInThisUnits = other.As(this.Unit);

            return(UnitsNet.Comparison.Equals(thisValue, otherValueInThisUnits, tolerance, comparisonType));
        }
Esempio n. 2
0
        public void AmountCompareAutoTest()
        {
            UnitsNet.AmountOfSubstance         A1 = new UnitsNet.AmountOfSubstance(65.743, UnitsNet.Units.AmountOfSubstanceUnit.Mole);
            EngineeringUnits.AmountOfSubstance A2 = new EngineeringUnits.AmountOfSubstance(65.743, EngineeringUnits.AmountOfSubstanceUnit.Mole);

            for (int i = 0; i < UnitsNet.AmountOfSubstance.Units.Length; i++)
            {
                //Getting Units
                var EU = EngineeringUnits.AmountOfSubstanceUnit.List().ToList()[i];
                var UN = UnitsNet.AmountOfSubstance.Units[i];

                //All units absolute difference
                Assert.AreEqual(0, A2.As(EU) - A1.As(UN), 1E-5);

                //All units relative difference
                Assert.AreEqual(0, HelperClass.Percent(A2.As(EU),
                                                       A1.As(UN)),
                                1E-10);
                //All units symbol compare
                Assert.AreEqual(A2.ToUnit(EU).DisplaySymbol(),
                                A1.ToUnit(UN).ToString("a"));
            }
        }
Esempio n. 3
0
        public void AmountCompare()
        {
            UnitsNet.AmountOfSubstance         A1 = new UnitsNet.AmountOfSubstance(65.743, UnitsNet.Units.AmountOfSubstanceUnit.Mole);
            EngineeringUnits.AmountOfSubstance A2 = new EngineeringUnits.AmountOfSubstance(65.743, EngineeringUnits.AmountOfSubstanceUnit.Mole);



            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.Centimole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Centimole), 1E-10);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.CentipoundMole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.CentipoundMole), 1E-10);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.Decimole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Decimole), 0);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.DecipoundMole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.DecipoundMole), 1E-10);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.Kilomole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Kilomole), 0);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.KilopoundMole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.KilopoundMole), 1E-10);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.Megamole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Megamole), 0);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.MicropoundMole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.MicropoundMole), 1E-10);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.Micromole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Micromole), 0);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.Millimole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Millimole), 1E-10);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.MillipoundMole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.MillipoundMole), 1E-10);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.Mole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Mole), 0);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.Nanomole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Nanomole), 7.7E-06);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.NanopoundMole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.NanopoundMole), 1E-7);
            Assert.AreEqual(0, A2.As(EngineeringUnits.AmountOfSubstanceUnit.PoundMole) - A1.As(UnitsNet.Units.AmountOfSubstanceUnit.PoundMole), 1E-10);



            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromCentimoles(435).Centimoles - EngineeringUnits.AmountOfSubstance.FromCentimoles(435).Centimoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromCentipoundMoles(435).CentipoundMoles - EngineeringUnits.AmountOfSubstance.FromCentipoundMoles(435).CentipoundMoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromDecimoles(435).Decimoles - EngineeringUnits.AmountOfSubstance.FromDecimoles(435).Decimoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromDecipoundMoles(435).DecipoundMoles - EngineeringUnits.AmountOfSubstance.FromDecipoundMoles(435).DecipoundMoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromKilomoles(435).Kilomoles - EngineeringUnits.AmountOfSubstance.FromKilomoles(435).Kilomoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromKilopoundMoles(435).KilopoundMoles - EngineeringUnits.AmountOfSubstance.FromKilopoundMoles(435).KilopoundMoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromMegamoles(435).Megamoles - EngineeringUnits.AmountOfSubstance.FromMegamoles(435).Megamoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromMicromoles(435).Micromoles - EngineeringUnits.AmountOfSubstance.FromMicromoles(435).Micromoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromMicropoundMoles(435).MicropoundMoles - EngineeringUnits.AmountOfSubstance.FromMicropoundMoles(435).MicropoundMoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromMillimoles(435).Millimoles - EngineeringUnits.AmountOfSubstance.FromMillimoles(435).Millimoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromMillipoundMoles(435).MillipoundMoles - EngineeringUnits.AmountOfSubstance.FromMillipoundMoles(435).MillipoundMoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromMoles(435).Moles - EngineeringUnits.AmountOfSubstance.FromMoles(435).Moles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromNanomoles(435).Nanomoles - EngineeringUnits.AmountOfSubstance.FromNanomoles(435).Nanomoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromNanopoundMoles(435).NanopoundMoles - EngineeringUnits.AmountOfSubstance.FromNanopoundMoles(435).NanopoundMoles, 0);
            Assert.AreEqual(0, UnitsNet.AmountOfSubstance.FromPoundMoles(435).PoundMoles - EngineeringUnits.AmountOfSubstance.FromPoundMoles(435).PoundMoles, 0);


            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.Centimole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Centimole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.CentipoundMole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.CentipoundMole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.Decimole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Decimole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.DecipoundMole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.DecipoundMole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.Kilomole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Kilomole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.KilopoundMole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.KilopoundMole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.Megamole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Megamole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.Micromole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Micromole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.MicropoundMole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.MicropoundMole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.Millimole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Millimole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.MillipoundMole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.MillipoundMole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.Mole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Mole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.Nanomole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.Nanomole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.NanopoundMole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.NanopoundMole)), 1E-10);
            Assert.AreEqual(0, HelperClass.Percent(A2.As(EngineeringUnits.AmountOfSubstanceUnit.PoundMole),
                                                   A1.As(UnitsNet.Units.AmountOfSubstanceUnit.PoundMole)), 1E-10);



            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.Centimole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.Centimole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.CentipoundMole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.CentipoundMole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.Decimole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.Decimole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.DecipoundMole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.DecipoundMole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.Kilomole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.Kilomole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.KilopoundMole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.KilopoundMole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.Megamole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.Megamole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.Micromole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.Micromole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.MicropoundMole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.MicropoundMole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.Millimole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.Millimole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.MillipoundMole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.MillipoundMole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.Mole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.Mole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.Nanomole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.Nanomole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.NanopoundMole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.NanopoundMole).ToString("a"));
            Assert.AreEqual(A2.ToUnit(EngineeringUnits.AmountOfSubstanceUnit.PoundMole).DisplaySymbol(),
                            A1.ToUnit(UnitsNet.Units.AmountOfSubstanceUnit.PoundMole).ToString("a"));
        }