| | |
Weird cast error
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 1
Reputation:
Solved Threads: 0
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):
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.
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
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
1.
int a = ((int-1) * x; // this does not compile!!!!
2.
assert(a == -5); // Assert fails!!!!!
Does assert(b == -5); work??
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
Please use code tags - Please mark solved threads as solved
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).
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
(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
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
This is C++ not C, so I would have written:
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.
c++ Syntax (Toggle Plain Text)
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
•
•
•
•
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.
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... ![]() |
Similar Threads
- cast error (C)
- Help with a cast error (ASP.NET)
- error C2664 (C++)
- weird compiler error (C)
- Weird GAC error when Trying to Install (VB.NET)
- A Really Weird DNS Error (Web Browsers)
- a weird Javascript error (JavaScript / DHTML / AJAX)
Other Threads in the C++ Forum
- Previous Thread: Game Mario (help!)
- Next Thread: Array/ for statment woes-Help
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






