Posted by
Matt Fair on
URL: http://quantlib.414.s1.nabble.com/money-comparison-tp11964p11965.html
I noticed that there are already functions that do exactly what I described. Here is a modification to money.cpp that I think would make sense:
Index: money.cpp
===================================================================
--- money.cpp (revision 17937)
+++ money.cpp (working copy)
@@ -103,17 +105,7 @@
bool operator==(const Money& m1, const Money& m2) {
if (m1.currency() == m2.currency()) {
- return m1.value() == m2.value();
- } else if (Money::conversionType == Money::BaseCurrencyConversion) {
- Money tmp1 = m1;
- convertToBase(tmp1);
- Money tmp2 = m2;
- convertToBase(tmp2);
- return tmp1 == tmp2;
- } else if (Money::conversionType == Money::AutomatedConversion) {
- Money tmp = m2;
- convertTo(tmp, m1.currency());
- return m1 == tmp;
+ return close_enough(m1, m2);
} else {
QL_FAIL("currency mismatch and no conversion specified");
}
Matt
On Fri, Sep 2, 2011 at 2:22 PM, Matt Fair
<[hidden email]> wrote:
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