Skip to content

Relative and absolute equality #451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ce4c327
Adding ability to compare by relative and absolute values
tmilnthorp Jun 21, 2018
4272747
Fixing units and tests due to new relative tolerance
tmilnthorp Jun 21, 2018
877db67
Removing default parameters for windows runtime component
tmilnthorp Jun 21, 2018
35a8d4c
Fixing therm(UK) values
tmilnthorp Jun 21, 2018
74c3faa
Relative difference calculation should be against expected value. Rep…
tmilnthorp Jun 21, 2018
9fe2dbc
Removing power density prefixes for another day
tmilnthorp Jun 21, 2018
2ac0600
Adding documentation and parameter checks
tmilnthorp Jun 21, 2018
b2898c7
Allow tolerance to be 0 (exact)
tmilnthorp Jun 21, 2018
882a4d7
Make sure comparison is done to the current units of [this] so scalin…
tmilnthorp Jun 21, 2018
490e5af
Windows runtime component haunts me :). Fix for now allowing a parame…
tmilnthorp Jun 21, 2018
2a50b72
tmilnthorp Jun 22, 2018
241cabc
Making Comparison class static
tmilnthorp Jun 25, 2018
06164a3
Updating documentation. Call EqualsAbsolute from Equals($quantityName…
tmilnthorp Jun 25, 2018
94be718
Bit by decimal :)
tmilnthorp Jun 25, 2018
5c5db3c
Updating documentation to be more clear and provide examples for quan…
tmilnthorp Jun 25, 2018
62e3424
Updating documentation for clarity
tmilnthorp Jun 26, 2018
2071848
Fix for compiler warning
tmilnthorp Jun 26, 2018
760f17f
Obsolete Equals($quantityName other, $quantityName maxError). Fix Equ…
tmilnthorp Jun 26, 2018
fb04723
Merge remote-tracking branch 'upstream/master' into RelativeAndAbsolu…
tmilnthorp Jun 26, 2018
d35958c
Fix for WRC when value is always double.
tmilnthorp Jun 26, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions UnitsNet.Tests/AssertEx.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit;
using System;
using Xunit;

namespace UnitsNet.Tests
{
Expand All @@ -7,9 +8,22 @@ namespace UnitsNet.Tests
/// </summary>
public static class AssertEx
{
public static void EqualTolerance(double expected, double actual, double tolerance)
public static void EqualTolerance(double expected, double actual, double tolerance, ComparisonType comparisonType = ComparisonType.Relative)
{
Assert.True(actual >= expected - tolerance && actual <= expected + tolerance, $"Values are not equal within tolerance: {tolerance}\nExpected: {expected}\nActual: {actual}\nDiff: {actual - expected:e}");
if(comparisonType == ComparisonType.Relative)
{
bool areEqual = UnitsNet.Comparison.EqualsRelative(expected, actual, tolerance);

double difference = Math.Abs(expected - actual);
double relativeDifference = difference / expected;

Assert.True( areEqual, $"Values are not equal within relative tolerance: {tolerance:P4}\nExpected: {expected}\nActual: {actual}\nDiff: {relativeDifference:P4}" );
}
else if( comparisonType == ComparisonType.Absolute )
{
bool areEqual = UnitsNet.Comparison.EqualsAbsolute(expected, actual, tolerance);
Assert.True( areEqual, $"Values are not equal within absolute tolerance: {tolerance}\nExpected: {expected}\nActual: {actual}\nDiff: {actual - expected:e}" );
}
}
}
}
2 changes: 1 addition & 1 deletion UnitsNet.Tests/CustomCode/AccelerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class AccelerationTests : AccelerationTestsBase

protected override double NanometersPerSecondSquaredInOneMeterPerSecondSquared => 1E9;

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

protected override double InchesPerSecondSquaredInOneMeterPerSecondSquared => 39.3700787;

Expand Down
2 changes: 1 addition & 1 deletion UnitsNet.Tests/CustomCode/AngleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class AngleTests : AngleTestsBase

protected override double ArcsecondsInOneDegree => 3600.0;

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

[Fact]
public void AngleDividedByDurationEqualsRotationalSpeed()
Expand Down
10 changes: 5 additions & 5 deletions UnitsNet.Tests/CustomCode/DurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ public class DurationTests : DurationTestsBase

protected override double MinutesInOneSecond => 0.0166667;

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

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

protected override double NanosecondsInOneSecond => 1e+9;

protected override double SecondsInOneSecond => 1;

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

protected override double YearsInOneSecond => 3.1689e-8;
protected override double YearsInOneSecond => 3.170979198376458e-8;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value was different than Years365 and the average year instead of 365 days.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you saying it is supposed to be different? Both are defines in the json files as x36524*3600, so I am not sure how they could be?


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

[Fact]
public static void ToTimeSpanShouldThrowExceptionOnValuesLargerThanTimeSpanMax()
Expand Down
10 changes: 5 additions & 5 deletions UnitsNet.Tests/CustomCode/EnergyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace UnitsNet.Tests.CustomCode
public class EnergyTests : EnergyTestsBase
{
// TODO Override properties in base class here
protected override double ThermsImperialInOneJoule => 9.478171203551087813109937767482e-15;
protected override double ThermsImperialInOneJoule => 9.478171203551087813109937767482e-9;

protected override double JoulesInOneJoule => 1;

Expand All @@ -40,11 +40,11 @@ public class EnergyTests : EnergyTestsBase

protected override double CaloriesInOneJoule => 0.239005736;

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

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

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

protected override double ElectronVoltsInOneJoule => 6.241509343260179e18;

Expand All @@ -68,4 +68,4 @@ public class EnergyTests : EnergyTestsBase

protected override double WattHoursInOneJoule => 0.000277777778;
}
}
}
2 changes: 1 addition & 1 deletion UnitsNet.Tests/CustomCode/ForceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ForceTests : ForceTestsBase

protected override double PoundsForceInOneNewton => 0.22481;

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

[Fact]
public void ForceDividedByAreaEqualsPressure()
Expand Down
4 changes: 2 additions & 2 deletions UnitsNet.Tests/CustomCode/IrradiationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public class IrradiationTests : IrradiationTestsBase
{
protected override double JoulesPerSquareMeterInOneJoulePerSquareMeter => 1;

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

protected override double KilowattHoursPerSquareMeterInOneJoulePerSquareMeter => 2.78E-07;
protected override double KilowattHoursPerSquareMeterInOneJoulePerSquareMeter => 2.777777777777778e-7;
}
}
8 changes: 4 additions & 4 deletions UnitsNet.Tests/CustomCode/MassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public class MassTests : MassTestsBase

protected override double KilotonnesInOneKilogram => 1E-6;

protected override double LongTonsInOneKilogram => 0.000984207;
protected override double LongTonsInOneKilogram => 9.842065276110606e-4;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is getting a bit crowded and not so easy to review. Could you perhaps pull out the bits not directly related to absolute/relative functionality in a separate PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately all of these changes are so the tests pass. These validation numbers were off quite a bit when I changed the tests to validate by % tolerance (not precise enough).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in other words, they're unfortunately all related to absolute/relative functionality! Especially since I have switched the tests over to relative tolerance by default.

Without the mass tests change, mass tests fail with the following (MegatonnesInOneKilogram in this example):

Message: Values are not equal within relative tolerance: 0.0010%
Expected: 1E-06
Actual: 1E-09
Diff: 99.9000%

As you can see, it was a difference of 1e-6, which was inside the 1e-5 tolerance, but it was off by almost double!! 😲

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha 😆 it's so good to finally fix this!
I understand about the precision changes, no problem!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this getting ready now? I might find some time tomorrow to review.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! I'm done making changes.


protected override double MegapoundsInOneKilogram => 2.2046226218487757e-6;

protected override double MegatonnesInOneKilogram => 1E-6;
protected override double MegatonnesInOneKilogram => 1E-9;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow.. Just wow!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know!


protected override double MicrogramsInOneKilogram => 1E9;

Expand All @@ -60,7 +60,7 @@ public class MassTests : MassTestsBase

protected override double PoundsInOneKilogram => 2.2046226218487757d;

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

protected override double StoneInOneKilogram => 0.1574731728702698;

Expand Down Expand Up @@ -105,4 +105,4 @@ public void MassTimesAccelerationEqualsForce()
Assert.Equal(force, Force.FromNewtons(54));
}
}
}
}
18 changes: 9 additions & 9 deletions UnitsNet.Tests/CustomCode/PressureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public class PressureTests : PressureTestsBase

protected override double BarsInOnePascal => 1E-5;

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

protected override double KilogramsForcePerSquareMeterInOnePascal => 0.101971621;

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

protected override double KilonewtonsPerSquareCentimeterInOnePascal => 1e-7;

Expand All @@ -44,9 +44,9 @@ public class PressureTests : PressureTestsBase

protected override double KilopascalsInOnePascal => 1e-3;

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

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

protected override double MegapascalsInOnePascal => 1E-6;

Expand All @@ -64,17 +64,17 @@ public class PressureTests : PressureTestsBase

protected override double PoundsForcePerSquareFootInOnePascal => 0.0208854342;

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

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

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

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

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

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

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

Expand Down
18 changes: 9 additions & 9 deletions UnitsNet.Tests/CustomCode/SpecificWeightTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,37 @@ namespace UnitsNet.Tests.CustomCode
{
public class SpecificWeightTests : SpecificWeightTestsBase
{
protected override double KilogramsForcePerCubicCentimeterInOneNewtonPerCubicMeter => 0.101971621e-7;
protected override double KilogramsForcePerCubicCentimeterInOneNewtonPerCubicMeter => 1.019716212977928e-7;

protected override double KilogramsForcePerCubicMeterInOneNewtonPerCubicMeter => 0.101971621;

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

protected override double KilonewtonsPerCubicCentimeterInOneNewtonPerCubicMeter => 1e-9;

protected override double KilonewtonsPerCubicMeterInOneNewtonPerCubicMeter => 1e-3;

protected override double KilonewtonsPerCubicMillimeterInOneNewtonPerCubicMeter => 1e-12;

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

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

protected override double NewtonsPerCubicCentimeterInOneNewtonPerCubicMeter => 1e-6;

protected override double NewtonsPerCubicMeterInOneNewtonPerCubicMeter => 1;

protected override double NewtonsPerCubicMillimeterInOneNewtonPerCubicMeter => 1e-9;

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

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

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

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

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

protected override double MeganewtonsPerCubicMeterInOneNewtonPerCubicMeter => 1e-6;

Expand Down
10 changes: 5 additions & 5 deletions UnitsNet.Tests/CustomCode/TorqueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public class TorqueTests : TorqueTestsBase

protected override double KilonewtonMillimetersInOneNewtonMeter => 1;

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

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

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

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

protected override double NewtonCentimetersInOneNewtonMeter => 100;

Expand Down Expand Up @@ -81,4 +81,4 @@ public void TorqueDividedByLengthEqualsForce()
Assert.Equal(force, Force.FromNewtons(2));
}
}
}
}
2 changes: 1 addition & 1 deletion UnitsNet.Tests/CustomCode/VolumeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class VolumeTests : VolumeTestsBase

protected override double CubicMetersInOneCubicMeter => 1;

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

protected override double CubicMillimetersInOneCubicMeter => 1E9;

Expand Down
2 changes: 1 addition & 1 deletion UnitsNet.Tests/QuantityTests.ToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace UnitsNet.Tests
[Collection(nameof(UnitSystemFixture))]
public partial class QuantityTests
{
public class ToString
public class ToStringTests
{
[Fact]
public void CreatedByDefaultCtor_ReturnsValueInBaseUnit()
Expand Down
Loading