Mathematic Operations in Cocoa

Discussion in 'Mac Programming' started by Taco Bell, Mar 5, 2010.

  1. Taco Bell macrumors newbie

    Mar 4, 2010
    I'm fairly experienced in ANSI C. I actively program microcontrollers for robots. This requires an immense amount of mathematical operation, as does most programming. But Cocoa doesn't seem to support even the most simple mathematical operations. So, can someone tell me how to perform mathematical operations in Cocoa?

    I have tried to make an application that adds two number together. I made an interface that that consists of three text fields, two for the numbers being added, one for the sum, and a button to initiate the calculation. But that as far as I got.
  2. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
  3. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    5 + 5
    like so?

    To be less snarky, Cocoa is an API. The language is Objective-C which is a strict superset of C. You can use all the C operators you're familiar with.
  4. Taco Bell thread starter macrumors newbie

    Mar 4, 2010
    How do you cast a number form a NSTextField to an integer?
  5. HiRez macrumors 603


    Jan 6, 2004
    Western US
    As Lee pointed out, you can just use the text field's intValue: method (inherited from NSControl):

    int i = [myTextField intValue];
    Or you might want to get the value as an NSString first using stringValue: to do some formatting and error-checking on it (or place an NSNumberFormatter on the field), then convert the value using NSString's intValue: method.
  6. chown33 macrumors 604

    Aug 9, 2009
    Using C types and C arithmetic.

    If you're not working from a tutorial or guided instruction of some kind, making up your own is almost certainly going to fail.

    See Apple's introductory tutorial:

    If your example of an application that adds two numbers together is from a tutorial, please post its URL. It may expect a prerequisite.
  7. BadWolf13 macrumors 6502

    Dec 17, 2009
  8. chown33 macrumors 604

    Aug 9, 2009
    They're done exactly the same way they're done in standard C: with functions in the math library.

    exponentiation is the pow() function.

    trig functions: sin(), cos(), atan2(), etc.

    A local (non-web) version of the man pages is available using Xcode; there's a man page menu-item under its Help menu.

    Finally, I find this program useful for reading man pages:

    It shows man pages in your browser using a man: url scheme. It even responds to the command-line 'open man:whatever' by opening the man page in the browser.

Share This Page