Solving VC++ Express Compiler Discrepancy with Conditional Operator

  • C/C++
  • Thread starter sid_galt
  • Start date
In summary: Thanks for the quick response.In summary, the programmer is having problems with their VC++ Express compiler and they need help figuring out what is wrong.
  • #1
sid_galt
502
1
My VC++ Express compiler has been acting very funnily of late.

I tried the following code
Code:
for(whatever)
{
double sum = whatever;

if(sum > 5.0)
    sum = 5
else if(sum < 1.0)
    sum = 1
}

The problem with this was that the program was now converting all the sum values generated into 1.0, even those which were greater than 1.0.

Instead of using the if statement, I tried the conditional operator and then it worked fine.

Code:
for(whatever)
{
double sum = whatever;

sum = sum > 5?  5:sum;
sum = sum < 1?  1:sum;
}//Working Okay

Does anyone know the reason for this discrepancy?
 
Technology news on Phys.org
  • #2
Well for one thing, you forgot the ';' after sum=5 / sum=1 in the first code.
 
  • #3
Seriously, I think the problem must be in the "whatever" (the code you didn't post). Either that, or you found a bug in the compiler.

In any case, the code below would be far more elegant (C++ jargon for cryptic :-)

sum = (sum > 5) ? 5 : ((sum < 1) ? 1 : sum);
 
  • #4
nabuco said:
Seriously, I think the problem must be in the "whatever" (the code you didn't post). Either that, or you found a bug in the compiler.

In any case, the code below would be far more elegant (C++ jargon for cryptic :-)

sum = (sum > 5) ? 5 : ((sum < 1) ? 1 : sum);

Most coding standards will fail nested ternary statements on code reviews. So try to keep it on your lab box. :-)
 
  • #5
Thanks for the replies.

Actually, the statements inside the for loop constitute an inline function. This inline function is what is actually called repeatedly through the for loop. The whatever is nothing but a simple for loop (int i = 0; i < ratingCount; i++)
I am getting another problem, this time both in VC++ and g++. I have created a structure -
Code:
typedef  unsigned char BYTE;

struct Data
{
short MovieId;
int CustId;
BYTE Rating;
float cache;
} rt;

cout<<sizeof(rt)<<endl;  //gives 16
cout<<sizeof(short)+sizeof(int)+sizeof(BYTE)+sizeof(float)<<endl; //correctly gives 11

When I use the sizeof() function to find out the size of the Data struct, it comes out to be 16 bytes instead of the 11 bytes it should be (2[short]+4[int]+1[BYTE aka char]+4[float]).
When I summed up the individual sizes of the variables inside the struct using sizeof(float), etc., the size was correctly coming to be 11 bytes.
 
Last edited:
  • #6
sid_galt said:
Actually, the statements inside the for loop constitute an inline function.
I still don't really understand the code in your first post. Since you are facing a subtle, apparently compiler-dependent problem, you should post the code as written.
I am getting another problem, this time both in VC++ and g++. I have created a structure -
Code:
typedef  unsigned char BYTE;

struct Data
{
short MovieId;
int CustId;
BYTE Rating;
float cache;
} rt;

cout<<sizeof(rt)<<endl;  //gives 16
cout<<sizeof(short)+sizeof(int)+sizeof(BYTE)+sizeof(float)<<endl; //correctly gives 11

When I use the sizeof() function to find out the size of the Data struct, it comes out to be 16 bytes instead of the 11 bytes it should be (2[short]+4[int]+1[BYTE aka char]+4[float]).
When I summed up the individual sizes of the variables inside the struct using sizeof(float), etc., the size was correctly coming to be 11 bytes.

Almost certainly the float (and sounds like the int as well) has to be aligned on 4-byte boundaries. That means you have a 3 byte gap between the end of the char and the start of the float, etc. If you want you struct to have the smallest size, reorder the data elements so that the largest ones are first. This is a good rule to observe whenever coding a struct or class.
 
  • #7
nmtim said:
I still don't really understand the code in your first post. Since you are facing a subtle, apparently compiler-dependent problem, you should post the code as written.

Code:
for(i = 0; i < m_nRatingCount; i++)

			{

				movieId = rating->MovieId;

				custId = rating->CustId;



				p = predictRating(rating->cache, UserFactor[f][custId], MovieFactor[f][movieId]);

				err = rating->Rating - p;

				sq += err*err;



				cf = UserFactor[f][custId];

				mf = MovieFactor[f][movieId];



				UserFactor[f][custId] += (float)(LRATE * (err * mf - K * cf));

				MovieFactor[f][movieId] += (float)(LRATE * (err * cf - K * mf));

				rating++;

			}

inline double predictRating(float &cache, float &UserFactor, float &MovieFactor)

{

	double sum = 0;

	sum = cache + UserFactor * MovieFactor;



	sum = sum > 5 ? 5:sum;//  This works but if condition does not

	sum = sum < 1 ? 1:sum;



	return sum;

}

Almost certainly the float (and sounds like the int as well) has to be aligned on 4-byte boundaries. That means you have a 3 byte gap between the end of the char and the start of the float, etc. If you want you struct to have the smallest size, reorder the data elements so that the largest ones are first. This is a good rule to observe whenever coding a struct or class.

Wow, I never knew that. Happily it worked.
 

1. What is a conditional operator in VC++ Express?

A conditional operator in VC++ Express is a symbol that allows you to perform different operations based on a condition. It is also known as a ternary operator, as it takes three operands - a condition, an expression to be evaluated if the condition is true, and an expression to be evaluated if the condition is false.

2. Why am I getting a compiler discrepancy with the conditional operator in VC++ Express?

This is likely due to a syntax error in your code. The conditional operator requires specific syntax, with the condition being followed by a question mark, the expression for the true case, a colon, and the expression for the false case. Make sure your code follows this syntax to avoid a compiler discrepancy.

3. Can I use multiple conditional operators in one expression in VC++ Express?

Yes, you can use multiple conditional operators in one expression in VC++ Express. However, it is important to keep the code readable and maintainable by not using too many nested conditional operators.

4. How do I debug issues with the conditional operator in VC++ Express?

You can use the built-in debugger in VC++ Express to step through your code and identify any issues with the conditional operator. You can also try breaking down your expression into smaller parts to narrow down the source of the discrepancy.

5. Are there any best practices for using the conditional operator in VC++ Express?

It is best to use the conditional operator in situations where the condition is simple and the expressions are short. If the expressions are longer or more complex, it is recommended to use an if-else statement instead for better readability and maintainability.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
29
Views
2K
Replies
6
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
1
Views
920
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
588
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
935
Back
Top