6

the maximum value for an 32-Bit integer is: 2^31-1 = 2147483647. but just with negative and positive number. because the half of the number is negative. so the real maximum value is 2^32-1 = 4294967295. but in this case we just use positive numbers.

ok, a normal int is both negative and positive number. i want to use just positive number because i want the maximum value to be: 4294967295. i'm going to use "unsigned int" instead of "int"

but this will not work! the maximum value is still 2147483647.

here is the code for a simple random number generator:

-(Action for my button) {

unsigned int minNumber;

unsigned int maxNumber;

unsigned int ranNumber;

minNumber=[self.textFieldFrom.text intValue]; //getting numbers from my textfields
maxNumber=[self.textFieldTo.text intValue]; //Should i use unsigned intValue?

ranNumber=rand()%(maxNumber-minNumber+1)+minNumber;

NSString *str = [NSString stringWithFormat:@"%d", ranNumber];

self.label.text = str;

}

and this will view : 2147483647 as a maximum value.

what's wrong? should i use unsigned intValue when i getting numbers from my textFields?

Jonathan


here you can read about this number. : http://en.wikipedia.org/wiki/2147483647

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Jonathan Gurebo
  • 169
  • 2
  • 3
  • 9
  • Yes, I believe you need to use the unsigned int. Once you stop using it, all values get thrown to the signed int max. Also, if you have an idea on how to accomplish something, why don't you try it? – Josiah Dec 30 '12 at 21:13
  • @JonathanGurebo And, by the way, if you're really concerned about numbers larger than `2,147,483,647`, you should note that `rand()` will never generate a number larger than `RAND_MAX`, `0x7fffffff`. You probably should tweak your random number algorithm. Try generating numbers between `0` and `4,294,967,295` and you'll see you'll never get a value larger than `2,147,483,647`. – Rob Dec 30 '12 at 22:44
  • If using iOS 4.3 or later, use `ranNumber = arc4random_uniform(maxNumber-minNumber+1) + minNumber;`, which solves `RAND_MAX` problem of `rand()`, supporting up through `UINT32_MAX`. – Rob Dec 30 '12 at 23:04
  • Why don't use double or float :) – iProgrammed Dec 30 '12 at 23:08

2 Answers2

9

The problem is intValue which returns a int (not an unsigned int) and is maxed out at 2,147,483,647. If you need larger than INT32_MAX, then use long variables and NSString method longLongValue instead of intValue.

Yes, %d is signed, but if you use %u that will not solve the problem with intValue. Using long long variables and longlongValue method will solve this. For example:

NSString *string = @"2147483650";

unsigned int i = [string intValue];
NSLog(@"i = %u", i); // returns 2147483647

NSUInteger j = [string integerValue];
NSLog(@"j = %u", j); // returns 2147483647

long long k = [string longLongValue];
NSLog(@"k = %lld", k); // returns 2147483650

So, looking at your original code, it might be:

long long minNumber;

long long maxNumber;

long long ranNumber;

minNumber = [self.textFieldFrom.text longLongValue];
maxNumber = [self.textFieldTo.text longLongValue];

ranNumber = arc4random_uniform(maxNumber-minNumber+1) + minNumber;

NSString *str = [NSString stringWithFormat:@"%lld", ranNumber];

self.label.text = str;
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • ok, i changed my code to: unsigned int minNumber; unsigned int maxNumber; unsigned int ranNumber; minNumber=[self.textFieldFrom.text longLongValue]; maxNumber=[self.textFieldTo.text longLongValue]; ranNumber=rand()%(maxNumber-minNumber+1)+minNumber; NSString *str = [NSString stringWithFormat:@"%d", ranNumber]; self.label.text = str; but it will give me negative numbers when i using high numbers. – Jonathan Gurebo Dec 30 '12 at 22:14
  • @JonathanGurebo 1. Change the `unsigned int` to `long long`. 2. don't use `%d` with `unsigned int`, but rather use `%u`, and if you're using `long long`, use `%lld`. – Rob Dec 30 '12 at 22:38
  • Hi, i have my label in the center of the screen, when i'm using "%11d" it is not center anymore. actually, xcode tells me to use "%11lld". – Jonathan Gurebo Dec 31 '12 at 10:11
  • I think you misread my comment. I was not suggesting `%11d` (with ones), but rather `%lld` (with lowercase "L"'s). Just make it `%lld` (with lowercase "L"'s and no number eleven). – Rob Dec 31 '12 at 10:13
5

%d modifier is parsed as signed integer.

Nickolay Olshevsky
  • 13,706
  • 1
  • 34
  • 48