my first if statement evaluates to true and my second if statement evaluates to false clearly but that doesn't make sense at all because the second part of it is true and theres just an or operator between them. my output therefore is:
5
1 5
can someone tell me what's going on ? if you need more code just ask me.
thanks in advance.
booloperator < (BigInt a,BigInt b) {
while (a.digits.size()-a.dec!=b.digits.size()-b.dec) {//add zeros at the end of the number to make sure our numbers have the same precision
if (a.digits.size()-a.dec>b.digits.size()-b.dec) {b.digits.push_back(0);} else {a.digits.push_back(0);}
}
while (a.digits.size()!=b.digits.size()) {//add zeros at the begin of the number to make sure they have the same size.
if (a.digits.size()>b.digits.size()) {b.digits.insert(b.digits.begin(),1,0);b.dec++;} else {a.digits.insert(a.digits.begin(),1,0);a.dec++;}
}
for (unsignedint i=0;i<a.digits.size();i++) {
if (a.digits.at(i)<b.digits.at(i)) {
returntrue;
} elseif (a.digits.at(i)>b.digits.at(i)) {
returnfalse;
}
}
returnfalse;
}
The only thing I can think of is that your < and/or > operator (edit: or the stream operator) is actually modifying the state of either this or test1, so that the result of the comparison at line 6 is different from the result of the seemingly identical comparison at line 9.
The zero-adding in operator< concerns me. Especially that you seem to be adding 0's at both end of the numbers, and I don't see why they need to be the same size to be compared.
1 is the same as 000000001, but it is not the same as 10000000.
Have you tested operator< to ensure that it actually works for different test cases?