Compare not comparing

Pages: 12
Ok I switched the code up.. Only problem now that I have no idea of fixing is this error -1.#IND which I think is an a rounding error of some sort. Does Anyone have any idea how to fix the -1.#IND error?

Here is the code rearranged
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
mylabelhere:

cout<<"Wa1: "<<Wa1<<"\n";
Vmix = 0.3 * pow((1.4 * 53.35 * 32.174 * Tt2R), 0.5);
PtPsRatio = pow(TtTsRatio, 3.5);
Ts1R = Tt1R / TtTsRatio;
Ps1 = Pt1 / PtPsRatio;
Rho1 = 144 * Ps1 / 53.353 / Ts1R;
Vel1 = Mach1 * pow((1.4 * 32.174 * 53.353 * Ts1R),0.5);
Wa1check = Rho1 * A1 * Vel1 / 144;
DP15 = Ps5 - Pt1;
cout<<"Wa2: "<<Wa2<<"\n";
cout<<"Mach1: "<<Mach1<<"\n";
Wa2 = Wa2 * (1 + Wa2factor);
double newWa2 = Wa2;
cout<<"newWa2: "<<newWa2<<"\n";
cout<<"Wa2factor before the loop: "<<Wa2factor<<"\n";
cout <<endl;

mylabel://Section 1
	
Npass ++; //counts the number of passes

cout << "Ps2: " <<Ps2<<"\n";
Tt4 = (Wa1 * Tt1R + Wa2 * Tt2R) / (Wa2 + Wa1);
Wa3 = Wa1 + Wa2;
Pt25 = (Pt2 + Kloss * Ps2) / (1 + Kloss);
Ts25R = Tt2R * pow((Ps2 / Pt25), (1 / 3.5));
Rho2 = 144 * Ps2 / 53.353 / Ts25R;
Vel2 = pow((2 * 32.174 * 778.17 * 0.24 * (Tt2R - Ts25R)), 0.5);
Mach2 = Vel2 / pow((1.4 * 32.174 * 53.353 * Ts25R), 0.5);
cout << "Mach2 inside the first section (1): " <<Mach2<<"\n";
Wa2check = Rho2 * A25 * Vel2 / 144;
Wa2factor = Wa2check / Wa2 - 1;
cout<<"Wa2factor after Wa2check: "<<Wa2factor<<"\n";
Ps2 = Ps2 * (1 + Wa2factor / 50);
cout<<"New Ps2: "<<Ps2<<"\n";
	if(std::fabs(Wa2factor) < 0.00001)
		{ 
			cout<<"Section 2"<<"\n";	
			mylabel2: //Section 2
			Pt1Ps3Ratio = Pt1 / Ps3;
			double x = Pt1Ps3Ratio;
			double y = pow(x,1/3.5)-1;
			double z = 5 * pow(y,0.5);
			Mach3pri = z;
			A3Astar = pow(((1 + 0.2 * Mach3pri*Mach3pri) / 1.2),3 / Mach3pri);
			Aprimary3 = A3Astar * A1;
			Asec3 = A3 - Aprimary3;
			PtPsSec = Pt25 / Ps3;
			double xx = PtPsSec;
			double yy = pow(xx,1/3.5)-1;
			double zz = 5 * pow(yy,0.5);
			Mach3sec = zz;
			TtTs3Sec = pow(PtPsSec, (1 / 3.5));
			Ts3Sec = Tt2R / TtTs3Sec;
			Rho3 = 144 * Ps3 / 53.353 / Ts3Sec;
			Vel3 = Mach3sec * pow((1.4 * 32.174 * 53.353 * Ts3Sec), 0.5);
			Wa2check = Rho3 * Asec3 * Vel3 / 144;
			Wa3check = Rho3 * Asec3 * Vel3 / 144 + Wa1;
			Wa3factor = (Wa3check) / Wa3 - 1;
			Ps3 = Ps3 * (1 + Wa3factor / 10);
			double newPs3 = Ps3;
			cout << "newPs3: "<<newPs3<<"\n";
			cout <<endl;

			if( std::fabs(Wa3factor) < 0.00001)
			{
				cout << "Inside the loop section (2)" <<"\n";
				cout << "Wa2check: " <<Wa2check<<"\n";
				cout << "Wa2factor: " <<Wa2factor<<"\n";
				cout << "Wa3check: " <<Wa3check<<"\n";
				cout << "Wa3factor: " <<Wa3factor<<"\n";
				cout << "Ps3: " <<Ps3<<"\n";
				cout << "Ps2: " <<Ps2<<"\n";
				cout <<endl;
			}
			
			else
			{
				Wa3=Wa3 + .0001;
				goto mylabel2;
				cout << "DONE with second part"<<"\n";
			}
	    }
	else
		{
		  Wa2=Wa2 + .0001;
		  goto mylabelhere;
		}
			
-1.#IND is negative indefinite NaN. It is caused by incorrect operations like adding positive and negative infinities together, -0/0 or taking square root or fractional power of negative number. I would check input of your pow function.
I rewrote the code and now I don't have any errors how ever. I notice some of the values are not changing.. for example Vel3. I "watched the value and it doesn't change. At first I started noticing it with other numbers.. I think Like NiiNiPaa suggested the square root and power could be the problem. I'm not too sure about fitting that. I guess I could break the problem down to where I would not have to use it on some equations.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{

double Pt1, Tt1, Tt1R,Tt2R,Ts25R,Tt4,Ts5,Tt2,Tt5,Ps1,Ps2,Ps3;
double Ps5,Pt2,Pt25,D1, D3,D5,DP25, Kloss, LD, Aratio, Cps,gamma1,flowparam;
double Length, A1,/*A4*/A3,A25,Wa1, Wa2,Wa2factor,Wa3factor, Vmix,PtPsSec,Pmixin,TtTsRatio;
double Mach1,Mach2,/*Mach3,*/Mach4,Mach3sec,Mach5,Cpcalc,Ps5calc,Mach3pri,Ts3Sec;
double A5,PtPsRatio,Pt1Ps3Ratio,A3Astar,A5Astar,mu,Vav,Accel,Blockage75;
double Ts1R,Rho1,Rho2,Rho3,Rho4,Rho5,Vel1,Vel2,Vel3,Vel5;
double Wa1check,Wa2check,Wa3check,Wa4check,Wa4factor,DP15,Wa3,Asec3,TtTs3Sec,Aprimary3;
double Tsmixed,Psmixed,DPerror,Dpsec,Pt5Ps5,lambda,Re,FfAt,Pexit,DPtPsdiff,Ptmixed,Ptideal;
int Npass;
double Wafactor = 0;
int NpassCount = 0;

//Inputs are below
Tt1 = 200;
Tt2 = 90;
LD = 7;
D1 = .05599;
D3 = .16;
Aratio = 4;
Pt1 = 51.2;
Pt2 = 14.7;
Ps5 = 18.7;
Kloss = .25;
Cps = .7;
//Inputs End


Tt1R = Tt1 + 460;
Tt2R = Tt2 + 460;
gamma1 = 0.000000000014 * (Tt1R*Tt1R*Tt1R) - 0.00000005575 * (Tt1R*Tt1R) + 0.00001864338 * Tt1R + 1.402579277865;
Mach1 = 1;
flowparam = Mach1 / pow((1 + (gamma1 - 1) / 2 * Mach1*Mach1), ((gamma1 + 1) / 2 / (gamma1 - 1)));
A1 = 3.14159 / 4 * D1*D1;
Wa1 = flowparam * Pt1 * A1 / pow((53.353 * Tt1R / 1.4 / 32.171), 0.5);
Length = LD * D3;
A3 = 3.14159 / 4 * D3 * D3;
A25 = 3.14159 / 4 * (D3 * D3 - D1 * D1);
D5 = pow((Aratio * 4 * A3 / 3.14159), 0.5);
A5 = 3.14159 / 4 * D5 * D5;
DP25 = Ps5 - Pt2;
DP15 = Ps5 - Pt1;
Wa2 = 0.1 * Wa1;
Ps2 = 0.9 * Pt2;
Ps3 = Ps2;
Vmix = 0.45 * pow((1.4 * 53.35 * 32.174 * Tt2R),.5);
Wa2factor = 0.2;
Wa3factor = 0.2;
Wa4factor = 0.2;
Vmix = 514.5177;
Wa2 = 0.003;
Npass = 1;
Mach1 = 1;
TtTsRatio = 1.2;
Ps2 = 13.47;
Ps3 = 13.4094;
PtPsRatio = pow(TtTsRatio, 3.5);
Ts1R = Tt1R / TtTsRatio;
Ps1 = Pt1 / PtPsRatio;
Rho1 = 144 * Ps1 / 53.353 / Ts1R;
Vel1 = Mach1 * pow((1.4 * 32.174 * 53.353 * Ts1R),0.5);
Wa1check = Rho1 * A1 * Vel1 / 144;
			
do
{
	
	 NpassCount = NpassCount++;
    		
			
			Wa2 = Wa2 * (1 + Wa2factor);
			Tt4 = (Wa1 * Tt1R + Wa2 * Tt2R) / (Wa2 + Wa1);
			Wa3 = Wa1 + Wa2;
			Pt25 = (Pt2 + Kloss * Ps2) / (1 + Kloss);
			Ts25R = Tt2R * pow((Ps2 / Pt25), (1 / 3.5));
			Rho2 = 144 * Ps2 / 53.353 / Ts25R;
			Vel2 = pow((2 * 32.174 * 778.17 * 0.24 * (Tt2R - Ts25R)), 0.5);
			Mach2 = Vel2 / pow((1.4 * 32.174 * 53.353 * Ts25R), 0.5);
			Wa2check = Rho2 * A25 * Vel2 / 144;
			Wa2factor = Wa2check / Wa2 - 1;
			Ps2 = Ps2 * (1 + Wa2factor / 50);
			Wafactor = pow((Wa2factor*Wa2factor + Wa3factor*Wa3factor + Wa4factor*Wa4factor),.5);
			//Wa2 = Wa2 + .001;// to increase Wa2
			if(Wafactor < .0000001)
				{
					goto mylabelPrint;
				}
			Pt1Ps3Ratio = Pt1 / Ps3;
			double x = Pt1Ps3Ratio;
			double y = pow(x,1/3.5)-1;
			double z = 5 * pow(y,0.5);
			Mach3pri = z;
			A3Astar = pow(((1 + 0.2 * Mach3pri*Mach3pri) / 1.2),3 / Mach3pri);
			Aprimary3 = A3Astar * A1;
			Asec3 = A3 - Aprimary3;
			PtPsSec = Pt25 / Ps3;
			double xx = PtPsSec;
			double yy = pow(xx,1/3.5)-1;
			double zz = 5 * pow(yy,0.5);
			Mach3sec = zz;
			TtTs3Sec = pow(PtPsSec, (1 / 3.5));
			Ts3Sec = Tt2R / TtTs3Sec;
			Rho3 = 144 * Ps3 / 53.353 / Ts3Sec;
			Vel3 = Mach3sec * pow((1.4 * 32.174 * 53.353 * Ts3Sec), 0.5);
			Wa2check = Rho3 * Asec3 * Vel3 / 144;
			Wa3check = Rho3 * Asec3 * Vel3 / 144 + Wa1;
			Wa3factor = (Wa3check) / Wa3 - 1;
			Ps3 = Ps3 * (1 + Wa3factor / 10);
			double newPs3 = Ps3;
			Wafactor = pow((Wa2factor*Wa2factor + Wa3factor*Wa3factor + Wa4factor*Wa4factor),.5);
			if(Wafactor < .0000001)
				{
					goto mylabelPrint;
				}

			Pmixin = (Ps2 * A25 + Ps1 * A1) / A3;
			Vav = (Vel3 + Vmix) / 2;
			mu = 1.26 * pow(10.0,-5);
			Re = Rho2 * Vav * D3 / 12 / mu;
			lambda = 0.3164 * pow(Re, -0.25);
			FfAt = lambda * LD * Rho2 * Vav*Vav / 2 / 144 / 32.174;
			Accel = (Wa2 * Vel2 + Wa1 * Vel1 - Wa3 * Vmix) / A3 / 32.174;
			Psmixed = Pmixin - FfAt + Accel;
			Tsmixed = Tt4 - (Vmix*Vmix) / 2 / 32.174 / 778.17 / 0.24;
			Rho4 = 144 * Psmixed / 53.353 / Tsmixed;
			Wa4check = Rho4 * A3 * Vmix / 144;
			Wa4factor = Rho4 * A3 * Vmix / 144 / Wa3;
			Vmix = Vmix * pow(Wa4factor+1,(-.25));
			//H = pow(Wa4factor,(-0.25));
			Mach4 = Vmix / pow((1.4 * 32.174 * 53.353 * Tsmixed), 0.5);
			Ptmixed = Psmixed * pow((Tt4 / Tsmixed), 3.5);
			Wafactor = pow((Wa2factor*Wa2factor + Wa3factor*Wa3factor + Wa4factor*Wa4factor),.5);
	   		if(Wafactor < .0000001)
				{
					goto mylabelPrint;
				}					
			DPtPsdiff = Ptmixed - Psmixed;
			Pexit = Psmixed + Cps * DPtPsdiff;
			Ps5calc = Pexit;
			Ptideal = Ptmixed;
			Tt5 = Tt4;
			Ts5 = Tt5 * pow((Ps5calc / Ptideal),(1 / 3.5));
			Rho5 = 144 * Ps5calc / 53.352 / Ts5;
			Vel5 = pow((2 * 32.174 * 778.17 * 0.24 * (Tt5 - Ts5)), 0.5);
			Mach5 = Vel5 / pow((1.4 * 32.174 * 53.353 * Ts5), 0.5);
			A5Astar = pow(((1 + 0.2 * Mach5*Mach5) / 1.2), 3) / Mach5;
			Blockage75 = (1 - 144 * Wa3 / Rho5 / A5 / Vel5);
			Blockage75 = (1 - 144 * Wa3 / Rho5 / A5 / Vel5) * 0.75;
			A5Astar = (1 - Blockage75) / (1 - Blockage75 / 0.75) * A5Astar;

			/*415 Mach5 = Mach5 * (1 + (Mach5 / Mach5trial - 1) / 50)
			Mach5trial = ((1 + 0.2 * Mach5 ^ 2) / 1.2) ^ 3 / A5Astar
			If ((Mach5 / Mach5trial - 1) ^ 2 < 0.00001) Then GoTo 480
			*/

			Mach5 = pow(((1 + 0.2 * Mach5*Mach5) / 1.2), 3) / A5Astar;
			Pt5Ps5 = (1 + 0.2 * pow((Mach5*Mach5), 3.5));
			Ps5calc = Ptideal / Pt5Ps5;
			Cpcalc = (Ps5calc - Psmixed) / (Ptmixed - Psmixed);
			Dpsec = Ps5calc - Pt2;
			DPerror = DP25 / Dpsec;

			Wafactor = pow((Wa2factor*Wa2factor + Wa3factor*Wa3factor + Wa4factor*Wa4factor),.5);
}
while (Wafactor >  .0000001);
		{ 
			cout << "Count: " <<NpassCount<<"\n";


Can anyone tell me why my do while loop is not acting "right".
I'm going to take a guess here and say the:
while (Wafactor < .0000001)
is wrong..
because it runs threw the loop 1 time. and wafactor is 0.952972
So it should keep looping right?

Wafactor: 0.952972
Vmix: 438.983
DP25: 4
Ps2: 13.4251
Wa1: 0.0026106
Wa2: 0.0036
A25: 0.017644
A3: 0.0201062
A1: 0.00246213
Length: 1.12
while (Wafactor < .0000001)
[...]
because it runs threw the loop 1 time. and wafactor is 0.952972
So it should keep looping right?
0.952972 < .0000001 is false so it stops iterating.
ok..I'm drawing a blank now..

While (Wafactor < .0000001)
{ statement}
Is half way right.. so what would make it right/True.
when I think about it
Wafactor > .0000001 seems to be the correct way..

iterate until Wafactor is greater than .0000001. So that would mean stay in the loop until Wafactor is smaller than .0000001 Is that correct?
I think you need to put the code aside for a moment and actually think about the logic of what you want to achieve.

You seem to be trying to fix this using trial and error (e.g. 'maybe i should change '<' to '>').

What does 'is halfway right' mean?

i'd grab a pen and paper and just scribble down (in steps) the logic of what you want to do.

Leaving the code alone for a period of time helps me a lot.
I'll try that, because it's driving me up the wall.. When I think I have it right and I don't get the results I want I second guess myself and then I start to try everything to see if i get the result i want.

By the way I check the steps of the do while loop.. and from my understanding
The loop while execute once, if the condition is false it will come out of the loop.
.952972 < .0000001 So that's why I felt I have the sign wrong. My logic is: if it is true loop until it's false.
The loop while execute once, if the condition is false it will come out of the loop.


This is the main difference between while loop and do-while loop.
In while loop
1
2
while(condition) 
{statements}

the condition is verified before entering the block. If the condition is false the block will be skipped.

The do-while loop
1
2
do {statements}
while(condition);

runs at least one time the block before checking the condition. After the 1st run, the condition will be verified and if true will run again.

If I remember correctly the repeat (...) until <condition> from Pascal/Delphi, this was actually the equivalent of while-loop (not DO-WHILE loop). I hope not to be wrong (last of my Pascal/Delphi programs were done about 10 years or so....).

Basically, a
for(int i = 0; i < 10; i++);
can be rewrite as:
1
2
int i = 0;
while(i++ < 10) {;}

or
1
2
3
 int i = 0;
do {;} 
while(++i < 10);

The number of iterations is the same.

Rgds.
Based on the logic of the do while loop.. I had the less than sign wrong.
It should have been Wafactor > .00000001 since I need it to loop until it results in a false test.

Now my next problem is to make sure everything is being calculated.
Topic archived. No new replies allowed.
Pages: 12