On Fri, 2011-09-02 at 16:36 -0600, Matt Fair wrote:
> 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
>