money comparison
Posted by
Matt Fair on
Sep 02, 2011; 8:22pm
URL: http://quantlib.414.s1.nabble.com/money-comparison-tp11964.html
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
------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev