什么是确定的最佳方式是一个双不为零是一个、为零、方式

2023-09-03 16:30:24 作者:太阳灼烧~眼泪的温热

在确定双方不是0任何建议?

Any suggestion on determining a double is not zero?

推荐答案

您必须设置的小量的是与你的解决问题的兼容。然后,你可以使用类似

You have to set an epsilon that is compatible with the problem you are solving. Then, you could use something like

bool DoubleEquals(double value1, double value2)
{
    return Math.Abs(value1 - value2) < epsilon;
}

修改 既然你问了一个方法来确定是否双不为零,可以通过书面形式使用此功能:

EDIT Since you asked a way to determine if a double is not zero, you could use this function by writing:

if (!DoubleEquals(value, 0)) { /* do your not zero things */ }

我觉得这种方法是更好,因为它更通用。

I felt this method was better because it's more general purpose.