K
Kai-Uwe Bux
Virtual_X said:Virtual_X said:i have write another powerful function "as i thought " to check
double equality with very high
precision because it check equality for every bit instead of the
"Microsoft msdn method"here's my function "depend on the pb()"bool d_eq(double x,double y)
{
byte bx[8];
byte by[8];pb(reinterpret_cast<unsigned char*>(&x),bx);
pb(reinterpret_cast<unsigned char*>(&y),by);for(int i=0;i < 8;i++)
for(int o=0;o < 8;o++)
if (bx.bit[o] != by.bit[o]) return false;
return true;
}
Where [quoted from some other posting]:
struct byte
{
bool bit[8]; // 8 = sizeof(double);
};void pb(unsigned char *ch,byte *bin)
{
for(int i=0;i <=7;i++)
for(int o=7;o >=0;o--)
{
if(ch & 1<<o)
bin.bit[abs(o-7)]= true;
else
bin.bit[abs(o-7)]= false;
}
}
May I ask what advantage d_eq(x,y) has compared to x==y?
you aren't able to use the operator "==" with floating-point numbers
That is incorrect.
"it's not a precision method"
That is a meaningless statement.
watch the standard
The standard is with me on this issue (operator== test for being equal, and
that's it).
http://www.cprogramming.com/tutorial/floating_point/understanding_floating_point.htmlfor more info or check this link
That link says (among other things):
... why is it so hard to know when two floats are equal? In one sense, it
really isn't that hard; the == operator will, in fact, tell you if two
floats are exactly equal (i.e., match bit for bit). ...
which seems to agree with me.
whereas this link is unrelated to C++ as C++ does not require IEEE
conformance.
d_eq compare each bit so it's very precision
try that code
double x=215.256487876545;
double y=215.256487876545;
cout << d_eq(x,y);
and then try
double x=215.256487876545;
double y=215.2564878765449; // i change only the last number and
add another in the fraction
cout << d_eq(x,y);
you will watch the precision
The standard guarantes that you will get the same with operator== provided
the type double on your implementation has the precision required to
distinguish x and y.
I think your d_eq() is a non-portable equivalent of
bool truly_equal ( double lhs, double rhs ) {
double volatile x = lhs;
double volatile y = rhs;
return ( x == y );
}
Here, I hope the volatile causes a write to memory so that any excess
precision that the parameters may have according to [5/10] goes away.
absolutely , as you see in microsoft msdn code you must limit the
equality precision by
#define EPSILON 0.0001 // Define your own tolerance
in d_eq() you don't need for that just put any numbers
the function d_eq() as i thought is very important in advanced math
application when you need
for high precision like
the number 215.256487876545 not equal to 215.2564
Actually, in advanced math you may need a different precision that 0.00001
but you will practically always want a precision that is below the inherent
precision of the floating point type. Otherwise, it is very very hard to
ensure that loops terminate. Consider, for instance the bisection method
for solving equations implemented like this:
double low = something;
double high = something_else;
while ( ! d_eq( low, high ) ) {
double middle = (low+high) / 2.0;
if ( f(low) and f(middle) have same sign ) {
high = middle;
else {
low = middle;
}
}
It may happen that the loop does not terminate.
That is, why I doubt that your method yields the _desired_ result.
Best
Kai-Uwe Bux