Objective-C Math functions

Math functions

double pow ( double, double ) – power of

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

double  sqrt( double ) – square root

NSLog(@"%.f", sqrt(16) ); //result 4
NSLog(@"%.f", sqrt(81) ); //result 9

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

double floor ( double ) – removes the decimal part of the argument

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

double 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

double fmin ( double, double ) – returns the smaller argument

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

double fmax ( double, double ) – returns the bigger argument

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

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

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

Eventually you will find also all the trigonometry you’ll need as: sin, cos, tan, atan, and their variations.

Few math constants

As found in the math.h

#define M_E         2.71828182845904523536028747135266250   /* e */
#define M_LOG2E     1.44269504088896340735992468100189214   /* log 2e */
#define M_LOG10E    0.434294481903251827651128918916605082  /* log 10e */
#define M_LN2       0.693147180559945309417232121458176568  /* log e2 */
#define M_LN10      2.30258509299404568401799145468436421   /* log e10 */
#define M_PI        3.14159265358979323846264338327950288   /* pi */
#define M_PI_2      1.57079632679489661923132169163975144   /* pi/2 */
#define M_PI_4      0.785398163397448309615660845819875721  /* pi/4 */
#define M_1_PI      0.318309886183790671537767526745028724  /* 1/pi */
#define M_2_PI      0.636619772367581343075535053490057448  /* 2/pi */
#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)

So, this is a wrap up for the overview of Objective-C math, if I missed the one function you use the most, feel free to leave a comment and share it with the other readers.

display latitude or longitude first

My next app will be a tracker, similar to a project I saw on hacker news a few weeks ago. This is a simple project, I already have the code to get your position, I use it in my Travel Guides, to see how far away you are from all the must see locations, and points of interest.

This app will be much simpler, and lets you track your movements until you ask it to stop, even when the app is closed. So this app will use iPhone multitasking, which is as easy as setting a tag in the plist file for the project.

You can see a list of the previous places you have been to, and I was wondering what is the convention for displaying your GPS location. The correct way to display your location is to put the latitude (North or South) first, and the longitude (East or West) second.

Google not so great at search?

Well, google sets the standard for its web search, but try searching on its android market website and you will be disappointed.

Here is a simple search, for anyone thinking about heading to the US, ‘USA Travel Guide’. Click on that link to open a new window. Currently it returns 271 results, but as we will see, the results don’t often have much to do with USA or travel guides.

The first set of results appear below, and it is not until we get to the fourth spot do we see a US travel guide. The first and third are not even travel guides, and the second is a travel guide to Barcelona? Same problem with the fifth entry, which is a travel guide to Bangkok? How do there get in there.

There are 24 entries on the first page, 12 of them are travel guides to completely different countries and cities, from Athens to Zurich, 2 of them are not even travel guides at all, and only 9 are of actual use, and most of them are towards the bottom of the page. The apps that are actual travel guides for the US are US Mobile Guide, New York City Guide, Tourist Eye Travel Guide, Washington D.C. Travel Guide, XLR8R Scion City Guide, Boston Travel Guide, Audio guide Toozla, Triposo Travel Guide and our app USA Travel Guide.

Our app, the app with the exact name, comes in at number 20.

I certainly expected more from Google.

The end of the world, Sydney, Australia

A dust storm hit Sydney, amazing pictures. The red dust obscured everything. This happens maybe every 10 years.

The dust hung around until early afternoon. Would have been a very bad time to have the bridge walk booked.

I had never experienced this before, during all my years in Australia, but it is the driest continent, you are going to get stuff like this happening. Amazingly, I had no problem breathing in this stuff.

Apple users buying 61% more apps, paying 14% more per app

The appetite for cool software is increasing, and customers on the iPhone and iPad are willing to pay more for it, much more than on the android platform.

One of the reason that our travel guides have become so popular on the device, is that people are much happier to pay for quality apps.

Just see how many apps have taken off on your favorite platform. This bodes well for the future.

Helicopter flight in New Zealand

The best way to walk on a glacier is to catch a helicopter up half way. That way, you get to walk with the best ice, rather than the dirty ice at the bottom. Somehow I managed to convince Kate to come up with me on the helicopter.

She didn’t quite appreciate me telling the pilot to do some tricks on the way back, especially when he just cleared the tree line by a few meters.

Crazy old video, showing two people come out of the ocean, walking from the ocean floor, chatting with a man, then he joins them by walking into the lake. He was never heard from again.