## Objective-C Math

April 1, 2009 § 4 Comments

Objective-C is the object oriented version of C. C contains a library of standard Mathematical functions.

Objective-C does support C-based mathematical functions. All C are written for long variable types. Variables are declared long. Functions must be declared long float or double and any contacts must be written in floating point forms. eg. 1 should be written as 1.0.

int i; // long int

double x; // long float

fabs() – Find the absolute value or unsigned value in parentheses

results = fabs(x); // fabs(-1.5) = 1.5

ceil() – Find the ceiling integer; integer just above the value in parentheses; similar to rounding up

i = ceil(x); // ceil(2.5) = 3

floor() – find the integer that is below the floating point value in parentheses

i = floor(x); // floor(2.2) = 2

exp() – find the expoential value

pow() – raise a number to the power

result = pow(x,y); // raise x to the power of y

result = power(4,2); // raise 4 to the square = 16

sqrt() – find the square root of a number

i = sqrt(x); // sqrt(9) = 3

this was super helpful for locating the obj-c sqrt() and pow() key-words. i knew there had to be some but wasn’t sure. found your site and it solved my problem. [i’m just starting to learn obj-c]. thx!

Thanks a lot ðŸ™‚

thanks ,very useful

thanks!