Home > Articles > Programming > Mac and iOS Programming

  • Print
  • + Share This
This chapter is from the book

Exploring the Language with Some Basic Math

Regardless of what kind of application you plan to build, you will invariably find yourself working with numbers or strings (text). Whether you want to allow your users to create a budget, report their high score in a game, or see the average of their test scores, you’ll need to use numbers for that.

You have probably performed some basic math using some programming language in the past and, as mentioned earlier, because Objective-C is a superset of C, anything you can do in C you can do in Objective-C, including math.

Follow these steps to create some code that illustrates the basic use of numbers and math in Objective-C:

  1. With Xcode open to the project you created in the preceding task, click the Hour3AppDelegate.m file. Locate the applicationDidFinishLaunching: method and add the following lines of code between the two curly braces:
    int a=5;
    int b=10;
    int c = a*b;
    NSLog(@"a*b = %d", c);
  2. In the top-right corner of the Xcode IDE, click the button that shows a bottom tray icon. If you hover over this button, it will indicate that it hides or displays the debug area.
  3. With the debug area now visible, run your application. In the debug area, below the information about the version of the debugger being used, you should see some text that looks like this:
    2011-05-29 12:16:44.916 Hour3[640:903] a*b = 50

NSLog is a function that you can call that will log text to a standard output device—in our case, the debug area of the application.

You may have noticed the little yellow triangles that appeared in the margin to the left of your code as you typed. Even before you build and run your application, as you type, the compiler is checking your code for errors or possible problems (warnings). The first warning you saw was the compiler informing you that you had declared the variable a but hadn’t yet used it. As soon as you used it, that warning disappeared. This kind of instant, interactive feedback from Xcode can come in extremely handy and save you a lot of troubleshooting time in the future.

  1. Now add the following lines of code where you left off:
    double d = 123.00;
    double e = 235.00;
    double f = d * e;
    NSLog(@"d*e = %f", f);
    NSLog(@"d/e = %f", d / e);
    NSLog(@"d/0 = %f", d / 0);
  1. Now try to run the application, even with that warning still in place. Your output in the debug area should look like this:
    2011-05-29 12:29:31.407 Hour3[745:903] a*b = 50
    2011-05-29 12:29:31.410 Hour3[745:903] d*e = 28905.000000
    2011-05-29 12:29:31.411 Hour3[745:903] d/e = 0.523404
    2011-05-29 12:29:31.412 Hour3[745:903] d/0 = inf

Notice here that dividing by zero didn’t crash our application. In many programming languages, including modern, managed runtime environments such as Java and C#, any attempt to divide by zero will crash the running application. Here, Objective-C kept on going and even managed to give the mathematically correct answer here of infinity rather than a crash.

When you are done with this hour, feel free to go back to this code and play around with more basic math. Remember that the data types we’ve used thus far are basic C types. As Cocoa programmers using Objective-C, you have the primitive numeric data types shown in Table 3.2 (as well as several others) available to you.

Table 3.2 Some Basic Objective-C Numeric Data Types

Type

Size

Description

int

32 or 64 bits

Basic integer. The size varies whether you're building a 32 or 64-bit application.

float

32 bits

A data type used for storing floating-point values.

double

64 bits

A data type used for storing large floating-point values.

long

64 bits

A data type used for storing large integers.

uint

2x(int)

A data type used for storing integers without a sign bit, allowing for the storage of numbers twice as big as in integer.

More numeric data types are available, but these are the ones that get used most often.

  1. Add the following lines of code to the sample you’ve been working with:
    float floaty = d*e;
    
    NSLog(@"size of float: %lu", sizeof(floaty));
    NSLog(@"size of double: %lu", sizeof(f));
    NSLog(@"size of int: %lu", sizeof(a));
    NSLog(@"size of long: %lu", sizeof(long));
    NSLog(@"size of uint: %lu", sizeof(uint));
  2. Now run the application to see the various sizes of the data types. Note that the sizeof function will work on a variable (such as floaty or f) or a data type (such as long or int).

One possible problem that you may already be thinking about is that different users have different computers, and as such, an int on the developer’s computer might not always be the same as an int on the end-user’s computer. There is a way to deal with these issues (and many more not-so-obvious problems with numbers and math), and you’ll see one solution later in this hour when we get to a discussion of pointers.

  • + Share This
  • 🔖 Save To Your Account