iOS Development

Course No. 10-152-139

Chapter 7 — Numbers

Integers

char 2^8 256 2^7 -128 to +128
int 2^32 4,294,967,296 2^31 -2,147,483,648 to +2,147,483,648
long 2^64 18,446,744,073,709,551,616 2^63 -9,223,372,036,854,775,808 to +9,223,372,036,854,775,808
  • Tokens for displaying integers
%d decimal or base 10
%o octal or base 8
%x hexadecimal or base 16

Integer operations

  • This works fine.

Integer division

  • What about this?
    • Um, what?
  • Casting
    • That’s better.

Operator shorthand

increment ++ index++;
decrement −− count--;
add & assign += salary += 1000.0;
subtract & assign -= rate -= 10;
multiply & assign *= tax *= 2;
divide & assign /= amount /= 3;

Floating-point numbers

Tokens for displaying floating-point numbers

  • %f
    • Displays a floating point number, usually with 6 decimal places.
  • %.2f
    • Displays a floating point number with 2 decimal places.
  • %.10f
    • Displays a floating point number with 2 decimal places.

The math library

  • A quick tour of the math functions that are part of C.
    • Open the Terminal application. It’s here:
      • /Applications/Utilities/Terminal
    • Enter the following on the command line:
      • man math
      • man is short for manual and is the unix utility for viewing system documentation.
    • Page up and down with these keys:
      • f – this means page forward
      • b – this means page backwards
    • Look for the function that will square or cube a number.
    • Look for the constant that gives the value of pi.
    • Those might come in handy in a bit.

Comments

  • Get in the habit of commenting your code
    • Write useful and specific comments that inform others (and yourself).
    • Single line comments: //
    • Multi line comments: /* */