Using math.h

  • Hi all,

    I am writing a Cocoa app, and for one of my methods, I'd like to use
    exponential math.  For example, I want to raise 10 to the power of 3
    and store the result.  I found the pow() function in math.h, and read
    the docs in the man page.  So, in my code, I have something like:

    double a = 10;
    double b = 3;
    double c = pow(a, b);

    But when compiling my app, I get a compile error "called object 'pow'
    is not a function".

    I've added #import <math.h> to the top of my .m file.  But still
    nothing.

    How do I get pow() and the other math functions to work in my Cocoa app?

    Thanks.
    Nelson
  • Try #include, not #import:

    #include <math.h>

    It is also available in CoreServices Framework (Don't forget to add
    the framework to the project):

    #import <CoreServices/CoreServices.h>

    Julio Cesar Silva dos Santos
    <jcssantos01...>
    Skype: jcssantos01
    Blogjective-C
    http://www.jcs.santos.nom.br/wp
    FetchYourLyrics
    http://www.jcs.santos.nom.br/fyl/fyl.html

    On 14/03/2007, at 11:22, Nelson Santos wrote:

    > Hi all,
    >
    > I am writing a Cocoa app, and for one of my methods, I'd like to
    > use exponential math.  For example, I want to raise 10 to the power
    > of 3 and store the result.  I found the pow() function in math.h,
    > and read the docs in the man page.  So, in my code, I have
    > something like:
    >
    > double a = 10;
    > double b = 3;
    > double c = pow(a, b);
    >
    > But when compiling my app, I get a compile error "called object
    > 'pow' is not a function".
    >
    > I've added #import <math.h> to the top of my .m file.  But still
    > nothing.
    >
    > How do I get pow() and the other math functions to work in my Cocoa
    > app?
    >
    > Thanks.
    > Nelson
  • On Mar 14, 2007, at 10:22 AM, Nelson Santos wrote:
    > double a = 10;
    > double b = 3;
    > double c = pow(a, b);
    >
    > But when compiling my app, I get a compile error "called object
    > 'pow' is not a function".
    >
    > I've added #import <math.h> to the top of my .m file.  But still
    > nothing.

    What you did works for me.  I don't suppose you're declaring
    something else called "pow" anywhere in your code?

    --Andy
  • When I make a new sample project, all math.h functions work (and as
    far as I know all other C standard library functions), I believe that
    these libraries are included in the Cocoa framework. So there might
    be some other code of yours conflicting, do you have an object
    (variable) called "pow"? You can try to NSLog other math functions
    such as sqrt(16) or sin(3.1415) to see whether the problem is indeed
    due math.h not being included.

    Good luck, Patrick

    On 14-Mar-07, at 10:22 AM, Nelson Santos wrote:

    > Hi all,
    >
    > I am writing a Cocoa app, and for one of my methods, I'd like to
    > use exponential math.  For example, I want to raise 10 to the power
    > of 3 and store the result.  I found the pow() function in math.h,
    > and read the docs in the man page.  So, in my code, I have
    > something like:
    >
    > double a = 10;
    > double b = 3;
    > double c = pow(a, b);
    >
    > But when compiling my app, I get a compile error "called object
    > 'pow' is not a function".
    >
    > I've added #import <math.h> to the top of my .m file.  But still
    > nothing.
    >
    > How do I get pow() and the other math functions to work in my Cocoa
    > app?
    >
    > Thanks.
    > Nelson
  • Thanks everyone.  Dumb mistake on my part.

    pow was defined earlier for something else, as some of you
    mentioned.  D-Oh!

    Nelson

    On Mar 14, 2007, at 10:38 AM, Andy Lee wrote:

    On Mar 14, 2007, at 10:22 AM, Nelson Santos wrote:
    > double a = 10;
    > double b = 3;
    > double c = pow(a, b);
    >
    > But when compiling my app, I get a compile error "called object
    > 'pow' is not a function".
    >
    > I've added #import <math.h> to the top of my .m file.  But still
    > nothing.

    What you did works for me.  I don't suppose you're declaring
    something else called "pow" anywhere in your code?

    --Andy
previous month march 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Go to today