Math functions and constants
Today few math functions and constants available in Objective-C.
They are included in the math.h header. It's available into the iOS/OS X SDK.
pow(double, double) - power of
NSLog(@"%.f", pow(3,2) ); //result 9 NSLog(@"%.f", pow(3,3) ); //result 27
sqrt(double) - square root
NSLog(@"%.f", sqrt(16) ); //result 4 NSLog(@"%.f", sqrt(81) ); //result 9
ceil(double) - the next bigger integer
NSLog(@"res: %.f", ceil(3.000000000001)); //result 4 NSLog(@"res: %.f", ceil(3.00)); //result 3
floor(double) - removes the decimal part
NSLog(@"res: %.f", floor(3.000000000001)); //result 3 NSLog(@"res: %.f", floor(3.9999999)); //result 3
round(double) - rounds the argument
NSLog(@"res: %.f", round(3.5)); //result 4 NSLog(@"res: %.f", round(3.46)); //result 3 NSLog(@"res: %.f", round(-3.5)); //NB: this one returns -4
fmin(double, double) - the smaller
NSLog(@"res: %.f", fmin(5,10)); //result 5 NSLog(@"res: %.f", MIN(5,10)); //result 5
fmax(double, double) - the bigger
NSLog(@"res: %.f", fmax(5,10)); //result 10 NSLog(@"res: %.f", MAX(5,10)); //result 10
fabs(double) - the absolute value
NSLog(@"res: %.f", fabs(10)); //result 10 NSLog(@"res: %.f", fabs(-10)); //result 10
other functions to get the absolute value are described here
M_E 2.71828182845904523536028747135266250 /* e */ M_LOG2E 1.44269504088896340735992468100189214 /* log 2e */ M_LOG10E 0.434294481903251827651128918916605082 /* log 10e */ M_LN2 0.693147180559945309417232121458176568 /* log e2 */ M_LN10 2.30258509299404568401799145468436421 /* log e10 */ M_PI 3.14159265358979323846264338327950288 /* pi */ M_PI_2 1.57079632679489661923132169163975144 /* pi/2 */ M_PI_4 0.785398163397448309615660845819875721 /* pi/4 */ M_1_PI 0.318309886183790671537767526745028724 /* 1/pi */ M_2_PI 0.636619772367581343075535053490057448 /* 2/pi */ M_2_SQRTPI 1.12837916709551257389615890312154517 /* 2/sqrt(pi) */ M_SQRT2 1.41421356237309504880168872420969808 /* sqrt(2) */ M_SQRT1_2 0.707106781186547524400844362104849039 /* 1/sqrt(2) */ MAXFLOAT ((float)3.40282346638528860e+38)