Define a tolerance value (aka an 'epsilon' or 'delta'), for instance, 0.00001, and then use to compare the difference like so:if (Math.Abs(a - b) < delta){ // Values are within specified tolerance of each other....}您可以使用 Double.Epsilon,但您必须使用乘数.You could use Double.Epsilon but you would have to use a multiplying factor.更好的是,编写一个扩展方法来做同样的事情.我们的单元测试中有类似 Assert.AreSimilial(a,b) 的东西.Better still, write an extension method to do the same. We have something like Assert.AreSimiliar(a,b) in our unit tests.Microsoft 的 Assert.AreEqual() 方法有一个带增量的重载:public static void AreEqual(double expected, double actual, double delta)Microsoft's Assert.AreEqual() method has an overload that takes a delta: public static void AreEqual(double expected, double actual, double delta)NUnit 还为其 Assert.AreEqual() 方法提供了一个重载,允许提供一个增量.NUnit also provides an overload to their Assert.AreEqual() method that allows for a delta to be provided. 这篇关于如何确定两个变量是否近似相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 11:00