PDA

View Full Version : Basic Math in Objective C




nashyo
Feb 6, 2012, 07:14 AM
Hello macrumours!

I realise I might get an empty can of diet pepsi thrown at my head for asking this, but here goes.

I need to do some calculations in my objective-c code, and I am looking for a good source of information. The following link lists basic c++ functions that I can use, but I need to consider the use of classes like NSInteger and NSNumber.

http://www.cplusplus.com/reference/clibrary/cmath/

If I wanted to implement the following calculation in objective c, what would be the best way to write a method? Does anyone have any good online free resources I can read up?

y = (1 - ex) / ex
where ex is e to the power of x
where x = 5 + b

Thanks



robbieduncan
Feb 6, 2012, 07:26 AM
1) Objective-C is a perfect superset of C (not C++, C). Any C function can be used (assuming you import the correct header). So you can use math.h (http://www.cplusplus.com/reference/clibrary/cmath/) for example

2) NSInteger is not a class: it's simply a type def to a primitive C type. So standard C functions can be used. NSNumber is class on the other hand. My personal advice would be to deal with C primitive types as much as possible for maths.

mobilehaathi
Feb 6, 2012, 08:42 AM
Definitely just stick with C primitive types for calculations.

nashyo
Feb 6, 2012, 08:49 AM
1) Objective-C is a perfect superset of C (not C++, C). Any C function can be used (assuming you import the correct header). So you can use math.h (http://www.cplusplus.com/reference/clibrary/cmath/) for example

2) NSInteger is not a class: it's simply a type def to a primitive C type. So standard C functions can be used. NSNumber is class on the other hand. My personal advice would be to deal with C primitive types as much as possible for maths.

Thanks for this.

nashyo
Feb 7, 2012, 05:21 AM
My code is a little messy, but it works. I have fuctions that accept int's as argument, and return void. However these functions assign values to properties that I've synthesized. This has allowed me to use these assigned properties in other methods that take the calculation one step further.

I have repeated this cycle several times and I now have a several properties that represent intermediate calculations. The code is messy and can most certainly be improved.

Does any one have any advice? How could I draft a function in objective-C for the following expression, in one method?

ex/(1-ex) = result
where x = -9 + (0.5A) + (0.4B)
A and B are variables that depend on user input.

dejo
Feb 7, 2012, 09:54 AM
I gotta ask: what have you done to educate yourself on the fundamentals of Objective-C programming? Please be as specific as possible.

nashyo
Feb 7, 2012, 10:29 AM
To educate myself, I used the link to math.h above. It was recommended.

KnightWRX
Feb 7, 2012, 10:43 AM
To educate myself, I used the link to math.h above. It was recommended.

I think he meant in general. You've basically been asking so many questions on basic things, it seems you have yet to read a book or tutorial about the language.

What books have you read ? What tutorials have you followed ? What documentation have you consulted before turning to this forum for help ?

nashyo
Feb 7, 2012, 11:01 AM
I think he meant in general. You've basically been asking so many questions on basic things, it seems you have yet to read a book or tutorial about the language.

What books have you read ? What tutorials have you followed ? What documentation have you consulted before turning to this forum for help ?

I misunderstood what this forum was about and I actually take offence to this. I've spent >100 hours in lynda.com tutorials and three books. I have developed two apps in that time, and only the most recent required math.h.

KnightWRX
Feb 7, 2012, 11:07 AM
I misunderstood what this forum was about and I actually take offence to this.

Don't get oversensitive. This forum is about helping, but not writing your code/homework/apps for you. Basically, you should first search the documentation, search online, read your books, try to write some code and then come here for help when all else fails.

For example, just going to Google and searching for "Objective-C Math" and clicking the very first link that pops up gives this tidbit on the first line of the article :

http://www.touch-code-magazine.com/objective-c-math-functions/
Objective-C uses C’s math which you can find in math.h

From this thread, the posters are getting the impression you didn't even bother to look it up before asking, which makes it seem like you think this forum is a "goto 1st" ressource, when it should be a "goto last" one. In the end, this is not for us, it's for you. If you read documentation, search online, more often than not, you can get your answer much faster than asking here, which would greatly benefit you. Don't think I'm scolding you or mad, it's just that the method you've chosen of just asking question is quite innefficient for both us and you in the end.

nashyo
Feb 7, 2012, 11:12 AM
I appreciate what your saying, but I still misunderstood this forum. I went here as a go to first option because it was also a conversation starter - to rouse general chit-chat.

I realise now that I shouldn't do this.

chown33
Feb 7, 2012, 11:30 AM
I have developed two apps in that time, and only the most recent required math.h.

It's not about math. It's about how to write C functions, which are fundamental building blocks.

For example, you said that the methods you wrote return void, and store their results in properties. So you apparently don't know how or why a method (or the simpler C function) would or should return a type other than void.

Taking your example:
where x = -9 + (0.5A) + (0.4B)

here's the declaration of a C function:
float nashyo_x( float A, float B );

Here's the definition of the function:
float nashyo_x( float A, float B )
{ return -9 + 0.5 * A + 0.4 * B; }

I assume you know the C arithmetic operators, so the translation from (0.5A) to 0.5 * A should be obvious.

You call the function like this:
float my_x = nashyo_x( someAvalue, someBvalue );

where someAvalue is a variable already defined, whose value you wish to use in the calculation.


If anything above is either new or surprising to you, then you need to study what C functions are, and how to make them. Make sure you understand what the difference between a parameter or arg is, compared to a property or instance variable. You should also study how functions or methods return values, because if you define everything as a function or method returning void, you're missing half the point of functions and methods.

Once you know how C functions are defined, and how to call them, you should review how methods are defined and how they return values.

dejo
Feb 7, 2012, 11:39 AM
I've spent >100 hours in lynda.com tutorials and three books.

This is what I was asking, kind of. Except you didn't take my advice to be as specific as possible. For example: "three books" is far too vague for us to understand what those resources are like. As a courtesy, you should inform us of their titles, authors, and editions.

chrono1081
Feb 7, 2012, 06:25 PM
To educate myself, I used the link to math.h above. It was recommended.

Have a look at this book OP:

Its great for beginners or for someone (like me) coming from other languages:

http://www.amazon.com/Objective-C-Programming-Ranch-Guide-Guides/dp/0321706285

Followed by this book which is great to read through and keep around as a reference:

http://www.amazon.com/Programming-Objective-C-4th-Developers-Library/dp/0321811909/ref=sr_1_2?s=books&ie=UTF8&qid=1328660609&sr=1-2

NOTE: I haven't read the version linked to you yet (its on my desk more as a reference) but I read through is second edition and its very good.

Although the Lynda.com tuts are nice, they're more of a brief over-view for people coming from other languages rather than a new tutorial for beginners.

nashyo
Feb 8, 2012, 05:22 AM
I have watched all of the video tutorials for the follwing topics at Lynda.com:

iOS 3 SDK
iOS 5 SDK new features
Xcode 4 new features


I have only read 1 book from front to back:

Objective C For Dummies by Neal Goldstein


However, I have used the following books for reference

Sam's teach yourself iOS 5 in 24 hours by John Ray
Beginning iOS 5 development by Dave, Jeff and Jack


I have now compressed 7 methods into 1 method, that returns a float. Great!

robbieduncan
Feb 8, 2012, 05:24 AM
Have you got any experience of C (not Objective-C)? Given that you are working with C functions and primatives I'd suggest it might be good to gain or refresh your knowledge in that area...

Regards,
Robbie

chrono1081
Feb 8, 2012, 06:37 AM
I have watched all of the video tutorials for the follwing topics at Lynda.com:

iOS 3 SDK
iOS 5 SDK new features
Xcode 4 new features


I have only read 1 book from front to back:

Objective C For Dummies by Neal Goldstein


However, I have used the following books for reference

Sam's teach yourself iOS 5 in 24 hours by John Ray
Beginning iOS 5 development by Dave, Jeff and Jack


I have now compressed 7 methods into 1 method, that returns a float. Great!

I would definitely suggest using the Big Nerd Ranch Objective-C book I posted above. It starts you out with C, then moves to Objective-C, then a little bit of iOS (but iOS is more for another book).

You don't have to be an expert at C, but its very important to know since many things you program in Objective-C will require knowledge of C.

nashyo
Feb 10, 2012, 02:42 AM
I would definitely suggest using the Big Nerd Ranch Objective-C book I posted above. It starts you out with C, then moves to Objective-C, then a little bit of iOS (but iOS is more for another book).

You don't have to be an expert at C, but its very important to know since many things you program in Objective-C will require knowledge of C.

thanks, i'll check this book out. i haven't focused much on c. i realised that c could be compiled by xcode, but i wasn't aware there was so much c in objective-c. ha, that sounds dumb.