Weird cast error

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 1
Reputation: Ray Gray is an unknown quantity at this point 
Solved Threads: 0
Ray Gray Ray Gray is offline Offline
Newbie Poster

Weird cast error

 
1
  #1
Jun 3rd, 2009
We came across a very strange behavior with a code like this (this is a simplified version; it took quite a time before we were able to identify a similar problem in our code):

unsigned int x = 10;
int a = ((int-1) * x;
int b = ((int)-1) * x / 2; // Result is not -5!!!!!
assert(a == -10); // OK
assert(a == -5);   // Assert fails!!!!!

Program was compiled with the Visual Studio 2008. Can anybody explain why 'b' is not -5? It seems like 'b' is treated as unsigned int instead of int.
Last edited by Tekmaven; Jun 3rd, 2009 at 4:02 pm. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Weird cast error

 
0
  #2
Jun 3rd, 2009
1.
int a = ((int-1) * x; // this does not compile!!!!

2.
assert(a == -5); // Assert fails!!!!!

Does assert(b == -5); work??
Last edited by jencas; Jun 3rd, 2009 at 7:33 am.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Weird cast error

 
0
  #3
Jun 3rd, 2009
int a = ((int-1) * x;
I suppose you meant int a = ((int)-1) * x;
assert(a == -5); // Assert fails!!!!!
I suppose you meant assert(b == -5); // Assert fails!!!!!
From TCPL K&R 2nd ed, if either operand of an arithmetic operation is unsigned int, the other is also converted to unsigned int and the result is also an unsigned int. (I'm only assuming the case where both are same width, e.g. unsigned int against signed int).
consider int b = ((int)-1) * x / 2; which is actually
int b =( ((int)-1) * x )/ 2; if you care for the left associativity of the multiplication operator.
((int)-1) yields a integer -1 (note that this cast is useless). Since it is being multiplied by a unsigned int, -1 is promoted to a unsigned int. Any negative int when promoted to int will yield smallest non-negative value that is congruent to that integer, modulo one more than the largest value that can be represented in the unsigned type. So it is a very large number(4294967286 on my machine)which when multiplied by 10 yields larger number which is again demoted to fit into a unsigned int. Now you divide that number by two and you get the result.(which is still a larger number)(2147483643 on my machine.
The funny part is: all this doesn't happens in the line previous to this. That is perhaps because of your compiler's smartness, it doesn't promote -1 to int but demotes x to a int. This is done as, there is no more operation to perform.
Anyways., the correct procedure would be ((int)-1) * (x / 2); which would yield the desired result.
Note that int(-1) is same as -1.

If I were you I would have chosen a statement like this: int b = ( int(x)*(-1) ) /2;
The code tags are :
[code=cplusplus]
//code goes here
[/code]
Last edited by siddhant3s; Jun 3rd, 2009 at 7:46 am.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Weird cast error

 
0
  #4
Jun 3rd, 2009
This is C++ not C, so I would have written:
  1. b= -1*static_cast<int>(x)/2;

The main advantage of this is the you have an explicit bracket around the section you are casting and it is a BIG flag that you are changing type. Something that in numerical code is often such a big area of problems that the old (type) casts are often forbidden.

It also avoids nasty stuff when you convert (accidentally) from a pointer to a numeric type. Easily done when you have a long expression with pointers and multiplications.
Last edited by StuXYZ; Jun 3rd, 2009 at 1:21 pm.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Weird cast error

 
0
  #5
Jun 3rd, 2009
The funny part is: all this doesn't happens in the line previous to this. That is perhaps because of your compiler's smartness, it doesn't promote -1 to int but demotes x to a int.
1. Probably you mean doesn't promote -1 to unsigned int...
2. All this happens in int a = (int)-1 * x . The funny part is: (int)-1 is the same as -1 because an integer literal already (and always) has int type. The compiler never takes into account left part type: it promotes -1 to unsigned int (max value) then multiply it to unsigned(10) (ignores integer overflow). After that the result is unsigned(4294967286) or int(-10) - the same bit pattern...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC