c++ - How to compare a long long with a double? -
c++ - How to compare a long long with a double? -
(this has duplicate, can't find duplicate...) how compare long long double in c++?
casting either other can cause loss of precision , perchance overflow, i'm not sure do...
honestly: utilize long double on platform long doubles have 64-bit significand.
if want handle platforms without such support:
i'm going assume long long 64-bit , double usual ieee 64-bit double.
if d >= 0x1.0p63, long long less it. if d < -0x1.0p63, long long greater it. if d != d, comparing should homecoming false because d nan. if fabs(d) >= 0x1.0p53, d represents integer, can convert d long long , comparing long longs. otherwise, can convert ll double , comparing doubles; if low bits lost, irrelevant comparison.
code (uncompiled , untested):
#define compar(a, b) ((a) < (b) ? -1 : (a) == (b) ? 0 : 1) int compar(long long a, double b) { if (b != b) homecoming false; if (b >= 0x1.0p63) homecoming -1; if (b < -0x1.0p63) homecoming 1; if (fabs(b) >= 0x1.0p53) homecoming compar(a, (long long)b); else homecoming compar((double)a, b); } #undef compar c++ double long-long
Comments
Post a Comment