(unsigned int) problem

Hi there,


Not sure if I am in the correct place to ask these kind of questions. However in any case, some help will be greatly appreciated.


The problem is that I do not know what is wrong with this code:


#import <Foundation/Foundation.h>


int main (void)

{

@autoreleasepool {

unsigned int i, j; // the problem is solved if this line is interchanged by:

// int i, j;

unsigned int m=4, n=3;

double matrix[m][n];

for (i=0; i<m; i++)

for (j=0; j<n; j++)

matrix[i][j]=5*i-2*j;

printf("Matrix:\n");

for (i=0; i<m; i++)

for (j=0; j<n; j++)

printf(" matrix[%u][%u] = %.3f\n", i, j, matrix[i][j]);

}

return 0;

}




When running this code, I get these odd results:

matrix[0][1] = 4294967294.000

matrix[0][2] = 4294967292.000


which should be:

matrix[0][1] = -2.000

matrix[0][2] = -4.000


Any idea?


Thank you!


P.S. This problem is solved if both i and j are defined as numbers of type int. Why is this? I am using Xcode 4.2.1.

Xcode 4.2.1-OTHER, Mac OS X (10.7.3)

Posted on Mar 11, 2012 10:21 AM

Reply
4 replies

Mar 11, 2012 10:52 AM in response to adarpodracir

What etre says.

This is the responsible line:


matrix[i][j]=5*i-2*j;


Since both i and j are unsigned ints at that point, the right halve is evaluated using unsigned ints. Unsigned numbers don't go lower than 0 (they are so typical in that behavior, they even got named for that). So the result is the next best thing, a wrap-around to the *maximum* integer value. Which is actually the way 'negative' numbers work (the maximum positive value is half the max you can get with unsigned numbers -- minus 1, because there also needs to be room for a 'zero' somewhere).


After this right halve is evaluated (using unsigned arithmetic) it is assigned to the double variable, but at that point it's "too late" -- unsigned numbers will be silently converted to these.


You cn solve this in one of two ways: make i and j signed ints after all, or force of the two to be a double *at evaluation time*. That, don't use "(double)(5*i-2*j)" because that will give the same result, but use "5*(double)i-2*j" or any variant thereof. Even "5.*i" is already enough.

Mar 11, 2012 11:40 AM in response to Jongware

Dear Jongware/etresoft,


Thank you very much for reply.


I think I am beginning to understand. Since this expresion:

5*i-2*j

is in function of i and j, which are unsigned integers, and because their coeffiecients are also unsigned integers (assuming that the minus sign is used here for subtraction), all the expresion is then computed by the computer suggesting the result is an unsigned integer as well. The resulting number is then converted to double. Am I correct?


Thank you.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

(unsigned int) problem

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.