Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EQUALITY NOT RECOGNIZED-CODE INCLUDED

Status
Not open for further replies.

ankoump

Programmer
May 29, 2002
11
DE
I have written an if statement which states that when x == y a task should be carried out.
Now my values are in double and are decimal values the output is that x = 1.51675 and y= 1.51675 so they are the same. but when the statement is not executed and when i subtract y from x just to test the output is very strange its like 2.356exp-09

How can i stop this?

see code below: you'll see that it does not recognize the equlaity:

#include <fstream.h>
#include <iostream.h>
#include <math.h>
//ofstream fout(&quot;NikeSurvives1.rtf&quot;,ios::eek:ut);
ofstream fout(&quot;NikeSurvives1.rtf&quot;,ios::eek:ut);


//Enumerations

enum OCCUPANCY { Dwelling,Office,Shop,HotelReception,HotelBedroom,PictureGallery};
enum HEATOFCOMBUSTION { Timber, PolyvinylChloride,PolyUrethane_flexible,PolyUrethane_rigid,Polystyrene,Polypropylene,NotSure};
//Class Declarations

class Dimensions //class for taking dimensions //declaration + some implementations
{
public:
Dimensions(){}; //constructor
~ Dimensions(){}; //destructor
double GetOriginLenght() { return OLenght;} //Accessors
double GetOriginWidth(){ return OWidth;}
double GetOriginHeight(){ return OHeight;}
double GetOriginOpHeight() { return OoHeight;}
double GetOriginOpWidth(){ return OoWidth;}

double GetCorridorLenght(){ return CorrLenght;}
double GetCorridorWidth(){ return CorrWidth;}
double GetCorridorHeight(){ return CorrHeight;}
double GetCorridorOpHeight(){ return CoHeight;}
double GetCorridorOpWidth(){ return CoWidth;}

double GetAlpha() {return ItsAlpha;}
double GetTime() {return ItsTime;}
double GetOldQp() {return ItsOldQp;}
double GetTp() {return ItsTp;}
double GetTs() {return ItsTs;}
double GetZs() {return ItsZs;}
double GetZp() {return ItsZp;}
double GetUpperLayerVolume() {return ItsUpperLayerVolume;}
double GetCp() {return ItsCp;}
double GetHSmoke() {return ItsHSmoke;}


double GetQp();
double GetQs();
double GetMassFlowRatePlume();

void SetOriginDimensions( double SOLenght,double SOWidth,double SOHeight);
void SetOriginOpDimensions(double SOoHeight,double SOoWidth);

void SetCorridorDimensions(double SCorrLenght,double SCorrWidth,double SCorrHeight);
void SetCorridorOpDimensions(double SCoHeight,double SCoWidth);

void SetAlpha(double SAlpha);
void SetTime(double STime);
void SetOldQp(double TheOldQp);
void SetTp(double TheTp);
void SetTs(double TheTs);
void SetZs(double TheZs);
void SetZp(double TheZp);
void SetUpperLayerVolume(double TheUpperLayerVolume);
void SetCp( double TheCp);
void SetHSmoke (double TheHSmoke);

private:
double OLenght,OWidth,OHeight,OoHeight,OoWidth,CorrLenght,CorrWidth,CorrHeight,CoHeight,CoWidth;
double ItsAlpha,ItsTime,ItsTp,ItsTs,ItsZs,ItsZp;
double ItsUpperLayerVolume,ItsCp,ItsHSmoke,ItsOldQp;

};//close class Dimensions


double DoOccupancy();
double AlphaSelection(double Selection1);
double DoAnalyseZ(Dimensions &rAdjacent);
void DoIterations(Dimensions &rAdjacent);


double Dimensions::GetQp()
{
return (GetAlpha() * (pow(GetTime(),2)));
}

double Dimensions::GetQs()
{
double T=0;

while (T == 0)
{
if (GetTime() == 0)
{
T = (GetAlpha() * (pow(GetTime(),2)));
break;
}

if (GetTime() > 0)
{
T = GetOldQp() + (GetAlpha() * (pow(GetTime(),2)));
break;
}
}

SetOldQp(T);
GetOldQp();
return T;
}


double Dimensions::GetMassFlowRatePlume()
{
return ((0.071) * (pow(GetQp(),0.3333)) * (pow(GetZp(),1.6666)));
}




void Dimensions::SetOriginDimensions(double SOLenght,double SOWidth,double SOHeight)
{
OLenght = SOLenght;

OWidth = SOWidth;

OHeight = SOHeight;
}

void Dimensions::SetOriginOpDimensions(double SOoHeight,double SOoWidth)
{
OoHeight = SOoHeight;
OoWidth = SOoWidth;
}




void Dimensions::SetCorridorDimensions(double SCorrLenght,double SCorrWidth,double SCorrHeight)
{
CorrLenght = SCorrLenght;
CorrWidth = SCorrWidth;
CorrHeight = SCorrHeight;
}

void Dimensions::SetCorridorOpDimensions(double SCoHeight,double SCoWidth)
{
CoHeight = SCoHeight;
CoWidth = SCoWidth;
}


void Dimensions::SetAlpha(double SAlpha)
{
ItsAlpha = SAlpha;

}


void Dimensions::SetTime(double STime)
{
ItsTime = STime;
}


void Dimensions::SetTp(double TheTp)
{
ItsTp = TheTp;
}

void Dimensions::SetTs(double TheTs)
{
ItsTs = TheTs;
}


void Dimensions::SetZs(double TheZs)
{
ItsZs = TheZs;
}

void Dimensions::SetZp(double TheZp)
{
ItsZp = TheZp;
}

void Dimensions::SetUpperLayerVolume(double TheUpperLayerVolume)
{
ItsUpperLayerVolume = TheUpperLayerVolume;
}

void Dimensions::SetCp( double TheCp)
{
ItsCp = TheCp;
}

void Dimensions::SetHSmoke( double TheHSmoke)
{
ItsHSmoke = TheHSmoke;
}

void Dimensions::SetOldQp(double TheOldQp)
{
ItsOldQp = TheOldQp;
}


double DoOccupancy()//Function To point out occupancies and types of fires associated with them ref.TM19
{
double Selection;
cout << &quot;Please Select the type of occupancy to be analysed from the selection below: &quot;<<endl;
fout << &quot;Please Select the type of occupancy to be analysed from the selection below: &quot;<<endl;
cout << &quot;(0) DWELLING (medium fire),(1) OFFICE (medium fire), (2) SHOP (Ultrafast Fire), (3) HOTEL (medium fire)(4) HOTEL BEDROOM (medium fire), (5) PICTURE GALLERY (slow fire)&quot; <<endl;
fout << &quot;(0) DWELLING (medium fire),(1) OFFICE (medium fire), (2) SHOP (Ultrafast Fire), (3) HOTEL (medium fire)(4) HOTEL BEDROOM (medium fire), (5) PICTURE GALLERY (slow fire)&quot; <<endl;
cin >> Selection;
fout << Selection <<endl;

if ((Selection!= Dwelling) && (Selection!= Office) && (Selection != Shop) && (Selection != HotelReception) && (Selection != HotelBedroom) && (Selection != PictureGallery))
{
cout << &quot;\t** Invalid Choice **, Please Reselect!&quot; <<endl;
//fout << &quot;\t** Invalid Choice **, Please Reselect!&quot; <<endl;
Selection = DoOccupancy();
}
return Selection;
}

double AlphaSelection(double Selection1) //Function that allocates the alpha values according to occupancy selection
{


double AlphaValue;
while( Selection1 == Dwelling||Office||Shop ||HotelReception||HotelBedroom||PictureGallery)
{

if (Selection1 == Dwelling)
{
//cout <<&quot;I'm here0&quot; <<endl;
AlphaValue = 0.0117;
break;
}

if (Selection1 == Office)
{
//cout <<&quot;I'm here1&quot; <<endl;
AlphaValue = 0.0117;
break;
}

if (Selection1 == Shop)
{
//cout <<&quot;I'm here2&quot; <<endl;
AlphaValue = 0.0469;
break;
}

if (Selection1 == HotelReception)
{
//cout <<&quot;I'm here3&quot; <<endl;
AlphaValue = 0.0117;
break;
}

if (Selection1 == HotelBedroom)
{
//cout <<&quot;I'm here4&quot; <<endl;
AlphaValue = 0.0117;
break;
}

if (Selection1 == PictureGallery)
{
// cout <<&quot;I'm here&quot; <<endl;
AlphaValue = 0.0029;
break;
}

}
return AlphaValue;
}

double DoAnalyseZ(Dimensions &rAdjacent)
{
double SmokeTemp,VolumeOfLayer,HSmoke,ZSmoke;
rAdjacent.GetMassFlowRatePlume();


if (rAdjacent.GetTime() == 0)
{
/*cout << &quot;Qs is &quot; << rAdjacent.GetQs()<<endl;
fout << &quot;Qs is &quot; << rAdjacent.GetQs()<<endl;
cout << &quot;m is &quot; << rAdjacent.GetMassFlowRatePlume() << endl;
fout << &quot;m is &quot; << rAdjacent.GetMassFlowRatePlume() << endl;*/
SmokeTemp = (rAdjacent.GetQs() /( rAdjacent.GetMassFlowRatePlume()*1) + 273);
rAdjacent.SetTs(SmokeTemp);
rAdjacent.GetTs();
VolumeOfLayer = ((rAdjacent.GetMassFlowRatePlume() * SmokeTemp)/352);
rAdjacent.SetUpperLayerVolume(VolumeOfLayer);
rAdjacent.GetUpperLayerVolume();
cout << &quot;Volume is &quot; << rAdjacent.GetUpperLayerVolume()<<endl;
HSmoke = (rAdjacent.GetUpperLayerVolume()/ (rAdjacent.GetOriginLenght() * rAdjacent.GetOriginWidth()));
rAdjacent.SetHSmoke(HSmoke);
rAdjacent.GetHSmoke();
ZSmoke = rAdjacent.GetOriginHeight() - rAdjacent.GetHSmoke();
rAdjacent.SetZs(ZSmoke);
rAdjacent.GetZs();

cout << &quot;Zp is &quot; << rAdjacent.GetZp() << &quot; and Zs is &quot; << ZSmoke << &quot;at zero&quot;<<endl;
fout << &quot;Zp is &quot; << rAdjacent.GetZp() << &quot; and Zs is &quot; << ZSmoke << &quot;at zero&quot;<<endl;

}


while (rAdjacent.GetTime() >0)
{
/*cout << &quot;Qs is &quot; << rAdjacent.GetQs()<<endl;
fout << &quot;Qs is &quot; << rAdjacent.GetQs()<<endl;
cout << &quot;m is &quot; << rAdjacent.GetMassFlowRatePlume() << endl;
fout << &quot;m is &quot; << rAdjacent.GetMassFlowRatePlume() << endl;*/
SmokeTemp = (rAdjacent.GetQs() /( rAdjacent.GetMassFlowRatePlume()*1) + 273);
rAdjacent.SetTs(SmokeTemp);
rAdjacent.GetTs();
VolumeOfLayer = ((rAdjacent.GetMassFlowRatePlume() * SmokeTemp)/352);
rAdjacent.SetUpperLayerVolume(VolumeOfLayer);
rAdjacent.GetUpperLayerVolume();
HSmoke = (rAdjacent.GetUpperLayerVolume()/ (rAdjacent.GetOriginLenght() * rAdjacent.GetOriginWidth()));
rAdjacent.SetHSmoke(HSmoke);
rAdjacent.GetHSmoke();
ZSmoke = rAdjacent.GetOriginHeight() - rAdjacent.GetHSmoke();
rAdjacent.SetZs(ZSmoke);
rAdjacent.GetZs();

/*cout << &quot;Zp is &quot; << rAdjacent.GetZp() << &quot; and Zs is &quot; << rAdjacent.GetZs() << &quot;so i'm setting1&quot;<<endl;
fout << &quot;Zp is &quot; << rAdjacent.GetZp() << &quot; and Zs is &quot; << rAdjacent.GetZs() << &quot;so i'm setting1&quot;<<endl;
*/
if (rAdjacent.GetZs() == rAdjacent.GetZp())
{
cout << &quot;Zp is &quot; << rAdjacent.GetZp() << &quot; and Zs is &quot; << rAdjacent.GetZs() << &quot;so i'm setting&quot;<<endl;
fout << &quot;Zp is &quot; << rAdjacent.GetZp() << &quot; and Zs is &quot; << rAdjacent.GetZs() << &quot;so i'm setting&quot;<<endl;

break;
}

if (rAdjacent.GetZs() != rAdjacent.GetZp())
{
DoIterations(rAdjacent);

break;
}
}
return ZSmoke;
}

void DoIterations(Dimensions &rAdjacent)
{
double NewZ = rAdjacent.GetZs(),Check =1,SmokeTempHere,VolumeOfLayerHere,HSmokeHere,ZSmokeHere ;
rAdjacent.SetZp(NewZ);
rAdjacent.GetZp();


while (Check == 1)
{
/*cout << &quot;OldQp is &quot; << rAdjacent.GetOldQp()<<endl;
fout << &quot;OldQp is &quot; << rAdjacent.GetOldQp()<<endl;
cout << &quot;m is &quot; << rAdjacent.GetMassFlowRatePlume() << endl;
fout << &quot;m is &quot; << rAdjacent.GetMassFlowRatePlume() << endl;*/
SmokeTempHere = (rAdjacent.GetOldQp() /( rAdjacent.GetMassFlowRatePlume()*1) + 273);
rAdjacent.SetTs(SmokeTempHere);
rAdjacent.GetTs();
VolumeOfLayerHere = ((rAdjacent.GetMassFlowRatePlume() * SmokeTempHere)/352);
rAdjacent.SetUpperLayerVolume(VolumeOfLayerHere );
rAdjacent.GetUpperLayerVolume();
HSmokeHere = (rAdjacent.GetUpperLayerVolume()/ (rAdjacent.GetOriginLenght() * rAdjacent.GetOriginWidth()));
rAdjacent.SetHSmoke(HSmokeHere );
rAdjacent.GetHSmoke();
ZSmokeHere = rAdjacent.GetOriginHeight() - rAdjacent.GetHSmoke();
rAdjacent.SetZs(ZSmokeHere);
rAdjacent.GetZs();

if (rAdjacent.GetZs() == rAdjacent.GetZp())
{
cout << &quot;Zp is equal to Zs&quot; <<endl;
fout << &quot;Zp is equal to Zs&quot; <<endl;
break;
}

if (rAdjacent.GetZs() != rAdjacent.GetZp())
{
cout << &quot;\nZp is &quot; << rAdjacent.GetZp() << &quot; and Zs is &quot; << rAdjacent.GetZs() << &quot;so i'm continuing&quot;<<endl;
fout << &quot;\nZp is &quot; << rAdjacent.GetZp() << &quot; and Zs is &quot; << rAdjacent.GetZs() << &quot;so i'm continuing&quot;<<endl;

rAdjacent.SetZp(ZSmokeHere);
rAdjacent.GetZp();

continue;
}
}

}




int main()
{


double CompLenght=2.34,CompWidth=2.34,CompHeight=2.16,CompOpHeight=1.6,CompOpWidth=0.81;
double CorrLenght=5,CorrWidth=3,CorrHeight=5,CorrOpHeight=3,CorrOpWidth=1,Choice1,Alpha;
double Time=0,Z;


/*cout << &quot;Please enter the dimensions for the Main Compartment.&quot; <<endl;
fout << &quot;Please enter the dimensions for the Main Compartment.&quot; <<endl;
cout << &quot;Enter Compartment Lenght: &quot; ;
fout << &quot;Enter Compartment Lenght: &quot; ;
cin>> CompLenght;
fout << CompLenght <<endl;
cout << &quot;\nCompartment Width: &quot; ;
fout << &quot;\nCompartment Width: &quot; ;
cin>> CompWidth;
fout << CompWidth << endl;
cout << &quot;\nCompartment Height: &quot;;
fout << &quot;\nCompartment Height: &quot;;
cin >> CompHeight;
fout << CompHeight <<endl;
cout << &quot;\nPlease enter dimensions for compartment opening&quot; <<endl;
fout << &quot;\nPlease enter dimensions for compartment opening&quot; <<endl;
cout << &quot;Enter Opening Height: &quot; <<endl;
fout << &quot;Enter Opening Height: &quot; <<endl;
cin >> CompOpHeight;
fout << CompOpHeight<<endl;
cout << &quot;Enter Opening Width: &quot; <<endl;
fout << &quot;Enter Opening Width: &quot; <<endl;
cin >> CompOpWidth;
fout << CompOpWidth <<endl;

fout << &quot;DIMENSIONS OF RoO(Metres)&quot; <<endl;
fout << &quot;LENGHT,WIDTH,HEIGHT,OPENING HEIGHT,OPENING WIDTH&quot; <<endl;
fout << CompLenght << &quot;,&quot; <<CompWidth << &quot;,&quot; << CompHeight << &quot;,&quot; << CompOpHeight << &quot;,&quot; << CompOpWidth << endl;
*/

//CLASS DIMENSIONS
Dimensions Adjacent;


/*cout << &quot;Please enter the dimensions for the Corridor.&quot; <<endl;
cout << &quot;Please enter the dimensions for the Corridor.&quot; <<endl;
cout << &quot;Enter Corridor Lenght: &quot; ;
fout << &quot;Enter Corridor Lenght: &quot; ;
cin>> CorrLenght;
fout<< CorrLenght <<endl;
cout << &quot;\nCorridor Width: &quot; ;
fout << &quot;\nCorridor Width: &quot; ;
cin>> CorrWidth;
fout << CorrWidth << endl;
cout << &quot;\nCorridor Height: &quot;;
fout << &quot;\nCorridor Height: &quot;;
cin >> CorrHeight;
fout << CorrHeight <<endl;
cout << &quot;\nPlease enter dimensions for Corridor opening&quot; <<endl;
fout << &quot;\nPlease enter dimensions for Corridor opening&quot; <<endl;
cout << &quot;Enter Opening Height: &quot; <<endl;
fout << &quot;Enter Opening Height: &quot; <<endl;
cin >> CorrOpHeight;
fout << CorrOpHeight<<endl;
cout << &quot;Enter Opening Width: &quot; <<endl;
fout << &quot;Enter Opening Width: &quot; <<endl;
cin >> CorrOpWidth;
fout << CorrOpWidth <<endl;*/


Choice1 = DoOccupancy();
Alpha = AlphaSelection(Choice1);

Adjacent.SetTime(Time);
Adjacent.SetAlpha(Alpha);
Adjacent.SetOriginDimensions(CompLenght,CompWidth,CompHeight);
Adjacent.SetOriginOpDimensions(CompOpHeight,CompOpWidth);
Adjacent.SetCorridorDimensions(CorrLenght,CorrWidth,CorrHeight);
Adjacent.SetCorridorOpDimensions(CorrOpHeight,CorrOpWidth);
Adjacent.SetZp(CompHeight);
Adjacent.GetZp();


for (Time = 0; Time <5;Time++)
{
cout << &quot;Time is :&quot; << Time <<endl;
fout << &quot;Time is :&quot; << Time <<endl;

Adjacent.SetTime(Time);
Adjacent.GetTime();

Z = DoAnalyseZ(Adjacent);

cout << &quot;\nAt time&quot; << Time << &quot;Z is &quot; << Z <<endl;
fout << &quot;\nAt time&quot; << Time << &quot;Z is &quot; << Z <<endl;
}

cout << &quot;THE END&quot; <<endl;

return 0;
}








 
double values are not that precise (floats are even worse!). For example, it's not unusual to see something like this:

[tt]
int intOne = 7;
int intTwo = 5;

double d = (double)intOne + (double)intTwo;

// value of 'd' is now 12.0000002 !![/tt]

Notice the miniscule fraction tacked on the end. That's what's causing all your double troubles.

A better solution would be to create your own 'double' type class which keeps track of the integer and fractional parts.
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
Because of such inaccuracy, when comparing doubles/ floats for &quot;equality&quot;, you want to check that the differance of the two numbers is less than a certain amount.

E.G.
( abs( double1 - double2 ) < .000001 )

It's an approximation of equality, but it's all you can do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top