包含在双打列表中

发布于 2025-01-18 17:15:21 字数 928 浏览 3 评论 0原文

我使用 Visual Studio 立即窗口来检查列表的内容。这是我可以看到的:

found_numbers
Count = 36
    [0]: 50,82
    [1]: 3358
    [2]: 954
    [3]: 5571
    [4]: 3142
    [5]: 700
    [6]: 322
    [7]: 402
    [8]: 1231
    [9]: 4118
    [10]: 4532
    [11]: 0
    [12]: 0
    [13]: 3101
    [14]: 18
    [15]: 0
    [16]: 0
    [17]: 8896
    [18]: 0
    [19]: 4,01
    [20]: 19,5
    [21]: 0,78
    [22]: 20,28
    [23]: 10
    [24]: 27,76
    [25]: 2,78
    [26]: 30,54
    [27]: 4648
    [28]: 508
    [29]: 1,51
    [30]: 4648
    [31]: 508
    [32]: 1,51
    [33]: 0,28
    [34]: 0,28
    [35]: 0,56

然后我检查两个变量的值

total1
50,82
total3
20,28

在最后一步中,我想查看我的列表是否包含这些值,这是我的输出:

found_numbers.Contains(total1)
false
found_numbers.Contains(total3)
true

我对这种行为感到非常困惑,因为这两个数字都进入了给定的列表。为什么会发生这种情况?编辑: total1total3 都是 double 类型。

Im using visual studio Immediate Window to check the content of my List. This is what I can see:

found_numbers
Count = 36
    [0]: 50,82
    [1]: 3358
    [2]: 954
    [3]: 5571
    [4]: 3142
    [5]: 700
    [6]: 322
    [7]: 402
    [8]: 1231
    [9]: 4118
    [10]: 4532
    [11]: 0
    [12]: 0
    [13]: 3101
    [14]: 18
    [15]: 0
    [16]: 0
    [17]: 8896
    [18]: 0
    [19]: 4,01
    [20]: 19,5
    [21]: 0,78
    [22]: 20,28
    [23]: 10
    [24]: 27,76
    [25]: 2,78
    [26]: 30,54
    [27]: 4648
    [28]: 508
    [29]: 1,51
    [30]: 4648
    [31]: 508
    [32]: 1,51
    [33]: 0,28
    [34]: 0,28
    [35]: 0,56

Then I check the value of my two variables

total1
50,82
total3
20,28

In the last step, I want to see if my list contains those values and this is my output:

found_numbers.Contains(total1)
false
found_numbers.Contains(total3)
true

Im really confused about this behaviour as the both numbers are into the the given list. Why could this be happening? Edit: both total1 and total3 are type double.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尸血腥色 2025-01-25 17:15:21

当我们使用 double 进行计算时,我们会得到舍入错误,这就是为什么我们实际上得到的是 50,82000000001< 而不是 50,82 /code> 或 50,81999999998。所有这些值通常表示四舍五入50,82,并且我们有奇怪的found_numbers.Contains(total1) == false< /代码>。要获得精确 double 值,请使用R 格式;

found_numbers[0].ToString("R")
total1.ToString("R")

尝试这些表示,您就会看到差异。如果您想摆脱舍入问题,您可以:

  1. double切换到decimal(特别是如果值有固定小数点,例如如果值是货币)
  2. 在比较时使用容差
//TODO: put the right value here
double tolerance = 1e-8;

bool found = found_numbers.Any(item => Math.Abs(item - total1) <= tolerance);

When we do computation with double we get rounding errors, and that's why instead of 50,82 we actually have 50,82000000001 or 50,81999999998. All these values are usually represented being rounded as 50,82 and we have strange found_numbers.Contains(total1) == false. To get exact double value use R format;

found_numbers[0].ToString("R")
total1.ToString("R")

try these representations and you'll see the diffrence. If you want to get rid of rounding problems you can:

  1. Switch from double to decimal (esp. if values have fixed decimal point, e.g. if values are currencies)
  2. Use tolerance when comparing:
//TODO: put the right value here
double tolerance = 1e-8;

bool found = found_numbers.Any(item => Math.Abs(item - total1) <= tolerance);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文