Skip to content

Commit 0702d1c

Browse files
tmilnthorpangularsen
authored andcommitted
Relative and absolute equality (#451)
This introduces relative equality checks, which makes it easier to do equality checks without knowing the values or units in advance. It also improves the existing absolute equality checks to more intuitively use `this` as the reference value/unit instead of the base unit. * Add `static Comparison` type for absolute and relative equality logic, that take a tolerance and a `ComparisonType` * Add new overload for instance method `Equals()` on quantity types * Adding ability to compare by relative and absolute values * Fixing units and tests due to new relative tolerance * Removing default parameters for windows runtime component * Fixing therm(UK) values * Relative difference calculation should be against expected value * Make sure comparison is done to the current units of [this] to avoid uneccessary scaling * Fix obsolete message of Equals($quantityName other, $quantityName maxError). * Fix Equals methods that were scaling to base units when unnecessary
1 parent eb4bb02 commit 0702d1c

File tree

113 files changed

+5700
-886
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+5700
-886
lines changed

UnitsNet.Tests/AssertEx.cs

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Xunit;
1+
using System;
2+
using Xunit;
23

34
namespace UnitsNet.Tests
45
{
@@ -7,9 +8,22 @@ namespace UnitsNet.Tests
78
/// </summary>
89
public static class AssertEx
910
{
10-
public static void EqualTolerance(double expected, double actual, double tolerance)
11+
public static void EqualTolerance(double expected, double actual, double tolerance, ComparisonType comparisonType = ComparisonType.Relative)
1112
{
12-
Assert.True(actual >= expected - tolerance && actual <= expected + tolerance, $"Values are not equal within tolerance: {tolerance}\nExpected: {expected}\nActual: {actual}\nDiff: {actual - expected:e}");
13+
if(comparisonType == ComparisonType.Relative)
14+
{
15+
bool areEqual = UnitsNet.Comparison.EqualsRelative(expected, actual, tolerance);
16+
17+
double difference = Math.Abs(expected - actual);
18+
double relativeDifference = difference / expected;
19+
20+
Assert.True( areEqual, $"Values are not equal within relative tolerance: {tolerance:P4}\nExpected: {expected}\nActual: {actual}\nDiff: {relativeDifference:P4}" );
21+
}
22+
else if( comparisonType == ComparisonType.Absolute )
23+
{
24+
bool areEqual = UnitsNet.Comparison.EqualsAbsolute(expected, actual, tolerance);
25+
Assert.True( areEqual, $"Values are not equal within absolute tolerance: {tolerance}\nExpected: {expected}\nActual: {actual}\nDiff: {actual - expected:e}" );
26+
}
1327
}
1428
}
1529
}

UnitsNet.Tests/CustomCode/AccelerationTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class AccelerationTests : AccelerationTestsBase
3939

4040
protected override double NanometersPerSecondSquaredInOneMeterPerSecondSquared => 1E9;
4141

42-
protected override double StandardGravityInOneMeterPerSecondSquared => 0.1019727;
42+
protected override double StandardGravityInOneMeterPerSecondSquared => 1.019716212977928e-1;
4343

4444
protected override double InchesPerSecondSquaredInOneMeterPerSecondSquared => 39.3700787;
4545

UnitsNet.Tests/CustomCode/AngleTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class AngleTests : AngleTestsBase
5252

5353
protected override double ArcsecondsInOneDegree => 3600.0;
5454

55-
protected override double RevolutionsInOneDegree => 2.77E-3;
55+
protected override double RevolutionsInOneDegree => 2.777777777777777e-3;
5656

5757
[Fact]
5858
public void AngleDividedByDurationEqualsRotationalSpeed()

UnitsNet.Tests/CustomCode/DurationTests.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ public class DurationTests : DurationTestsBase
3636

3737
protected override double MinutesInOneSecond => 0.0166667;
3838

39-
protected override double MonthsInOneSecond => 3.8027e-7;
39+
protected override double MonthsInOneSecond => 3.858024691358024e-7;
4040

41-
protected override double Months30InOneSecond => 3.85802469135e-7;
41+
protected override double Months30InOneSecond => 3.858024691358024e-7;
4242

4343
protected override double NanosecondsInOneSecond => 1e+9;
4444

4545
protected override double SecondsInOneSecond => 1;
4646

47-
protected override double WeeksInOneSecond => 1.6534e-6;
47+
protected override double WeeksInOneSecond => 1.653439153439153e-6;
4848

49-
protected override double YearsInOneSecond => 3.1689e-8;
49+
protected override double YearsInOneSecond => 3.170979198376458e-8;
5050

51-
protected override double Years365InOneSecond => 3.170979198376e-8;
51+
protected override double Years365InOneSecond => 3.170979198376458e-8;
5252

5353
[Fact]
5454
public static void ToTimeSpanShouldThrowExceptionOnValuesLargerThanTimeSpanMax()

UnitsNet.Tests/CustomCode/EnergyTests.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace UnitsNet.Tests.CustomCode
2424
public class EnergyTests : EnergyTestsBase
2525
{
2626
// TODO Override properties in base class here
27-
protected override double ThermsImperialInOneJoule => 9.478171203551087813109937767482e-15;
27+
protected override double ThermsImperialInOneJoule => 9.478171203551087813109937767482e-9;
2828

2929
protected override double JoulesInOneJoule => 1;
3030

@@ -40,11 +40,11 @@ public class EnergyTests : EnergyTestsBase
4040

4141
protected override double CaloriesInOneJoule => 0.239005736;
4242

43-
protected override double DecathermsEcInOneJoule => 9.47816988e-10;
43+
protected override double DecathermsEcInOneJoule => 9.478171203133172e-10;
4444

45-
protected override double DecathermsImperialInOneJoule => 9.478171203551087813109937767482e-15;
45+
protected override double DecathermsImperialInOneJoule => 9.478171203551087813109937767482e-10;
4646

47-
protected override double DecathermsUsInOneJoule => 9.4804342797334860315281322406817e-10;
47+
protected override double DecathermsUsInOneJoule => 9.480434279733486e-10;
4848

4949
protected override double ElectronVoltsInOneJoule => 6.241509343260179e18;
5050

@@ -68,4 +68,4 @@ public class EnergyTests : EnergyTestsBase
6868

6969
protected override double WattHoursInOneJoule => 0.000277777778;
7070
}
71-
}
71+
}

UnitsNet.Tests/CustomCode/ForceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class ForceTests : ForceTestsBase
4141

4242
protected override double PoundsForceInOneNewton => 0.22481;
4343

44-
protected override double TonnesForceInOneNewton => 1.02e-4;
44+
protected override double TonnesForceInOneNewton => 1.019716212977928e-4;
4545

4646
[Fact]
4747
public void ForceDividedByAreaEqualsPressure()

UnitsNet.Tests/CustomCode/IrradiationTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public class IrradiationTests : IrradiationTestsBase
4848
{
4949
protected override double JoulesPerSquareMeterInOneJoulePerSquareMeter => 1;
5050

51-
protected override double WattHoursPerSquareMeterInOneJoulePerSquareMeter => 2.78E-04;
51+
protected override double WattHoursPerSquareMeterInOneJoulePerSquareMeter => 2.777777777777778e-4;
5252

53-
protected override double KilowattHoursPerSquareMeterInOneJoulePerSquareMeter => 2.78E-07;
53+
protected override double KilowattHoursPerSquareMeterInOneJoulePerSquareMeter => 2.777777777777778e-7;
5454
}
5555
}

UnitsNet.Tests/CustomCode/MassTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public class MassTests : MassTestsBase
4242

4343
protected override double KilotonnesInOneKilogram => 1E-6;
4444

45-
protected override double LongTonsInOneKilogram => 0.000984207;
45+
protected override double LongTonsInOneKilogram => 9.842065276110606e-4;
4646

4747
protected override double MegapoundsInOneKilogram => 2.2046226218487757e-6;
4848

49-
protected override double MegatonnesInOneKilogram => 1E-6;
49+
protected override double MegatonnesInOneKilogram => 1E-9;
5050

5151
protected override double MicrogramsInOneKilogram => 1E9;
5252

@@ -60,7 +60,7 @@ public class MassTests : MassTestsBase
6060

6161
protected override double PoundsInOneKilogram => 2.2046226218487757d;
6262

63-
protected override double ShortTonsInOneKilogram => 0.00110231;
63+
protected override double ShortTonsInOneKilogram => 1.102311310924388e-3;
6464

6565
protected override double StoneInOneKilogram => 0.1574731728702698;
6666

@@ -105,4 +105,4 @@ public void MassTimesAccelerationEqualsForce()
105105
Assert.Equal(force, Force.FromNewtons(54));
106106
}
107107
}
108-
}
108+
}

UnitsNet.Tests/CustomCode/PressureTests.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public class PressureTests : PressureTestsBase
3030

3131
protected override double BarsInOnePascal => 1E-5;
3232

33-
protected override double KilogramsForcePerSquareCentimeterInOnePascal => 0.101971621e-5;
33+
protected override double KilogramsForcePerSquareCentimeterInOnePascal => 1.019716212977928e-5;
3434

3535
protected override double KilogramsForcePerSquareMeterInOnePascal => 0.101971621;
3636

37-
protected override double KilogramsForcePerSquareMillimeterInOnePascal => 0.101971621e-7;
37+
protected override double KilogramsForcePerSquareMillimeterInOnePascal => 1.019716212977928e-7;
3838

3939
protected override double KilonewtonsPerSquareCentimeterInOnePascal => 1e-7;
4040

@@ -44,9 +44,9 @@ public class PressureTests : PressureTestsBase
4444

4545
protected override double KilopascalsInOnePascal => 1e-3;
4646

47-
protected override double KilopoundsForcePerSquareFootInOnePascal => 2.089e-5;
47+
protected override double KilopoundsForcePerSquareFootInOnePascal => 2.088543423315013e-5;
4848

49-
protected override double KilopoundsForcePerSquareInchInOnePascal => 1.45e-7;
49+
protected override double KilopoundsForcePerSquareInchInOnePascal => 1.450377377302092e-7;
5050

5151
protected override double MegapascalsInOnePascal => 1E-6;
5252

@@ -64,17 +64,17 @@ public class PressureTests : PressureTestsBase
6464

6565
protected override double PoundsForcePerSquareFootInOnePascal => 0.0208854342;
6666

67-
protected override double PoundsForcePerSquareInchInOnePascal => 0.000145037737730209;
67+
protected override double PoundsForcePerSquareInchInOnePascal => 1.450377377302092e-4;
6868

69-
protected override double PsiInOnePascal => 1.450377*1E-4;
69+
protected override double PsiInOnePascal => 1.450377377302092e-4;
7070

7171
protected override double TechnicalAtmospheresInOnePascal => 1.0197*1E-5;
7272

73-
protected override double TonnesForcePerSquareCentimeterInOnePascal => 1e-8;
73+
protected override double TonnesForcePerSquareCentimeterInOnePascal => 1.019716212977928e-8;
7474

75-
protected override double TonnesForcePerSquareMeterInOnePascal => 1e-4;
75+
protected override double TonnesForcePerSquareMeterInOnePascal => 1.019716212977928e-4;
7676

77-
protected override double TonnesForcePerSquareMillimeterInOnePascal => 1e-10;
77+
protected override double TonnesForcePerSquareMillimeterInOnePascal => 1.019716212977928e-10;
7878

7979
protected override double TorrsInOnePascal => 7.5006*1E-3;
8080

UnitsNet.Tests/CustomCode/SpecificWeightTests.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,37 @@ namespace UnitsNet.Tests.CustomCode
2525
{
2626
public class SpecificWeightTests : SpecificWeightTestsBase
2727
{
28-
protected override double KilogramsForcePerCubicCentimeterInOneNewtonPerCubicMeter => 0.101971621e-7;
28+
protected override double KilogramsForcePerCubicCentimeterInOneNewtonPerCubicMeter => 1.019716212977928e-7;
2929

3030
protected override double KilogramsForcePerCubicMeterInOneNewtonPerCubicMeter => 0.101971621;
3131

32-
protected override double KilogramsForcePerCubicMillimeterInOneNewtonPerCubicMeter => 0.101971621e-10;
32+
protected override double KilogramsForcePerCubicMillimeterInOneNewtonPerCubicMeter => 1.019716212977928e-10;
3333

3434
protected override double KilonewtonsPerCubicCentimeterInOneNewtonPerCubicMeter => 1e-9;
3535

3636
protected override double KilonewtonsPerCubicMeterInOneNewtonPerCubicMeter => 1e-3;
3737

3838
protected override double KilonewtonsPerCubicMillimeterInOneNewtonPerCubicMeter => 1e-12;
3939

40-
protected override double KilopoundsForcePerCubicFootInOneNewtonPerCubicMeter => 6.366e-6;
40+
protected override double KilopoundsForcePerCubicFootInOneNewtonPerCubicMeter => 6.365880354264159e-6;
4141

42-
protected override double KilopoundsForcePerCubicInchInOneNewtonPerCubicMeter => 3.684e-9;
42+
protected override double KilopoundsForcePerCubicInchInOneNewtonPerCubicMeter => 3.683958538347314e-9;
4343

4444
protected override double NewtonsPerCubicCentimeterInOneNewtonPerCubicMeter => 1e-6;
4545

4646
protected override double NewtonsPerCubicMeterInOneNewtonPerCubicMeter => 1;
4747

4848
protected override double NewtonsPerCubicMillimeterInOneNewtonPerCubicMeter => 1e-9;
4949

50-
protected override double PoundsForcePerCubicFootInOneNewtonPerCubicMeter => 0.006366;
50+
protected override double PoundsForcePerCubicFootInOneNewtonPerCubicMeter => 6.365880354264159e-3;
5151

52-
protected override double PoundsForcePerCubicInchInOneNewtonPerCubicMeter => 3.684e-6;
52+
protected override double PoundsForcePerCubicInchInOneNewtonPerCubicMeter => 3.683958538347314e-6;
5353

54-
protected override double TonnesForcePerCubicCentimeterInOneNewtonPerCubicMeter => 1.02e-13;
54+
protected override double TonnesForcePerCubicCentimeterInOneNewtonPerCubicMeter => 1.019716212977928e-10;
5555

56-
protected override double TonnesForcePerCubicMeterInOneNewtonPerCubicMeter => 1.02e-4;
56+
protected override double TonnesForcePerCubicMeterInOneNewtonPerCubicMeter => 1.019716212977928e-4;
5757

58-
protected override double TonnesForcePerCubicMillimeterInOneNewtonPerCubicMeter => 1.02e-10;
58+
protected override double TonnesForcePerCubicMillimeterInOneNewtonPerCubicMeter => 1.019716212977928e-13;
5959

6060
protected override double MeganewtonsPerCubicMeterInOneNewtonPerCubicMeter => 1e-6;
6161

UnitsNet.Tests/CustomCode/TorqueTests.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ public class TorqueTests : TorqueTestsBase
4343

4444
protected override double KilonewtonMillimetersInOneNewtonMeter => 1;
4545

46-
protected override double MegapoundForceFeetInOneNewtonMeter => 7.376e-7;
46+
protected override double MegapoundForceFeetInOneNewtonMeter => 7.375621492772654e-7;
4747

48-
protected override double KilopoundForceFeetInOneNewtonMeter => 7.376e-4;
48+
protected override double KilopoundForceFeetInOneNewtonMeter => 7.375621492772654e-4;
4949

50-
protected override double MegapoundForceInchesInOneNewtonMeter => 8.8516E-6;
50+
protected override double MegapoundForceInchesInOneNewtonMeter => 8.850745791327184e-6;
5151

52-
protected override double KilopoundForceInchesInOneNewtonMeter => 8.8516E-3;
52+
protected override double KilopoundForceInchesInOneNewtonMeter => 8.850745791327184e-3;
5353

5454
protected override double NewtonCentimetersInOneNewtonMeter => 100;
5555

@@ -81,4 +81,4 @@ public void TorqueDividedByLengthEqualsForce()
8181
Assert.Equal(force, Force.FromNewtons(2));
8282
}
8383
}
84-
}
84+
}

UnitsNet.Tests/CustomCode/VolumeTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class VolumeTests : VolumeTestsBase
4040

4141
protected override double CubicMetersInOneCubicMeter => 1;
4242

43-
protected override double CubicMilesInOneCubicMeter => 3.86102*1E-7;
43+
protected override double CubicMilesInOneCubicMeter => 2.399127585789277e-10;
4444

4545
protected override double CubicMillimetersInOneCubicMeter => 1E9;
4646

UnitsNet.Tests/QuantityTests.ToString.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace UnitsNet.Tests
2828
[Collection(nameof(UnitSystemFixture))]
2929
public partial class QuantityTests
3030
{
31-
public class ToString
31+
public class ToStringTests
3232
{
3333
[Fact]
3434
public void CreatedByDefaultCtor_ReturnsValueInBaseUnit()

0 commit comments

Comments
 (0)