Book Image

iOS 9 Game Development Essentials

By : Chuck Gaffney
Book Image

iOS 9 Game Development Essentials

By: Chuck Gaffney

Overview of this book

Table of Contents (15 chapters)
iOS 9 Game Development Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Ints, UInts, floats, and doubles


In addition to Boolean values, another basic data type we have up to this point briefly mentioned is the various numeric objects, such as integers (Ints), unsigned integers (UInts), floating point numbers / decimals (floats), and double precision floating point numbers / decimals (doubles).

Integers and unsigned integers

Integers represent negative and positive whole numbers, while unsigned integers represent positive whole numbers. Like with C and other programming languages, Swift lets us create various types of integers and unsigned integers from 8, 16, 32, and 64 bits. For example, an Int32 type is a 32-bit integer, while a UInt8 type is an 8-bit unsigned integer. The size of the bits for Ints and UInts represents how much space is being allocated to store the values. Using our UInt8 example, a number made from this type of unsigned Int can only store the values 0-255 (or 11111111 in a base-2 system). This is also known as 1 byte (8 bits). If we need to store numbers larger than 255 or negative numbers, then maybe an Int16 type would suffice as that can store numbers between –32767 and 32767. Usually, we don't have to worry too much about the size allocated by our integer variables and constants. So, using just the class name of Int will work fine in most cases.

Note

The size of Int will differ depending on the type of system we are working on. If we are compiling our code on a 32-bit system, an integer will be equal to Int32, while the same integer would be an Int64 on a 64-bit system.

Swift can let us see what our minimum and maximum values are for an Int variable with the .min or .max class variables (that is, Int16.max = 32767 and UInt.min = 0).

Floats and doubles

Floats are 32bit floating point numbers / fractions, such as pi (3.14), or the golden ratio, phi (1.61803).

In game designing, we work with floating point values and ranges rather often, be it to determine the CGPoint in x and y of a 2D sprite, using linear interpolation for smoothing a game's camera movement in 3D space, or applying various physics forces on an object or 2D/3D vector. The precision needed for each situation will determine if a float is needed or if the 64-bit floating point value, the double is needed. Doubles can be as precise as 15 decimal places, while a float is six decimal places precise.

Tip

It's actually best practice to use doubles in situations that would work for either floats or doubles.