Compare statement not working

I'm trying to relearn c++, but seems like I have forgotten a lot. Can anyone tell me why this compare statement is not working. When Ps2 is equal to 13.4701 it should then move to the else statement right?
Thanks in advance

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
 #include <iostream>
#include <math.h>
//#include <stdio.h>
#include <string>

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,H;
int Npass,Npass2;

//Inputs are below
cout << "Enter the Tt1 for the parameters: ";
cin >> Tt1;
cout << "Enter the Tt2 for the parameters: ";
cin >> Tt2;
cout << "Enter the L/D for the parameters: ";
cin >> LD;
cout << "Enter the Nozzle Exit Diameter for the parameters: ";
cin >> D1;
cout << "Enter the D3 for the parameters: ";
cin >> D3;
cout << "Enter the Aratio for the parameters: ";
cin >> Aratio;
cout << "Enter the Pt1 for the parameters: ";
cin >> Pt1;
cout << "Enter the Pt2 for the parameters: ";
cin >> Pt2;
cout << "Enter the Ps5 for the parameters: ";
cin >> Ps5;
cout << "Enter the Kloss for the parameters: ";
cin >> Kloss;
cout << "Enter the Cps for the parameters: ";
cin >> Cps;
//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;
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;
Wa1 = flowparam * Pt1 * A1 / pow((53.353 * Tt1R / 1.4 / 32.171), 0.5);
DP25 = Ps5 - Pt2;
Wa2 = 0.1 * Wa1;
Ps2 = 0.9 * Pt2;
Ps3 = Ps2;
mylabelhere:
Vmix = 0.3 * pow((1.4 * 53.35 * 32.174 * Tt2R), 0.5);
Wa2 = 0.003;
//Ps2 = 13.47;
Wa2factor = 0.0001;
//Ps3 = 13.4094;
Vmix = 514.5177;
Npass = 1;
Mach1 = 1;
TtTsRatio = 1.2;
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(Ps2 != 13.4701)
		{ //checks to see if Ps2 is 13.470
			Ps2=Ps2 + .0001;
			goto mylabelhere;
			
			
		}
	else


here is the output I'm getting

New Ps2: 13.4698
Wa2: 0.003
Mach1: 1
newWa2: 0.0030003
Wa2factor before the loop: 0.0001

Ps2: 13.4699
Mach2 inside the first section (1): 0.319004
Wa2factor after Wa2check: 5.88747e-005
New Ps2: 13.47
Wa2: 0.003
Mach1: 1
newWa2: 0.0030003
Wa2factor before the loop: 0.0001

Ps2: 13.4701
Mach2 inside the first section (1): 0.318988
Wa2factor after Wa2check: 1.63448e-005
New Ps2: 13.4701
Wa2: 0.003
Mach1: 1
newWa2: 0.0030003
Wa2factor before the loop: 0.0001

Ps2: 13.4702
Mach2 inside the first section (1): 0.318973
Wa2factor after Wa2check: -2.19816e-005
New Ps2: 13.4702
Wa2: 0.003
Mach1: 1
newWa2: 0.0030003
Wa2factor before the loop: 0.0001

Ps2: 13.4703
Mach2 inside the first section (1): 0.31896
Wa2factor after Wa2check: -5.65195e-005
New Ps2: 13.4702
Wa2: 0.003
Mach1: 1
newWa2: 0.0030003
Wa2factor before the loop: 0.0001

Ps2: 13.4703
Mach2 inside the first section (1): 0.318949
Wa2factor after Wa2check: -8.7643e-005
New Ps2: 13.4703
Wa2: 0.003
Mach1: 1
newWa2: 0.0030003
Wa2factor before the loop: 0.0001

Ps2: 13.4704
Mach2 inside the first section (1): 0.318938
Wa2factor after Wa2check: -0.000115689
New Ps2: 13.4704
Understand that doubles are approximations. There may be some residual to the right in ps2 or the literal 13.4701 may not convert exactly to a binary representation.

I would suggest setting a double variable (e.g. limit) to 13.4701 then displaying both ps2 and that variable with a high degree of precision to see what they really contain.
1
2
cout << "limit=" << setprecision(10) << limit << endl;
cout << "ps2=" << setprecision(10) << ps2 << endl;
So let me ask.. is that the reason why it's not "jumping" out of the loop? I'm going to try what you suggested.
Thanks
AbstractionAnon,

I did what you suggested, it matched the number, but it didn't come out the loop.
1
2
3
4
5
6
7
8
double limit = 13.4701;
cout << "limit=" << setprecision(7) << limit << endl;
cout << "Ps2=" << setprecision(7) << Ps2 << endl;
	if(Ps2 != limit)
		{ //checks to see if Ps2 is 13.470
			Ps2=Ps2 + .00001;
			goto mylabelhere;
			



Ps2: 13.47008
Mach2 inside the first section (1): 0.3189841
Wa2factor after Wa2check: 6.362809e-006
New Ps2: 13.47009
limit=13.4701
Ps2=13.47009
Wa2: 0.003
Mach1: 1
newWa2: 0.0030003
Wa2factor before the loop: 0.0001

Ps2: 13.4701
Mach2 inside the first section (1): 0.3189825
Wa2factor after Wa2check: 2.06259e-006
New Ps2: 13.4701
limit=13.4701
Ps2=13.4701
Wa2: 0.003
Mach1: 1
newWa2: 0.0030003
Wa2factor before the loop: 0.0001

After it should have came out of the loop

Ps2: 13.47011
Mach2 inside the first section (1): 0.318981
Wa2factor after Wa2check: -1.812376e-006
New Ps2: 13.47011
limit=13.4701
Ps2=13.47011
Wa2: 0.003
Mach1: 1
newWa2: 0.0030003
Wa2factor before the loop: 0.0001
I tried 10 as well and they compare, but it never came out of the loop. So that tells me they didn't compare unless it a flaw some where that I'm missing
Topic archived. No new replies allowed.