pow function in math.h

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Black Eagle

    pow function in math.h

    Hey guys,

    Under linux, I tried this :
    #include <stdio.h>
    #include <math.h>
    main()
    {
    double a= 2.0;
    double b= 3.0;
    printf("%f^%f=% f",a,b,pow(a,b) );
    }


    gcc main.c -o main

    But it's not working, it says that pow function is undefined, any idea ?

    Thx
  • Martin Ambuhl

    #2
    Re: pow function in math.h

    "Black Eagle" <black.eagle@ea synet.be> wrote (09 Jul 2003) in
    news:pan.2003.0 7.09.20.51.22.6 28847@easynet.b e / comp.lang.c:
    [color=blue]
    > Hey guys,
    >
    > Under linux, I tried this :
    > #include <stdio.h>
    > #include <math.h>
    > main()
    > {
    > double a= 2.0;
    > double b= 3.0;
    > printf("%f^%f=% f",a,b,pow(a,b) );
    > }
    >
    >
    > gcc main.c -o main
    >
    > But it's not working, it says that pow function is undefined, any
    > idea ?[/color]

    Yes, that you failed to check the FAQs before posting, or that you
    did that -- as a civilized poster would -- but failed to notice

    and




    --
    Martin Ambuhl
    Returning soon to the
    Fourth Largest City in America

    Comment

    • Jatin Kukreja

      #3
      Re: pow function in math.h


      Is it better to use
      x*x

      or pow(x,2) ???

      Just asking cause someone told me that the former is better

      Tom St Denis <tomstdenis@iah u.ca> wrote:

      : Black Eagle wrote:
      :> Hey guys,
      :>
      :> Under linux, I tried this :
      :> #include <stdio.h>
      :> #include <math.h>
      :> main()
      :> {
      :> double a= 2.0;
      :> double b= 3.0;
      :> printf("%f^%f=% f",a,b,pow(a,b) );
      :> }
      :>
      :>
      :> gcc main.c -o main
      :>
      :> But it's not working, it says that pow function is undefined, any idea ?

      : try

      : gcc main.c -lm -o main

      : -lm means to link the math lib in.

      : Tom

      Comment

      • Black Eagle

        #4
        Re: pow function in math.h

        [color=blue]
        > Yes, that you failed to check the FAQs before posting, or that you
        > did that -- as a civilized poster would -- but failed to notice
        > http://www.eskimo.com/~scs/C-faq/q13.25.html
        > and
        > http://www.eskimo.com/~scs/C-faq/q14.3.html[/color]

        Sorry I didnt know that FAQ exists, Ill be checking there before posting
        now, thanks !

        Comment

        • Mark McIntyre

          #5
          Re: pow function in math.h

          On Thu, 10 Jul 2003 05:29:56 +0000 (UTC), in comp.lang.c , Jatin
          Kukreja <kukreja@epsilo n.me.iitb.ac.in > wrote:
          [color=blue]
          >
          >Is it better to use
          >x*x
          >
          >or pow(x,2) ???
          >
          >Just asking cause someone told me that the former is better[/color]

          AFAIK the Standard doesn't say. Indeed its possible for your compiler
          to optimise the latter into the former, should the 2nd parameter be an
          integer.

          If it really matters to you (and it should not) then do some
          extensive tests in your implementation and remember the results may be
          different on a different computer, compiler etc.

          --
          Mark McIntyre
          CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
          CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>


          ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
          http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
          ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

          Comment

          • John Tsiombikas (Nuclear / the Lab)

            #6
            Re: pow function in math.h

            Jatin Kukreja wrote:[color=blue]
            > Is it better to use
            > x*x
            >
            > or pow(x,2) ???
            >
            > Just asking cause someone told me that the former is better[/color]

            the first one is certainly better in terms of performance (if the
            compiler does not detect and substitute it) since you skip the function
            call overhead which is far more than the code for the simple operation
            x*x just placed inline with the rest of the code.

            I usually define something like this: #define SQ(x) ((x) * (x)) and
            use for such cases

            -- Nuclear / the Lab --

            Comment

            • Chris Dollin

              #7
              Re: pow function in math.h

              John Tsiombikas (Nuclear / the Lab) wrote:
              [color=blue]
              > Jatin Kukreja wrote:[color=green]
              >> Is it better to use
              >> x*x
              >>
              >> or pow(x,2) ???
              >>
              >> Just asking cause someone told me that the former is better[/color]
              >
              > the first one is certainly better in terms of performance (if the
              > compiler does not detect and substitute it) since you skip the function
              > call overhead[/color]

              What function call overhead? The compiler is free to inline pow if it
              likes. If it doesn't like, it may have good reason. It likely depends
              on the compiler flags.

              [If x is integral, then the results of x*x and pow(x,2) are *different*,
              so you must pick the correct one, and arguments about efficiency cut
              little grass.]
              [color=blue]
              > which is far more than the code for the simple operation
              > x*x just placed inline with the rest of the code.[/color]

              Unlikely. The overhead of pow is its generality; the function-call
              overhead might be as little as two instructions plus a pipeline
              glitch.
              [color=blue]
              > I usually define something like this: #define SQ(x) ((x) * (x)) and
              > use for such cases[/color]

              Well, let's hope your arguments to SQ are never complicated and never
              have side-effects.

              --
              Chris "electric hedgehog" Dollin
              C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
              C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html

              Comment

              • Malcolm

                #8
                Re: pow function in math.h


                "Jatin Kukreja" <kukreja@epsilo n.me.iitb.ac.in > wrote in message[color=blue]
                >
                > Is it better to use
                > x*x
                >
                > or pow(x,2) ???
                >
                > Just asking cause someone told me that the former is better
                >[/color]
                Generally x*x will be much faster, since pow() involves a call to a
                complicated function that can handle arbitrary reals. I don't believe that
                many compilers would optimise to x*x, though I'm open to correction on that
                one.

                However unless your calculation is in an inner loop, it is unlikely to make
                any noticeable difference to the program's running time. A program is a
                means of communication between programmers as well as a series of
                instructions for a computer. If you think that pow(x,2) makes your
                expression easier to read, or communicates your intention better (eg if we
                are calculating a series pow(x,0) + pow(x, 1) + pow(x, 2) + pow(x,3) ...)
                then go ahead and use it.


                Comment

                • John Tsiombikas (Nuclear / the Lab)

                  #9
                  Re: pow function in math.h

                  > What function call overhead? The compiler is free to inline pow if it[color=blue]
                  > likes. If it doesn't like, it may have good reason. It likely depends
                  > on the compiler flags.[/color]

                  Well that was my point actually... you don't know and if we're talking
                  about a performance critical part of the code, you might want to make
                  sure that it ends up to x*x by actually doing it like that yourself :)
                  [color=blue]
                  > Unlikely. The overhead of pow is its generality; the function-call
                  > overhead might be as little as two instructions plus a pipeline
                  > glitch.[/color]

                  Indeed, you're right. So we're talking for even greater overhead (I
                  would do it x*x, even if it was just the function call overhead
                  involved, in an inner loop).

                  -- Nuclear / the Lab --

                  Comment

                  Working...