First of all, please excuse any typo, English is not my native language.
Here is my question. I am creating a class that represents approximate values as such:
public sealed class ApproximateValue { public double MaxValue { get; private set; } public double MinValue { get; private set; } public double Uncertainty { get; private set; } public double Value { get; private set; } public ApproximateValue(double value, double uncertainty) { if (uncertainty < 0) { throw new ArgumentOutOfRangeException("uncertainty", "Value must be postivie or equal to 0."); } this.Value = value; this.Uncertainty = uncertainty; this.MaxValue = this.Value + this.Uncertainty; this.MinValue = this.Value - this.Uncertainty; } }
I want to use this class for undefined measurements, for example x = 8.31246 +/- 0.0045, and perform calculations on these values.
I want to overload the operators of this class. I don’t know how to implement the>,> =, <= and <operators ... The first thing I thought was something like this:
public static bool? operator >(ApproximateValue a, ApproximateValue b) { if (a == null || b == null) { return null; } if (a.MinValue > b.MaxValue) { return true; } else if (a.MaxValue < b.MinValue) { return false; } else { return null; } }
However, in the latter case, I am not satisfied with this "zero", since the exact result is not equal to "null". It may be "truth," or it may be a "lie."
Is there any object in .Net 4 that will help implement this function that I don’t know about, or am I doing the right way? I also thought about using an object instead of a logical one, which would determine in what circumstances the value is superior or not relative to another, instead of performing comparison operations, but I feel that it is too complicated for what I am trying to achieve ..
source share