• 0

C++ Programming, a simple error I'm not sure what is wrong


Question

All these variables are defined as double. Here is my code

X = (A / B]) ;

Range = (X - m_Min12) / (m_Max12 - m_Min12);

if (Range < (16 / 100.0))

{

execute code!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

}

I cannot figure this out for the life of me, but I only want the code executed within the if statement when the variable Range is < .16. Is there anything that I am writing incorrectly that is causing this to screw up (I'm thinking possibly due to rounding). Because I am getting executions within the if statement when Range is > .16

Thanks

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0
All these variables are defined as double. Here is my code

X = (A / B]) ;

Range = (X - m_Min12) / (m_Max12 - m_Min12);

if (Range < (16 / 100.0))

{

execute code!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

}

I cannot figure this out for the life of me, but I only want the code executed within the if statement when the variable Range is < .16. Is there anything that I am writing incorrectly that is causing this to screw up (I'm thinking possibly due to rounding). Because I am getting executions within the if statement when Range is > .16

Thanks

How is "Range" defined?

You may need to cast 16 / 100.0 to a double.

if ((double)Range < (double)(16 / 100.0))

Link to comment
Share on other sites

  • 0

I tested this code and it works fine for me

	double range;

	range = 0.1;

	if(range &lt; 0.16){
		cout &lt;&lt; "its less than 0.16";
	}else{
		cout &lt;&lt; "its greater than 0.16";
	}

When you do 16/100 I think it's doing integer division and skipping the if statement because 16/100 will be 0 remainder 16 so the IF will be false.

Link to comment
Share on other sites

  • 0
All these variables are defined as double. Here is my code

X = (A / B]) ;

Range = (X - m_Min12) / (m_Max12 - m_Min12);

if (Range < (16 / 100.0))

{

execute code!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

}

I cannot figure this out for the life of me, but I only want the code executed within the if statement when the variable Range is < .16. Is there anything that I am writing incorrectly that is causing this to screw up (I'm thinking possibly due to rounding). Because I am getting executions within the if statement when Range is > .16

Thanks

Try making the division about 16.0 / 100, or 16f / 100.

It's not enough to have the variable being assigned to a double; it has to perform the division itself as a floating point division too. Otherwise you will get an integer division, with the result Vizion tells above. And this is determined by the dividend. As a more generic example: double d = (double)a / b, in case a is an integer, that is.

Btw, as for the parenthesis there, the comparison operators all have lower precedence than the arithmetic ones. There are a lot of details here, but simplified it's basically arithmetic -> comparative -> logical.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.