Mathematic Operations in Cocoa

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

  1. Taco Bell macrumors newbie

    Joined:
    Mar 4, 2010
    #1
    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

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
  3. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #3
    Code:
    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

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

    HiRez

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

    Code:
    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

    Joined:
    Aug 9, 2009
    #6
    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:
    http://developer.apple.com/mac/libr...jCTutorial/01Introduction/01Introduction.html

    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

    Joined:
    Dec 17, 2009
    #7
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    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.

    http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man3/math.3.html

    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:
    http://www.bruji.com/bwana/

    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