Sei sulla pagina 1di 5

Objective-C Math functions http://www.touch-code-magazine.

com/objective-c-math-functions/

Tutorials and excerpts about iPhone and iPad programming in Objective-C and Cocoa Touch

Touch Code Magazine


Home About Web color to UIColor convertor Yin and Yang of iPhone Development Search

Read More »

RSS Feed »

1 of 5 4/23/11 2:18 PM
Objective-C Math functions http://www.touch-code-magazine.com/objective-c-math-functions/

7 Featured Articles
Objective-C Math functions tweets

retweet Share Doodle Booth HD


Marin on February 6, 2010
for iPad is on the
App Store
0 Comments

Top 10 Tips To
Creating
Profitable iPhone
Apps
5 Comments

Objective-C uses C’s math which you can find in math.h,


Advertise Here
however as we do program in different languages it is really
annoying to remember by heart how the function names are
abbreviated in each and every language. Thus comes here a
short math overview of the math in Objective-C.

Math functions
double pow ( double, double ) - power of

NSLog(@"%.f", pow(3,2) ); //result 9


NSLog(@"%.f", pow(3,3) ); //result 27 Blood Booth is
available in the
App Store
double sqrt( double ) - square root
0 Comments

NSLog(@"%.f", sqrt(16) ); //result 4 Vertex Helper Pro


NSLog(@"%.f", sqrt(81) ); //result 9 in the Mac App
Store
3 Comments
double ceil ( double ) - if the argument has any decimal part, returns the next bigger integer

NSLog(@"res: %.f", ceil(3.000000000001)); //result 4


NSLog(@"res: %.f", ceil(3.00)); //result 3
Year 7 math practice
Geometry, decimals, pre-algebra — Practice
double floor ( double ) - removes the decimal part of the argument 200+ year seven skills!

NSLog(@"res: %.f", floor(3.000000000001)); //result 3


NSLog(@"res: %.f", floor(3.9999999)); //result 3
Recent Articles
double round ( double ) - rounds the argument
Doodle Booth HD
for iPad is on the
NSLog(@"res: %.f", round(3.5)); //result 4
App Store
NSLog(@"res: %.f", round(3.46)); //result 3
0 Comments
NSLog(@"res: %.f", round(-3.5)); //NB: this one returns -4

Top 10 Tips To
Creating
double fmin ( double, double ) - returns the smaller argument
Profitable iPhone
Apps
NSLog(@"res: %.f", fmin(5,10)); //result 5
5 Comments

Blood Booth is
double fmax ( double, double ) - returns the bigger argument available in the
App Store
NSLog(@"res: %.f", fmax(5,10)); //result 10 0 Comments

double fabs( double ) - returns the absolute value of the argument

2 of 5 4/23/11 2:18 PM
Objective-C Math functions http://www.touch-code-magazine.com/objective-c-math-functions/

NSLog(@"res: %.f", fabs(10)); //result 10


Tutorial: Fetch
NSLog(@"res: %.f", fabs(-10)); //result 10 and parse JSON
1 Comment

Eventually you will find also all the trigonometry you’ll need as: sin, cos, tan, atan, and their
variations. Vertex Helper Pro
in the Mac App
Store
Few math constants 3 Comments

As found in the math.h New app: Doodle


Booth for
#define M_E 2.71828182845904523536028747135266250 /* e */
iPhone/iPod
#define M_LOG2E 1.44269504088896340735992468100189214 /* log 2e */ 3 Comments
#define M_LOG10E 0.434294481903251827651128918916605082 /* log 10e */
#define M_LN2 0.693147180559945309417232121458176568 /* log e2 */ iAD error: The
#define M_LN10 2.30258509299404568401799145468436421 /* log e10 */ operation
#define M_PI 3.14159265358979323846264338327950288 /* pi */ couldn’t be
#define M_PI_2 1.57079632679489661923132169163975144 /* pi/2 */ completed. Ad
#define M_PI_4 0.785398163397448309615660845819875721 /* pi/4 */ inventory
#define M_1_PI 0.318309886183790671537767526745028724 /* 1/pi */ unavailable
#define M_2_PI 0.636619772367581343075535053490057448 /* 2/pi */
0 Comments
#define M_2_SQRTPI 1.12837916709551257389615890312154517 /* 2/sqrt(pi) */
#define M_SQRT2 1.41421356237309504880168872420969808 /* sqrt(2) */
#define M_SQRT1_2 0.707106781186547524400844362104849039 /* 1/sqrt(2) */

#define MAXFLOAT ((float)3.40282346638528860e+38) Cool web


Coda, a Panic Software
So, this is a wrap up for the overview of Objective-C math, if I missed the one function you use the
Cristina Catana Photos
most, feel free to leave a comment and share it with the other readers.
Daniel Freeman
Image: http://www.sxc.hu/profile/hisks
Mac Rumors

Smashing Magazine

If you liked this article please share it with your friends or colleagues; there's also a low traffic RSS feed to give you
heads up when there's a new post in Touch Code Magazine.

Like 2 people like this. Be the first of your friends.

Tweet This!

Stumble upon something good? Share it on StumbleUpon


Share this on del.icio.us
Marin Todorov
Share this on Facebook
is a Digg
programmer
this! with some 18 years of experience in a dozen of languages, most recently fascinated
about Objective-C and Cocoa Touch on the iPhone and iPad.
Share this on Reddit
» Marin's homepage » Personal blog » Doodle Booth for iPhone » Pimpy for iPhone
Add this to Google Bookmarks
Buzz up!
Year 7 math practice Math Puzzles
Pre-algebra, geometry, integers — Practice Great Bargains For Puzzles Buy/Sell/Find
200+ year seven skills! Items At AyosDito!

Comments

Objective-c math functions sqrt | Touch Code Magazine


[...] more Objective-C math functions examples read my full article. Retweet Share Bookmark [...]

Categories
3 of 5 Advanced Answers 4/23/11 2:18 PM

Basics cocos2d
Objective-C Math functions http://www.touch-code-magazine.com/objective-c-math-functions/

blinker13
on Saturday 6, 2010

In your “double floor (double)” example you are using ceil().


Probably a copy/paste error.

Marin
on Saturday 6, 2010

Ha! Totally
Great great catch – thanks

A personal message
[...] do you find interesting to read about? Some of my API Overviews seem to be quite popular like the
Objective-C Math API and the Geometry API. Also the tutorial for downloading dynamic content from
Internet looked like [...]

Objective-C Math functions | Advanced App Development


[...] Objective-C Math functions This entry was posted in iOS Development. Bookmark the permalink.
← UIView [...]

Leave a Response

Name (required)

Mail (will not be published) (required)

Website

About this Blog


4 of 5 Touch Code Magazine. 4/23/11 2:18 PM
Tutorials and articles about programming for iPhone, iPod touch and iPad.
Basic and Advanced Objective-C examples, Xcode and Interface Builder examples.
Objective-C Math functions http://www.touch-code-magazine.com/objective-c-math-functions/

Back to top

5 of 5 4/23/11 2:18 PM

Potrebbero piacerti anche