-1

I have made a simple program to print out numbers by using arithmetic operations. Every thing works fine, except last operator. It prints result as : 1 which is wrong and the expected result should be 1.7. What is wrong with my program I have made. Why does it print like this?

#include <iostream>
#include <exception>


class Money
{
public:
    Money(float amount = 0) : m_amount(amount){}

    // logic operations
    bool operator==(const Money& other) const
    {
        return m_amount == other.m_amount;
    }

    // arithmetic operations
    Money operator*=(const Money& other)
    {
        m_amount *= other.m_amount;
        return *this;
    }

    Money operator/=(const Money& other)
    {
        if (other.m_amount == 0)
            throw std::invalid_argument("Division by zero");

        m_amount /= other.m_amount;
        return *this;
    }

    friend std::ostream& operator<<(std::ostream& os, const Money& money)
    {
        return os << '$' << money.m_amount;
    }

private:
    float m_amount;
};

int main()
{
    Money my_money(1.7f);

    std::cout << (my_money *= my_money) << '\n';
    std::cout << (my_money /= my_money) << '\n'; // <-- wrong it should be 1.7
}
MORTAL
  • 383
  • 2
  • 10

1 Answers1

2
my_money /= my_money

is in maths talk

x = a/a, x,a ∈ ℝ\{0}

and because the (ℝ,+,⋅) constitute a field, that has to yield the one-element of the multiplicative sub-group (ℝ, ⋅), and that is 1.

decltype_auto
  • 1,706
  • 10
  • 19
  • Like the posh maths bit, that's got to be worth a plus one. – Bathsheba Nov 06 '15 at 10:52
  • @Bathsheba When I once a counter-proofed a theoretical physicist's attempt to invalidate Maxwell's, Lorentz's and Einstein's work with a single stroke, he started blaming me for being a dumb numeric only; that's when I made it a habit to get analytical at the earliest possible stage. ;) – decltype_auto Nov 06 '15 at 11:00