Math and Numbers

General Notes

It may not seem obvious, at least it wasn’t to me the first couple of days, butNSNumber does not support calculations directly. NSNumber is a class as opposed to a primitive datatype, so it does not replace the primitives like int, double, float, and the rest. This means that calculations done to them need to be converted fromNSNumber to the appropriate primitive.

What is also important to note is that NSNumber is good for is storing numbers inNSArray, NSDictionary, NSSet, and the like (including their mutable siblings) because those classes only accept objects and not primitive datatypes. Even then, it is important to note there is a distinction between NSNumber and a “class” likeNSInteger with the latter being a typedef to help manage 32-bit and 64-bit intsdespite the NS prefix.

Generate random numbers within a specific range

There are dozens of ways this can be handled, but this is the simplest way I have come up with so far that is still safe from compiler errors, particularly the divide by zero errors.