简体   繁体   中英

what is the best way to determine is 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.

Hi depends on what sort of calculations you are doing. Some times you get a very small number which is close to 0 but not quite

I usually do

if (Math.Abs(MyNumber) < 1e-10)

Hope it helps

if ( Math.Abs(doubleNumber) > Math.Pow(1,-9) ) //doubleNumber not zero.

This much is enough to check the double is zero or not

        double d = 0.00000005;
        bool l =  d.Equals(0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM