I'm trying use the modulo operator (%) on long longs, and it seems to return 0 if the number is above the range of an unsigned int. Is there an operator or function that I should be using instead, or should I roll my own?
-- Update: sorry for the lack of example code before, I was in a hurry, heading out the door --
Here's the code:
long long val = 12345678991245; // always less than LLONG_MAX
int power = 0;
NSMutableArray *powers = [[NSMutableArray alloc] init];
while (pow(10, power) <= val) {
tVal = (val % (lround(pow(10, power + 1)))) / lround(pow(10, power));
[powers addObject:[NSNumber numberWithInt:tVal]];
NSLog(@"Power: %d tVal: %d", power, tVal);
val -= val % lround(pow(10, power + 1));
power++;
}
When you look at the output made by that NSLog
statement, though, you see this when the number exceeds UINT_MAX
(for the number 1,111,111,111,111):
2009-03-18 20:42:16.471 Numbers[11197:20b] Power: 0 tVal: 1
2009-03-18 20:42:16.472 Numbers[11197:20b] Power: 1 tVal: 1
2009-03-18 20:42:16.473 Numbers[11197:20b] Power: 2 tVal: 1
2009-03-18 20:42:16.476 Numbers[11197:20b] Power: 3 tVal: 1
2009-03-18 20:42:16.476 Numbers[11197:20b] Power: 4 tVal: 1
2009-03-18 20:42:16.477 Numbers[11197:20b] Power: 5 tVal: 1
2009-03-18 20:42:16.477 Numbers[11197:20b] Power: 6 tVal: 1
2009-03-18 20:42:16.477 Numbers[11197:20b] Power: 7 tVal: 1
2009-03-18 20:42:16.477 Numbers[11197:20b] Power: 8 tVal: 1
2009-03-18 20:42:16.478 Numbers[11197:20b] Power: 9 tVal: 0
2009-03-18 20:42:16.478 Numbers[11197:20b] Power: 10 tVal: 0
2009-03-18 20:42:16.479 Numbers[11197:20b] Power: 11 tVal: 0
2009-03-18 20:42:16.479 Numbers[11197:20b] Power: 12 tVal: 0
Everything is kosher until you get up to past UINT_MAX
, then it just returns 0.
I'm compiling against the iPhone SDK using Xcode 3.1.2.
Any ideas?