开发者

Treating double variable as boolean

Which is the better way(efficiency/best practice wise) to test if a double variable is equal to 0?

开发者_如何转开发    1. if(a_double)
          .....
       else
          .....

    OR

    2. if(a_double == 0)
          .....
       else
          .....


The second is generally better (more explicit about what you're doing). I'd normally prefer if (a_double == 0.0) though. Also note that with floating point, it's often useful to do an approximate comparison to account for the possibility of rounding in the calculations (though doing this well can be non-trivial).

Edit: Since there appears to be some misunderstanding about what numbers can and can't be represented precisely: most computers use binary floating point. This means a fraction in which the denominator is a sum of powers of two can be represented exactly. If (and only if) the denominator contains a prime factor that cannot be represented as a sum of powers of 2 is it impossible to represent that number precisely.

Of course, if you get too small (or too large) it can be impossible to represent the number at all (e.g., normal IEEE floating point doesn't provide a way to represent a number like 1e+10000 or 1e-2000). Also, when you approach the limits of representation, you give up some precision (e.g., the limit for a normal double is 1e-308 -- and at 1e-300, you only get ~7 digits of precision instead of the usual ~15).

Within certainly limits, however, integers can be represented precisely (depends on the size of the significand -- usually around 253), and so can fractions where the denominator is a sum or powers of 2 (e.g., 3.5, 1.375).

There are also computers that use hexadecimal floating point and/or decimal floating point. For what we're considering here, hexadecimal floating point is essentially the same as binary floating point (i.e., since 16 is a power of 2, it can still only represent fractions precisely if the denominator is a sum of powers of 2). Decimal floating point allows precise representation of numbers where the denominator includes powers of 5 as well (e.g., 1.2 can be represented precisely in decimal floating point but not in binary or hexadecimal floating point). This has the obvious advantage that (again, within the limits of its range and precision) anything you enter as a decimal number will be represented precisely.


Because in general doubles are not going to be exactly equal to any integer you need to be careful when testing them for equality.

Read this: http://docs.sun.com/source/806-3568/ncg_goldberg.html

In essence with a float you want to test that the float is close to some number within some tolerance.


Edit

Apologies for the previous incomplete and incorrect explanation. This is what I meant.

#include<iostream>
using namespace std;
int main(){
double a=3/5;
double b=2/5;
double c=(a+b)-1;
if(c==0)
        cout<<"C is 0"<<endl;
else if(c==0.0)
        cout<<"C is 0.0"<<endl;
else
        cout<<"C!=0 && C!=0.0"<<endl; //This will print most of the time
}

--Edit Ends--

You may have to read the test cases as to how a_double gets its values. The greatest problem as @Jerry told is that it's difficult to test == with double. Note the following will not work as expected..

#include<iostream>
using namespace std;
int main(){
double a=5.5;
double b=6.5;
double c=(a+b)/12;
if(c==0)
        cout<<c<<endl;
else
        cout<<"c is not equal to 0"<<endl;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜