The equality operator (==) in my class is not working

I have been following some cpp exercises to learn cpp and I've run into a problem.

I've created a class called "FixedPoint2" to implement a fixed point number to 2 decimals. I've included the header file which includes all the functions below.

What I am struggling with is that when I try to test out the equality operator, I always get a false answer. In other words the following happens:

cout << (FixedPoint2(1.0)==FixedPoint2(1.0)) << endl; //returns true as expected
cout << (FixedPoint2(1.2)==FixedPoint2(1.2)) << endl; //returns false
cout << FixedPoint2(1.2) << "t" << FixedPoint2(1.2) << endl; returns 1.2 1.2 

So you get the idea. I have also tested it with if-statements to make sure my overloaded call out is not the problem. For example:

if (FixedPoint2(4.5)==FixedPoint2(4.5)) 
    cout << "Don't post to stackoverflow"; //This doesn't print

My intuition tells me something is up with with either some implicit type conversion I have overlooked, or some messy things within double. But I don't think either of those is it.

using namespace std;


class FixedPoint2
{
    private:
        int16_t m_digi; //chosen because I want the range
        int8_t m_deci; //chosen to optimise memory 
    public:
        FixedPoint2(int16_t digi = 0, int8_t deci = 0):m_digi{digi}, m_deci{deci}
        {
            assert(!(deci>127 || deci<-127)); //to prevent overflows
            if(deci<-100 || deci>100) //just in case some moron (me) does some weird decimal calculations 
            {
                m_digi+=(static_cast<int16_t>(deci)/100);
                m_deci+=(-100);
            }
        }
    FixedPoint2(double mydouble) 
    {
        if (mydouble>=0) //The if-else floors the absolute value of the integer base
        {
            m_digi=static_cast<int16_t>(floor(mydouble));
        }
        else
        {
            m_digi=static_cast<int16_t>(floor(mydouble)+1);
        }
        m_deci=static_cast<int8_t>(round(100*(mydouble-m_digi))); //This rounds off the decimal to two digits

    };

    operator double();

    friend ostream& operator<<(ostream &out, const FixedPoint2 &f1);
    friend istream& operator>>(istream &in, FixedPoint2 &f1);
    friend FixedPoint2 operator+(const FixedPoint2 &f1, const FixedPoint2 &f2);
};

FixedPoint2::operator double()
{
    double base= static_cast<double>(m_digi);
    double decimal= static_cast<double>(m_deci);
    return base+0.01*decimal;
}

ostream& operator<<(ostream &out, const FixedPoint2 &f1)
{
    FixedPoint2 a=f1;
    out << double(a); //this is an easy work around to handling the period placement for the fixed point number
    return out;
}


istream& operator>>(istream &in, FixedPoint2 &f1)
{
    double placeholder;
    in>>placeholder;
    f1=FixedPoint2(placeholder);
    return in;
}

FixedPoint2 operator+(const FixedPoint2 &f1, const FixedPoint2 &f2)
{
    return FixedPoint2(f1.m_digi+f2.m_digi, f1.m_deci+f2.m_deci);
}

The compiler does not automatically generate operator== because odds are really good that it will get it wrong a lot more often than it gets it right. Let's take a simple example: a dynamic string. The compiler would generate code that compared character by character, right? When does it stop? Now the compiler needs to know more about the programmer's intent with their string, and the compiler doesn't need the extra complexity of a telepathic interface.

Better to have on consistent rule, no, and force an explicit definition of what is to be compared than a minefield of crap code resulting from people assuming they got what they wanted. Longer discussion on the topic here: Why don't C++ compilers define operator== and operator!=?

The compiler looks around for a way to satisfy the comparison. It doesn't find a direct operator== , but it does find operator double and double s can be used for the compare. Except sometimes they can't: Is floating point math broken?

That said I'm unable to reproduce OP's results. I would expect the exact same formula to execute on the exact same input to get the exact same result, even if that result is 1.199999... rather than 1.2

While I cannot reproduce, OP is still better off implementing the fixed point operator== because there is no imprecision with a fixed point number. Fixed point will be equal or not equal, no ifs ands or buts, so "Nothing can go wrong!" Plus, this operator should be trivial to write. Something like return (rhs.m_digi == lhs.m_digi) && (rhs.m_deci == lhs.m_deci);

链接地址: http://www.djcxy.com/p/73788.html

上一篇: 布尔运算符==在typedef结构中

下一篇: 我班的等号运算符(==)不起作用