The right way to compare a System.Double to '0' (a number, int?)

前端 未结 6 2092
遥遥无期
遥遥无期 2020-12-08 03:44

Sorry, this might be a easy stupid question, but I need to know to be sure.

I have this if expression,

void Foo()
{
    System.Double so         


        
6条回答
  •  一个人的身影
    2020-12-08 04:26

    If something has been assigned from the result of an operation other than something = 0 then you better use:

    if(Math.Abs(something) < Double.Epsilon)
    {
    //do something
    }
    

    Edit: This code is wrong. Epsilon is the smallest number, but not quite zero. When you wish to compare a number to another number, you need to think of what is the acceptable tolerance. Let's say that anything beyond .00001 you don't care about. That's the number you'd use. The value depends on the domain. However, it's mostly certainly never Double.Epsilon.

提交回复
热议问题