首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较c# 4.0中的近似值?

比较c# 4.0中的近似值?
EN

Stack Overflow用户
提问于 2011-10-29 00:25:18
回答 5查看 1.1K关注 0票数 4

首先,请原谅我的打字错误,英语不是我的母语。

这是我的问题。我正在创建一个表示近似值的类:

代码语言:javascript
复制
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;
    }
}

我想将这个类用于不确定的测量,例如x= 8.31246 +/-0.0045,并对这些值执行计算。

我想重载这个类中的运算符。我不知道如何实现>、>=、<=和<运算符...我首先想到的是这样的东西:

代码语言:javascript
复制
    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; }
    }

然而,在最后一种情况下,我对这个'null‘并不满意,因为准确的结果不是'null’。它可能是“true”,也可能是“false”。

在.Net 4中是否有我不知道的对象可以帮助实现这个特性,或者我这样做是正确的吗?我也在考虑使用对象而不是布尔值来定义在什么情况下值优于或不优于另一个值,而不是实现比较运算符,但我觉得对于我试图实现的目标来说,这有点太复杂了……

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-10-29 00:37:31

这是为数不多的几种情况之一,在这种情况下,定义一个值类型(struct)可能更有意义,这样就消除了null case问题。您还可以将MinValue和MaxValue修改为计算属性(只需实现计算结果的get方法),而不是在构造时存储它们。

另外,近似值的比较本身就是一种近似操作,因此您需要考虑您的数据类型的用例;您是否只打算使用比较来确定范围何时不重叠?这真的取决于你的类型的含义。这是否打算表示来自正态分布数据集的数据点,其中不确定性是采样的一些标准偏差?如果是这样的话,比较操作返回一个数值概率可能更有意义(当然,不能通过比较运算符调用它)。

票数 1
EN

Stack Overflow用户

发布于 2011-10-29 00:40:48

我可能会做这样的事情。我将实现IComparable<ApproximateValue>,然后根据CompareTo()的结果定义<、>、<=和>=

代码语言:javascript
复制
public int CompareTo(ApproximateValue other)
{
    // if other is null, we are greater by default in .NET, so return 1.
    if (other == null)
    {
        return 1;
    }

    // this is > other
    if (MinValue > other.MaxValue)
    {
        return 1;
    }

    // this is < other
    if (MaxValue < other.MinValue)
    {
        return -1;
    }

    // "same"-ish
    return 0;
}

public static bool operator <(ApproximateValue left, ApproximateValue right)
{
    return (left == null) ? (right != null) : left.CompareTo(right) < 0;
}

public static bool operator >(ApproximateValue left, ApproximateValue right)
{
    return (right == null) ? (left != null) : right.CompareTo(left) < 0;
}

public static bool operator <=(ApproximateValue left, ApproximateValue right)
{
    return (left == null) || left.CompareTo(right) <= 0;
}

public static bool operator >=(ApproximateValue left, ApproximateValue right)
{
    return (right == null) || right.CompareTo(left) <= 0;
}

public static bool operator ==(ApproximateValue left, ApproximateValue right)
{
    return (left == null) ? (right == null) : left.CompareTo(right) == 0;
}

public static bool operator !=(ApproximateValue left, ApproximateValue right)
{
    return (left == null) ? (right != null) : left.CompareTo(left) != 0;
}
票数 3
EN

Stack Overflow用户

发布于 2011-10-29 00:34:15

在我看来,您需要检查a.MaxValue == b.MinValue在您当前的实现中是否也会返回null,这似乎是不正确的,它应该根据您希望的规范的实际工作方式返回truefalse。我不确定有没有内置的.net功能,所以我相信你的做法是正确的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7932033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档