Basic Math in Objective C

Discussion in 'iPhone/iPad Programming' started by nashyo, Feb 6, 2012.

  1. macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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 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.
     
  3. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #3
    Definitely just stick with C primitive types for calculations.
     
  4. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #4
    Thanks for this.
     
  5. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #5
    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.
     
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    I gotta ask: what have you done to educate yourself on the fundamentals of Objective-C programming? Please be as specific as possible.
     
  7. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #7
    To educate myself, I used the link to math.h above. It was recommended.
     
  8. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #8
    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 ?
     
  9. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #9
    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.
     
  10. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #10
    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/
    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.
     
  11. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #11
    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.
     
  12. macrumors 603

    Joined:
    Aug 9, 2009
    #12
    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:
    here's the declaration of a C function:
    Code:
    float nashyo_x( float A, float B );
    
    Here's the definition of the function:
    Code:
    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:
    Code:
    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.
     
  13. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    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.
     
  14. macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #14
    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.
     
  15. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #15
    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!
     
  16. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #16
    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
     
  17. macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #17
    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.
     
  18. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #18
    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.
     

Share This Page