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
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.
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
//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
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;
}
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
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.
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.
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
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 :)