| Thread Closed |
VC++ has gone crazy! |
Share Thread | Thread Tools |
| Apr27-07, 06:27 AM | #1 |
|
|
VC++ has gone crazy!
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
}
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
|
| Apr27-07, 06:38 AM | #2 |
|
|
Well for one thing, you forgot the ';' after sum=5 / sum=1 in the first code.
|
| Apr27-07, 10:10 AM | #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); |
| Apr27-07, 11:29 PM | #4 |
|
|
VC++ has gone crazy! |
| Apr28-07, 12:43 PM | #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 summed up the individual sizes of the variables inside the struct using sizeof(float), etc., the size was correctly coming to be 11 bytes. |
| Apr28-07, 01:46 PM | #6 |
|
|
|
| Apr28-07, 02:19 PM | #7 |
|
|
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;
}
|
| Thread Closed |
| Thread Tools | |
Similar Threads for: VC++ has gone crazy!
|
||||
| Thread | Forum | Replies | ||
| going crazy | Academic Guidance | 20 | ||
| This is crazy! | General Discussion | 3 | ||
| I'm going crazy | Computing & Technology | 7 | ||