Compare not comparing

Pages: 12
Can someone tell me why this is not comparing.. I'm pulling my 3 string of hair out my head.. I'm trying to compare 13.4701.. when this is true.. come out of the loop. I had one guy trying to help, but it didn't fix the problem .
I know doubles have a bunch of numbers after it for example. 13.4701XXXXXXX.. I'm just trying to compare the13.4701 not the x's


1
2
3
4
5
6
7
8
9
10
Ps2 = Ps2 * (1 + Wa2factor / 50);
cout<<"New Ps2: "<<Ps2<<"\n";
double limit = 13.4701;
//cout << "limit=" << setprecision(5) << limit << endl;
cout << "Ps2=" << setprecision(6) << Ps2 << endl;
	if(Ps2 != limit)
		{ //checks to see if Ps2 is 13.470
			Ps2=Ps2 + .001;
			goto mylabelhere;
			cout<<"DONE"<<"\n";

You should never compare floating point numbers you got as a result of computation with == and != unless you know how they exactly work and some compiler and CPU quirks.

in your case you can do:
if( std::fabs(Ps2 - limit) < 0.001 ) //compares up to 4 decimal spaces.

I know doubles have a bunch of numbers after it
Wrong. Those numbers is just unrepresentable with binary, just liky you cannot represent 1/2 in decimals.
Ok.. I'll keep that in mind,
but I don't understand what you did.
if I were to place Ps2 and limit with numbers for example

(13.3301 - 13.4701) < .001
-.14 < .001 True
but it's not 13.4701 I need it to be the same number.

So how do I compare 13.4701 to that number and then moved out of the loop.
Thanks for the help, but I'm trying to understand
fabs: means the absolute value of a number which is a float. Which makes the negative to a positive number, but it's not the same as 13.4701.. so should the < be a like this
( std::fabs(Ps2 - limit) !=< 13.4701 )
Right here should be the answer were it comes out of the loop, but for some reason it keep running in the loop.. After this condition is met it should jump out the loop.

Ps2: 13.4701
Mach2 inside the first section (1): 0.318987
Wa2factor after Wa2check: -3.34034e-005
New Ps2: 13.4701
limit=13.4701
Ps2=13.4701
Wa2: 0.00300045
Mach1: 1
newWa2: 0.00300035
Wa2factor before the loop: -3.34034e-005
so should the < be a like this
( std::fabs(Ps2 - limit) !=< 13.4701 )
No
std::fabs(Ps2 - limit) < 0.001 mensa Ps2 and limit differs less than 0.001

for some reason it keep running in the loop
Show me your code.
I appreciate it very much. I know the code is long.. I put it in bold for the part that we were discussing. Thanks for all your help

#include <iostream>
#include <math.h>
//#include <stdio.h>
#include <string>
#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,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;
Wa2factor = 0.0001;
Vmix = 514.5177;
Vmix = 514.5177;
Wa2 = 0.003;
Npass = 1;
Mach1 = 1;
TtTsRatio = 1.2;
//Ps2 = 13.47;
//Ps3 = 13.4094;

mylabelhere:

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";
double limit = 13.4701;
cout << "limit=" << setprecision(6) << limit << endl;
cout << "Ps2=" << setprecision(6) << Ps2 << endl;
if( std::fabs(Ps2 / limit-1) < 0.0001)
{ //checks to see if Ps2 is 13.4701
Ps2=Ps2 + .0001;
goto mylabelhere;
cout<<"DONE"<<"\n";

}
else
{

if (sqrt(Wa2factor) < 0.00001)
{
/* 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;
cout << "Inside the loop section (2)" <<"\n";
cout << "Wa2check: " <<Wa2check<<"\n";
cout << "Wa3check: " <<Wa3check<<"\n";
cout << "Wa3factor: " <<Wa3factor<<"\n";
cout << "Ps3: " <<Ps3<<"\n";
cout << "Ps2: " <<Ps2<<"\n";
cout <<endl;
}
}
if(Ps3 <= 13.40937)
{
goto mylabel;
}
else
{
sqrt(Wa3factor) < 0.00001;
cout << "counts the number of passes: " <<Npass<<"\n";
cout <<endl;
mylabel2://Section 3
Npass2 = 1;
Npass2 ++; //counts the number of passes
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,(-0.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);
}

if (sqrt(H-1) < 0.00001)
{
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;
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;
}
else
{
goto mylabel2; //goto section 3
}

if ( pow((Wa2factor*Wa2factor)+(Wa3factor*Wa3factor)+(H*H),0.5) < .00001)
{
cout << "counts the number of passes in the second group: " <<Npass2<<"\n";

there is a list of cout statements that I did not paste
}
else
{
goto mylabel;
}

return 0;
}
if( std::fabs(Ps2 / limit-1) < 0.0001) 
if( std::fabs(Ps2 - limit) < 0.0001 )Find the difference
Last edited on
if( std::fabs(Ps2 / limit-1) < 0.0001) Ok I wanted to see if it would come out of the loop by finding the limit as it approaches 1 and it would be less then .0001. That didn't work either.

if( std::fabs(Ps2 - limit) < 0.0001 ) Like you said, this finds the difference and if the difference is less than .0001.

What I'm trying to do is when Ps2 and limit are equal. I want to come out of the loop.
As below.
1
2
3
4
5
6
7
8
9
10
Ps2: 13.4701
Mach2 inside the first section (1): 0.318987
Wa2factor after Wa2check: -3.34034e-005
New Ps2: 13.4701
limit=13.4701
Ps2=13.4701
Wa2: 0.00300045
Mach1: 1
newWa2: 0.00300035
Wa2factor before the loop: -3.34034e-005
are you allowed to use classes?
this thing would be easier to debug if you were.
yes, but I don't remember how to do it in classes. I would have to try to remember how to use classes. I have not wrote code in over 8 years. I'm trying to recall as much as I can. I have about 2 books open and the web. I did it in EXCEl, but I'm trying to take my code and use it in c++. Any pointers would be very helpful..
that's a shame, debugging would be loads faster if the code was easier to skim through.

MiiniPaa has already mentioned bracketing. I also see this:
sqrt(Wa3factor) < 0.00001;

that line isn't doing anything.

looks like calculation of Ps2, Wa2factor and NewPs2 are eventually coming out wrong (have a google for things like -1.IND errors) . None of your if statements ever return true therefore you get into an infinite loop.

goto stuff is very bad.
brokeas wrote:
What I'm trying to do is when Ps2 and limit are equal

You seems to not understand. Due to nature of floating point numbers code (sin(x) == sin(x)) can return false. It is nearly impossible to compare vagues you get as a result of computation. Best you can do is to check if they are within some distance between each other. If you want more precision, decrease your epsilon.

HEre is an article about that: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
and to add another link to MiiNiPaa's excellent link:
http://floating-point-gui.de/errors/comparison/

This caused my company some grief over the last few months when we moved compilers.
Correct that line is not doing anything, the reason is because I'm stuck at the "bold" part of the code. After I get that part to work.. I will fix "that part".

Ps2 does eventaully get to 13.4701 and when it does it so give a true answer when compared to the limit. I think it should come out of the loop then. Concerning Wa2factor, it's is a result.. When I get it I will then use Wa2factor, Wa3factor and Wa4factor will be set to Wafactor.. when the 3 are added together it has to be less than .000001.
For example

Wafactor = (Wa2factor^2 + Wa3factor^2 + Wa4factor^2) < .000001

then I will know what to set the valves to on the air system.
Ps2 does eventaully get to 13.4701

It never comes out of the loop though..

what range (approx) are your input params? i'm inputting 1 to 11 for the input params. are those realistic values?
Last edited on
Correct and I don't know why it doesn't. I thought once it compares the limit and Ps2.. it "should" exit out of the loop.
I think your issue might also be due to pow()

Stuff like this:

Vel2 = pow((2 * 32.174 * 778.17 * 0.24 * (Tt2R - Ts25R)), 0.5);

when i run your code with some test values i have the situation where Ts25R is BIGGER than Tt2R. So you are essentially trying to find the square root of a negative number.
pow() won't like that :)

http://www.cplusplus.com/reference/cmath/pow/
"If the base is finite negative and the exponent is finite but not an integer value, it causes a domain error."
Last edited on
The test values are

200
90
7
.05599
.16
4
51.2
14.7
18.7
.25
.7

The Vel2 should be 363.01177
the guess of Ps2 is Ps2=.9+14.7
Ps2 =13.23

Pt25 = (14.7 + .25 *13.23) / (1 +.25);
Ts25R = 550 * 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);
Pages: 12