issue with if statement

hello with this code the if statement get completely ignored, why is that? If i was not in increments of .1 and was just 1 while still being double it works but in this form when it approaches 9 the if statement does not work.

thank you for your time

1
2
3
4
5
6
7
8
9
10
11
12
13
  double a = .1;
	
	cout << "Thinking: ";
	for(double i = 0 ; i < 10; i+= .1){
		cout << i << endl;
		if((pow(a,2) + pow(i,2)) == 400)
			cout << a << " + " << i << endl;
		if(i == 9){
			cout << " i reset" << endl;
			i = 0;
		a = a + .1;
		}
	}
Make i an integer. Create a different double and assign the value of i to that double at the start of the for loop, before the if statements.
i believe i am not understanding what you said. If i make a new double and assign the value of i which is now an integer and can't be in decimal form to it won't the double just be the same i value?
Basically the thing is you cant use the comparison operator ( == ) like that for any floating-point number. THey are not stored the same as integers.
9 could really be equal to 9.000000000000000000001 or even 8.999999999999999999 to compare you would have to put a range

like i < 9.0000000001 && i > 8.99999999999
or something along those lines.

basically what mats is saying is to do something like this I am guessing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for( int i = 0; i < 10; ++i )
{
    for( double j = 0.0; j < 1; j += .1 )
    {
        cout << j << endl;
        if( (pow( a , 2 ) + pow( j , 2 ) ) == 400 )
            cout << a << " + " << j << endl;
     }
     if( i == 9 )
     {
         cout << "i reset" << endl;
         i = 0;
         a += .1;
     }
}


though to be honest I am not sure why you are resetting i when it is equal to 9 if you have the loop running till 10.
sorry i guess i should of explained the logic.

The idea is to find two numbers that when they are squared then added they will equal to 400. My thinking was to have a number that would scroll through testing possible answers example 5.2^2 + .1^2, 5.3^2 + .1^2, 5.4^2 + .1^2.... when at 9 the .1 would change to .2 and the cycle would continue over 5.2^2 + .2^2

Why not just do this -> i * i <- to square the number? Or even better, just square root 400? Also, if you finding the squares of only square numbers, you only need integers and not any other type.
hes tyring to find
x2 + y2 = 202

basically it could have a trillion of results.

x = 0
y = 20

pretty much you can plug and chug this formula

a2 + b2 = c2
( Pythagorean theorem )

which means you pick a number for b

and do

a = sqrt( 20 - b );

essentially pow and i*i is the same thing as i am still learning c++ i like to try different things. Does pow take more time to process then i * i ? of course on this level the difference is irrelevant but would be good to know. Also about just using integers yes it would make things simpler but same thing i would like to try different things.

pow uses a very complex formula it is something like using log's and natural logs. its not like i * i....doesn't work that way with rational numbers.
interesting i will look that up i was under the impression it was the same thing. I knew about the a = sqrt approach but i was thinking of a different way because there is more to what i have to do but that might actually be the better solution since working with double using pow or I*i i don't get a solution. i wish i could of gotten it to work lol.
Topic archived. No new replies allowed.