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
- refactored the `IQuantity` interface: removed the `Equals` with
tolerance and the `ToString` overloads
- introduced `IAffineQuantity` (see `Temperature`),
`ILogarithmicQuantity` (see `AmplitudeRatio`) and `ILinearQuantity` (see
`Mass`)
- introduced extension methods for `Equals` with tolerance as well as
`Sum` and `Average` (as applicable)
- replaced the implementation of the `GetHashCode` method with a call to
a better optimized function in the `Comparison` class
- removed the `Sum`/`Average` from `UnitMath` (replaced be the new
extension methods using the _more concrete_ definitions)
- marked the `GenericMathExtensions` class as `[Obsolete]` (replaced by
the new extension methods)
fixes#1461
---------
Co-authored-by: Andreas Gullberg Larsen <andreas.larsen84@gmail.com>
@@ -658,7 +692,12 @@ public static bool TryParseUnit([NotNullWhen(true)]string? str, IFormatProvider?
658
692
659
693
privatevoidGenerateArithmeticOperators()
660
694
{
661
-
if(!_quantity.GenerateArithmetic)return;
695
+
if(_quantity.IsAffine)
696
+
{
697
+
// the generation of arithmetic operators with affine quantities, such as Temperature + TemperatureDelta, is currently not supported
698
+
// TODO see about handling this case using the UnitRelations
699
+
return;
700
+
}
662
701
663
702
// Logarithmic units required different arithmetic
664
703
if(_quantity.Logarithmic)
@@ -919,6 +958,15 @@ public bool Equals({_quantity.Name} other)
919
958
920
959
#pragma warning restore CS0809
921
960
961
+
/// <summary>
962
+
/// Returns the hash code for this instance.
963
+
/// </summary>
964
+
/// <returns>A hash code for the current {_quantity.Name}.</returns>
965
+
public override int GetHashCode()
966
+
{{
967
+
return Comparison.GetHashCode(Unit, Value);
968
+
}}
969
+
922
970
/// <summary>Compares the current <see cref=""{_quantity.Name}""/> with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other when converted to the same unit.</summary>
923
971
/// <param name=""obj"">An object to compare with this instance.</param>
/// Compare equality to another {_quantity.Name} within the given absolute or relative tolerance.
961
-
/// </para>
962
-
/// <para>
963
-
/// Relative tolerance is defined as the maximum allowable absolute difference between this quantity's value and
964
-
/// <paramref name=""other""/> as a percentage of this quantity's value. <paramref name=""other""/> will be converted into
965
-
/// this quantity's unit for comparison. A relative tolerance of 0.01 means the absolute difference must be within +/- 1% of
966
-
/// this quantity's value to be considered equal.
967
-
/// <example>
968
-
/// In this example, the two quantities will be equal if the value of b is within +/- 1% of a (0.02m or 2cm).
969
-
/// <code>
970
-
/// var a = Length.FromMeters(2.0);
971
-
/// var b = Length.FromInches(50.0);
972
-
/// a.Equals(b, 0.01, ComparisonType.Relative);
973
-
/// </code>
974
-
/// </example>
975
-
/// </para>
976
-
/// <para>
977
-
/// Absolute tolerance is defined as the maximum allowable absolute difference between this quantity's value and
978
-
/// <paramref name=""other""/> as a fixed number in this quantity's unit. <paramref name=""other""/> will be converted into
979
-
/// this quantity's unit for comparison.
980
-
/// <example>
981
-
/// In this example, the two quantities will be equal if the value of b is within 0.01 of a (0.01m or 1cm).
982
-
/// <code>
983
-
/// var a = Length.FromMeters(2.0);
984
-
/// var b = Length.FromInches(50.0);
985
-
/// a.Equals(b, 0.01, ComparisonType.Absolute);
986
-
/// </code>
987
-
/// </example>
988
-
/// </para>
989
-
/// <para>
990
-
/// Note that it is advised against specifying zero difference, due to the nature
991
-
/// of floating-point operations and using double internally.
992
-
/// </para>
993
-
/// </summary>
994
-
/// <param name=""other"">The other quantity to compare to.</param>
995
-
/// <param name=""tolerance"">The absolute or relative tolerance value. Must be greater than or equal to 0.</param>
996
-
/// <param name=""comparisonType"">The comparison type: either relative or absolute.</param>
997
-
/// <returns>True if the absolute difference between the two values is not greater than the specified relative or absolute tolerance.</returns>
998
-
[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."")]
999
-
public bool Equals({_quantity.Name} other, double tolerance, ComparisonType comparisonType)
1000
-
{{
1001
-
if (tolerance < 0)
1002
-
throw new ArgumentOutOfRangeException(nameof(tolerance), ""Tolerance must be greater than or equal to 0."");
1003
-
1004
-
return UnitsNet.Comparison.Equals(
1005
-
referenceValue: this.Value,
1006
-
otherValue: other.As(this.Unit),
1007
-
tolerance: tolerance,
1008
-
comparisonType: comparisonType);
1009
-
}}
1010
-
1011
-
/// <inheritdoc />
1012
-
public bool Equals(IQuantity? other, IQuantity tolerance)
1013
-
{{
1014
-
return other is {_quantity.Name} otherTyped
1015
-
&& (tolerance is {_quantity.Name} toleranceTyped
1016
-
? true
1017
-
: throw new ArgumentException($""Tolerance quantity ({{tolerance.QuantityInfo.Name}}) did not match the other quantities of type '{_quantity.Name}'."", nameof(tolerance)))
1018
-
&& Equals(otherTyped, toleranceTyped);
1019
-
}}
1020
-
1021
-
/// <inheritdoc />
1022
-
public bool Equals({_quantity.Name} other, {_quantity.Name} tolerance)
1023
-
{{
1024
-
return UnitsNet.Comparison.Equals(
1025
-
referenceValue: this.Value,
1026
-
otherValue: other.As(this.Unit),
1027
-
tolerance: tolerance.As(this.Unit),
1028
-
comparisonType: ComparisonType.Absolute);
1029
-
}}
1030
-
1031
-
/// <summary>
1032
-
/// Returns the hash code for this instance.
1033
-
/// </summary>
1034
-
/// <returns>A hash code for the current {_quantity.Name}.</returns>
1035
-
public override int GetHashCode()
1036
-
{{
1037
-
return new {{ Info.Name, Value, Unit }}.GetHashCode();
1038
-
}}
1039
-
1040
1006
#endregion
1041
1007
");
1042
1008
}
@@ -1061,6 +1027,15 @@ public double As({_unitEnumName} unit)
@@ -1222,27 +1197,6 @@ public override string ToString()
1222
1197
return ToString(null, null);
1223
1198
}}
1224
1199
1225
-
/// <summary>
1226
-
/// Gets the default string representation of value and unit using the given format provider.
1227
-
/// </summary>
1228
-
/// <returns>String representation.</returns>
1229
-
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
/// Gets the string representation of this instance in the specified format string using the specified format provider, or <see cref=""CultureInfo.CurrentCulture"" /> if null.
0 commit comments