Skip to content
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

[Feature] Global Tolerance Configurations #1440

Open
ArchiDog1998 opened this issue Dec 2, 2024 · 2 comments
Open

[Feature] Global Tolerance Configurations #1440

ArchiDog1998 opened this issue Dec 2, 2024 · 2 comments

Comments

@ArchiDog1998
Copy link

ArchiDog1998 commented Dec 2, 2024

a.Equals(b) is the basic way to check if two elements are the same. But it is Obsolete!
For the case I want to use record, the equality is almost unusable.

/// <inheritdoc />
/// <summary>Indicates strict equality of two <see cref="Length"/> quantities, where both <see cref="Value" /> and <see cref="Unit" /> are exactly equal.</summary>
[Obsolete("Use Equals(Length other, Length 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.")]
public override bool Equals(object? obj)
{
if (obj is null || !(obj is Length otherQuantity))
return false;
return Equals(otherQuantity);
}
/// <inheritdoc />
/// <summary>Indicates strict equality of two <see cref="Length"/> quantities, where both <see cref="Value" /> and <see cref="Unit" /> are exactly equal.</summary>
[Obsolete("Use Equals(Length other, Length 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.")]
public bool Equals(Length other)
{
return new { Value, Unit }.Equals(new { other.Value, other.Unit });
}

Shall we add a static class like Global Tolerance Configurations to let it work like the Equals method with tolerance? That would make things ez.

/// <inheritdoc />
public bool Equals(Length other, Length tolerance)
{
return UnitsNet.Comparison.Equals(
referenceValue: this.Value,
otherValue: other.As(this.Unit),
tolerance: tolerance.As(this.Unit),
comparisonType: ComparisonType.Absolute);
}

So it should look like this.

 public bool Equals(Length other) 
 { 
     return Equals(other, GlobalToleranceConfigurations.Length);
 } 
@lipchev
Copy link
Collaborator

lipchev commented Dec 4, 2024

I know this is a PIA, but defining a global tolerance constant might not be the safest option (there isn't really a way of coming up with reasonable defaults), especially when you consider the possibility of linking together different projects.

PS I keep saying this for months now, but I'm reeeally close to finishing my new proposal for v6 which, if accepted, should bring back the == operators (without any of the rounding issues we've had in the past).

@ArchiDog1998
Copy link
Author

That sounds awesome! I can't wait for that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants