|
From: Matt F. <mat...@gm...> - 2011-09-02 20:22:16
|
I'm wondering if in money.cpp for bool operator==(const Money& m1, const
Money& m2), the comparison between two double values should not be a
straight ==. But instead there should be a is_equal function with an
epselon to compare the two values. Because due to how the system stores
numbers, double values can be shifted slightly when operations are performed
on them. I'm having problems comparing two money values because of several
math operations, they aren't quite the same.
Given:
bool is_equal(double d1, double d2)
{
if(abs(d1-d2)<epsilon)
return true;
return false;
}
Where epsilon could be defined by the currency precision.
I would suggest:
return m1.value() == m2.value();
be changed to:
return is_equal(m1.value(), m2.value());
See the following links for more info:
http://www.cplusplus.com/forum/articles/3827/
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
Thanks,
Matt
|