You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ref #1193
In v5, the default equality implementation changed to strict equality
and the existing methods to compare across units with a tolerance, but
this was not available in `IQuantity` interfaces.
### Changes
- Add `Equals(IQuantity? other, IQuantity tolerance)` to `IQuantity`
- Add `Equals(TSelf? other, TSelf tolerance)` to `IQuantity<TSelf,
TUnitType, TValueType>` for strongly typed comparisons
- Obsolete `Equals(TQuantity other, double tolerance, ComparisonType
comparisonType)` method in quantity types
/// <summary>Indicates strict equality of two <see cref=""{_quantity.Name}""/> quantities, where both <see cref=""Value"" /> and <see cref=""Unit"" /> are exactly equal.</summary>
752
-
/// <remarks>Consider using <see cref=""Equals({_quantity.Name}, {_valueType}, ComparisonType)""/> to check equality across different units and to specify a floating-point number error tolerance.</remarks>
753
-
[Obsolete(""For null checks, use `x is null` syntax to not invoke overloads. For quantity comparisons, use Equals({_quantity.Name}, {_valueType}, ComparisonType) to check equality across different units and to specify a floating-point number error tolerance."")]
752
+
[Obsolete(""For null checks, use `x is null` syntax to not invoke overloads. For equality checks, use Equals({_quantity.Name} other, {_quantity.Name} tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units."")]
754
753
public static bool operator ==({_quantity.Name} left, {_quantity.Name} right)
755
754
{{
756
755
return left.Equals(right);
757
756
}}
758
757
759
758
/// <summary>Indicates strict inequality of two <see cref=""{_quantity.Name}""/> quantities, where both <see cref=""Value"" /> and <see cref=""Unit"" /> are exactly equal.</summary>
760
-
/// <remarks>Consider using <see cref=""Equals({_quantity.Name}, {_valueType}, ComparisonType)""/> to check equality across different units and to specify a floating-point number error tolerance.</remarks>
761
-
[Obsolete(""For null checks, use `x is not null` syntax to not invoke overloads. For quantity comparisons, use Equals({_quantity.Name}, {_valueType}, ComparisonType) to check equality across different units and to specify a floating-point number error tolerance."")]
759
+
[Obsolete(""For null checks, use `x is null` syntax to not invoke overloads. For equality checks, use Equals({_quantity.Name} other, {_quantity.Name} tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units."")]
762
760
public static bool operator !=({_quantity.Name} left, {_quantity.Name} right)
763
761
{{
764
762
return !(left == right);
765
763
}}
766
764
767
765
/// <inheritdoc />
768
766
/// <summary>Indicates strict equality of two <see cref=""{_quantity.Name}""/> quantities, where both <see cref=""Value"" /> and <see cref=""Unit"" /> are exactly equal.</summary>
769
-
/// <remarks>Consider using <see cref=""Equals({_quantity.Name}, {_valueType}, ComparisonType)""/> to check equality across different units and to specify a floating-point number error tolerance.</remarks>
770
-
[Obsolete(""Consider using Equals({_quantity.Name}, {_valueType}, ComparisonType) to check equality across different units and to specify a floating-point number error tolerance."")]
767
+
[Obsolete(""Use Equals({_quantity.Name} other, {_quantity.Name} tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units."")]
771
768
public override bool Equals(object? obj)
772
769
{{
773
770
if (obj is null || !(obj is {_quantity.Name} otherQuantity))
@@ -778,8 +775,7 @@ public override bool Equals(object? obj)
778
775
779
776
/// <inheritdoc />
780
777
/// <summary>Indicates strict equality of two <see cref=""{_quantity.Name}""/> quantities, where both <see cref=""Value"" /> and <see cref=""Unit"" /> are exactly equal.</summary>
781
-
/// <remarks>Consider using <see cref=""Equals({_quantity.Name}, {_valueType}, ComparisonType)""/> to check equality across different units and to specify a floating-point number error tolerance.</remarks>
782
-
[Obsolete(""Consider using Equals({_quantity.Name}, {_valueType}, ComparisonType) to check equality across different units and to specify a floating-point number error tolerance."")]
778
+
[Obsolete(""Use Equals({_quantity.Name} other, {_quantity.Name} tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units."")]
783
779
public bool Equals({_quantity.Name} other)
784
780
{{
785
781
return new {{ Value, Unit }}.Equals(new {{ other.Value, other.Unit }});
@@ -863,15 +859,37 @@ public int CompareTo({_quantity.Name} other)
863
859
/// <param name=""tolerance"">The absolute or relative tolerance value. Must be greater than or equal to 0.</param>
864
860
/// <param name=""comparisonType"">The comparison type: either relative or absolute.</param>
865
861
/// <returns>True if the absolute difference between the two values is not greater than the specified relative or absolute tolerance.</returns>
862
+
[Obsolete(""Use Equals({_quantity.Name} other, {_quantity.Name} tolerance) instead, to check equality across units and to specify the max tolerance for rounding errors due to floating-point arithmetic when converting between units."")]
866
863
public bool Equals({_quantity.Name} other, {_quantity.ValueType} tolerance, ComparisonType comparisonType)
867
864
{{
868
865
if (tolerance < 0)
869
-
throw new ArgumentOutOfRangeException(""tolerance"", ""Tolerance must be greater than or equal to 0."");
866
+
throw new ArgumentOutOfRangeException(nameof(tolerance), ""Tolerance must be greater than or equal to 0."");
public bool Equals(IQuantity? other, IQuantity tolerance)
877
+
{{
878
+
return other is {_quantity.Name} otherTyped
879
+
&& (tolerance is {_quantity.Name} toleranceTyped
880
+
? true
881
+
: throw new ArgumentException($""Tolerance quantity ({{tolerance.QuantityInfo.Name}}) did not match the other quantities of type '{_quantity.Name}'."", nameof(tolerance)))
882
+
&& Equals(otherTyped, toleranceTyped);
883
+
}}
884
+
885
+
/// <inheritdoc />
886
+
public bool Equals({_quantity.Name} other, {_quantity.Name} tolerance)
0 commit comments