/// <summary> /// <para> /// Compare equality to another Mass 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(Mass 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)); }
public void MassCompareAutoTest() { UnitsNet.Mass A1 = new UnitsNet.Mass(65.743, UnitsNet.Units.MassUnit.Kilogram); EngineeringUnits.Mass A2 = new EngineeringUnits.Mass(65.743, EngineeringUnits.MassUnit.Kilogram); var EU11 = EngineeringUnits.MassUnit.List(); var UN11 = UnitsNet.Mass.Units; int DiffCount = 0; for (int i = 0; i < UnitsNet.Mass.Units.Length; i++) { if (UnitsNet.Mass.Units[i] == UnitsNet.Units.MassUnit.SolarMass) { DiffCount++; continue; } //Getting Units var EU = EngineeringUnits.MassUnit.List().ToList()[i - DiffCount]; var UN = UnitsNet.Mass.Units[i]; //All units absolute difference Assert.AreEqual(0, A2.As(EU) - A1.As(UN), 0.008); //All units relative difference Assert.AreEqual(0, HelperClass.Percent(A2.As(EU), A1.As(UN)), 1E-3); //All units symbol compare Assert.AreEqual(A2.ToUnit(EU).DisplaySymbol(), A1.ToUnit(UN).ToString("a")); } }
public void Masscompare() { UnitsNet.Mass L1 = new UnitsNet.Mass(5674.889, UnitsNet.Units.MassUnit.Kilogram); EngineeringUnits.Mass L2 = new EngineeringUnits.Mass(5674.889, EngineeringUnits.MassUnit.Kilogram); //UnitsNet has some small numerical-error that show off as big in small units like Nanometer Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Centigram) - L1.As(UnitsNet.Units.MassUnit.Centigram), 0); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Decigram) - L1.As(UnitsNet.Units.MassUnit.Decigram), 0); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Decagram) - L1.As(UnitsNet.Units.MassUnit.Decagram), 0); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.EarthMass) - L1.As(UnitsNet.Units.MassUnit.EarthMass), 9.5E-20); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Grain) - L1.As(UnitsNet.Units.MassUnit.Grain), 0); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Gram) - L1.As(UnitsNet.Units.MassUnit.Gram), 0); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Hectogram) - L1.As(UnitsNet.Units.MassUnit.Hectogram), 0); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Kilogram) - L1.As(UnitsNet.Units.MassUnit.Kilogram), 0); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Microgram) - L1.As(UnitsNet.Units.MassUnit.Microgram), 0); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Milligram) - L1.As(UnitsNet.Units.MassUnit.Milligram), 0); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Nanogram) - L1.As(UnitsNet.Units.MassUnit.Nanogram), 0); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Ounce) - L1.As(UnitsNet.Units.MassUnit.Ounce), 0.0003); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Pound) - L1.As(UnitsNet.Units.MassUnit.Pound), 1.9E-12); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.ShortTon) - L1.As(UnitsNet.Units.MassUnit.ShortTon), 0); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Stone) - L1.As(UnitsNet.Units.MassUnit.Stone), 0.0008); Assert.AreEqual(0, L2.As(EngineeringUnits.MassUnit.Tonne) - L1.As(UnitsNet.Units.MassUnit.Tonne), 0); //Assert.AreEqual(0, UnitsNet.Length.FromKilometers(435).Meters - EngineeringUnits.Length.FromKilometers(435).Meters, 0); }