this doesn't make sense.. need help

hello i have a very big code so i can't post it all but my problem is in this part:

1
2
3
4
5
6
7
8
  cout <<*this<<endl;
		
		if (*this >test1) {	
			cout <<"1 "<<*this<<endl;
		}
		if ((*this <test2) || (*this >test1)) {	
			cout <<"2 "<<*this<<endl;
		}


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.
Last edited on
Haha, nice one. Must be some typo, or maybe there is in fact else if in second statement?
How is < and > defined for your type?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void scaleby2(BigInt N) { 
		BigInt test1=BigInt("1");
		BigInt test2=BigInt("0.5");
		cout <<*this<<endl;
		
		if (*this >test1) {	
			cout <<"1 "<<*this<<endl;
		}
		if ((*this <test2) || (*this >test1)) {	
			cout <<"2 "<<*this<<endl;
		}



		while (*this <test2 || *this >test1) {
			if (*this <test2) {
				cout <<"-----------"<<*this<<endl;
				*this=*this*2;
				N=N*2;
			} else if (*this >test1) {
				cout <<"    "<<*this<<endl;
				cout <<*this<<endl;
				*this=*this/2;
				N=N/2;
			} else {
				cout <<"##########"<<*this<<endl;
				return;
			}
			
		}

	}


this is my function which is in the BigInt class
and it looks like its not even doing anything in that while loop..
Last edited on
lines 2 and 3 can be simplified like this BigInt test1("1");
and I think peter was asking about your operator function eg
1
2
3
4
 BigInt operator< (BigInt param)
{
//stuff
}
my < operator is defined like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool operator < (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 (unsigned int i=0;i<a.digits.size();i++) {
		if (a.digits.at(i)<b.digits.at(i)) {
			return true;
		} else if (a.digits.at(i)>b.digits.at(i)) {
			return false;
		}
	}
	return false;
}
anyone ? /:
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.
Last edited on
no they don't change the value /:
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?
Last edited on
i had some major updates in my class and i didn't even change the scaleby2 function but it works now somehow so i guess thanks for your help.
Topic archived. No new replies allowed.