0

To make a button square the value of the mainLabel, I am using

- (IBAction)squarePressed:(id)sender
{

    NSString *mainLabelString = mainLabel.text;

    int mainLabelValue = [mainLabelString intValue];

    NSString *calculatedValue = [NSString stringWithFormat: @"%d", mainLabelValue*mainLabelValue];

    mainLabel.text = calculatedValue;

}

No erros are shown, and it works well with small numbers,
for example, the result of squaring 720 is correct, 518400, but if I try to square that number, instead of 268738560000, I get -1844379648.
Why is this happening?

Peter V
  • 2,478
  • 6
  • 36
  • 54

4 Answers4

4

An int is a signed 32 bit number.

The result of 518400 squared does not fit in 32 bits, so the calculation overflows, that is why you see the large negative value.

Try using a long, which is 64 bits and should be sufficient for that number:

long mainLabelValue = [mainLabelString longLongValue];
driis
  • 161,458
  • 45
  • 265
  • 341
4

The int is probably a 32-bit number, and 518400 squared is more than can be represented in 32 bits. Some of that is explained at When to use NSInteger vs. int .

You could write

NSInteger mainLabelValue = [mainLabelString longValue]; // or long mainLabelValue...
NSString *calculatedValue = [NSString stringWithFormat: @"%ld", mainLabelValue*mainLabelValue];

NSInteger seems to be 64 bits in iOS.

Community
  • 1
  • 1
emrys57
  • 6,679
  • 3
  • 39
  • 49
3

You are probably overflowing the maximum integer size, hence the negative switch. For a 32-bit system this value is 2,147,483,647 (2^31 - 1)

1

That's a numeric overflow, use a long long if you want to use the highest number of bits avoiding the overflow.Also a long double would be good, but not so precise.

- (IBAction)squarePressed:(id)sender
{

    NSString *mainLabelString = mainLabel.text;

    long long mainLabelValue = [mainLabelString longLongValue];

    NSString *calculatedValue = [NSString stringWithFormat: @"%lld", mainLabelValue*mainLabelValue];

    mainLabel.text = calculatedValue;

}

Of course if you use high numbers also this way it may occur an overflow, but it occurs for higher values, depending on the bits difference from int to long long int.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187