#include <math.h>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Darius Fatakia

    #include <math.h>

    Hi,

    I seem to be having trouble with some of my math functions (pow, sqrt,
    acos). They're the only ones I use in my code and they prevent the program
    from compiling. I get a "undefined reference to 'pow'" error. Here is the
    relevant portion of my code.

    Your help would be appreciated. Thanks!

    * Genetic Algorithm module
    *
    */

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #include "string.h"


    /* GetSD
    *
    * This function returns the standard deviation of the feature vectors
    * in a dataT * array.
    */
    static double GetSD(dataT *dataArr[], int data_size, int features[], double
    mean[]) {
    int i, j;
    double data_vect[GENES_PER_CHROM], data_sum, mean_sum, ang_sum, dotprod,
    data_norm, mean_norm, angle;
    double sd;

    ang_sum = 0;

    for(i = 0; i < data_size; i++) {
    for(j = 0; j < GENES_PER_CHROM ; j++) {
    data_vect[j] = dataArr[i]->val[(features[j])];
    }

    // get dot product of the two vectors
    dotprod = 0;
    for(j = 0; j < GENES_PER_CHROM ; j++) {
    dotprod += data_vect[j] * mean[j];
    }

    // get the norms
    data_sum = 0;
    mean_sum = 0;
    for(j = 0; j < GENES_PER_CHROM ; j++) {
    data_sum += pow(data_vect[j], 2);
    mean_sum += pow(mean[j], 2);
    }
    data_norm = sqrt(data_sum);
    mean_norm = sqrt(mean_sum);

    // compute the angle
    angle = acos(dotprod/(data_norm * mean_norm));

    ang_sum += pow(angle, 2);
    }

    sd = sqrt(ang_sum / (data_size - 1));

    return sd;
    }


  • Alex

    #2
    Re: #include &lt;math.h&g t;

    Darius Fatakia <darius_fatakia @yahoo.com> wrote:[color=blue]
    > Hi,[/color]
    [color=blue]
    > I seem to be having trouble with some of my math functions (pow, sqrt,
    > acos). They're the only ones I use in my code and they prevent the program
    > from compiling. I get a "undefined reference to 'pow'" error. Here is the
    > relevant portion of my code.[/color]

    You are most likely having a linking problem. Read your system
    documentation to find out how to link in the math library.

    <OT> Appending -lm to your compile line might work. </OT>

    Alex

    Comment

    • E. Robert Tisdale

      #3
      Re: #include &lt;math.h&g t;

      Darius Fatakia wrote:
      [color=blue]
      > I seem to be having trouble with some of my math functions (pow, sqrt,
      > acos). They're the only ones I use in my code and they prevent the program
      > from compiling. I get a "undefined reference to 'pow'" error. Here is the
      > relevant portion of my code.[/color]
      [color=blue]
      > cat gam.c[/color]
      typedef struct dataT {
      double* val;
      } dataT;

      #define GENES_PER_CHROM 1024

      /*
      * Genetic Algorithm module
      */

      #include <stdio.h>
      #include <stdlib.h>
      #include <time.h>
      #include <math.h>
      #include "string.h"


      /* GetSD
      *
      * This function returns the standard deviation
      * of the feature vectors in a dataT* array.
      */

      static
      double GetSD(dataT* dataArr[],
      int data_size, int features[], double mean[]) {
      double data_vect[GENES_PER_CHROM];
      double ang_sum = 0.0;

      for(int i = 0; i < data_size; ++i) {
      for(int j = 0; j < GENES_PER_CHROM ; ++j) {
      data_vect[j] = dataArr[i]->val[(features[j])];
      }

      // get dot product of the two vectors
      double dotprod = 0.0;
      for(int j = 0; j < GENES_PER_CHROM ; ++j) {
      dotprod += data_vect[j]*mean[j];
      }

      // get the norms
      double data_sum = 0.0;
      double mean_sum = 0.0;
      for(int j = 0; j < GENES_PER_CHROM ; ++j) {
      data_sum += pow(data_vect[j], 2);
      mean_sum += pow(mean[j], 2);
      }
      double data_norm = sqrt(data_sum);
      double mean_norm = sqrt(mean_sum);

      // compute the angle
      double angle = acos(dotprod/(data_norm*mean _norm));
      ang_sum += pow(angle, 2);
      }
      return sqrt(ang_sum/(data_size - 1));
      }

      int main(int argc, char* argv[]) {
      dataT* dataArr[GENES_PER_CHROM];
      int data_size = 1024;
      int features[1024];
      double mean[GENES_PER_CHROM];
      GetSD(dataArr, data_size, features, mean);

      return 0;
      }
      [color=blue]
      > gcc -Wall -std=c99 -pedantic -o gam gam.c -lm[/color]

      If you include math.h, you need to link in libm.a with the -lm option.

      Comment

      • Kelsey Bjarnason

        #4
        Re: #include &lt;math.h&g t;

        On Mon, 24 Nov 2003 16:52:07 -0800, Darius Fatakia wrote:
        [color=blue]
        > Hi,
        >
        > I seem to be having trouble with some of my math functions (pow, sqrt,
        > acos). They're the only ones I use in my code and they prevent the program
        > from compiling. I get a "undefined reference to 'pow'" error. Here is the
        > relevant portion of my code.[/color]

        Sounds like a linker error - are you linking the math libraries? There's
        a certain, popular compiler, especially widely used in the Linux world,
        which has the terminally brain-dead habit of not including the math
        libraries by default. Assuming you're using this compiler, try something
        like this:

        gcc -lm file.c


        Comment

        • jacob navia

          #5
          Re: #include &lt;math.h&g t;

          I wonder how many HUNDREDS of times I have seen this question
          pop up.

          Are there any gcc fans out here?

          Wouldn't it be time to include that library as a default library????

          eh? !!!

          This bug is similar to the make utility bug. Tabs are used as
          separator
          in "make", and the makefile will not work if they are substituted by
          spaces!

          This makes every novice spend hours trying to find out why two
          makefiles that look the
          same do not work.


          Comment

          • Dan Pop

            #6
            Re: #include &lt;math.h&g t;

            In <pan.2003.11.25 .02.05.07.38742 4@lightspeed.bc .ca> Kelsey Bjarnason <kelseyb@lights peed.bc.ca> writes:
            [color=blue]
            >On Mon, 24 Nov 2003 16:52:07 -0800, Darius Fatakia wrote:
            >[color=green]
            >> I seem to be having trouble with some of my math functions (pow, sqrt,
            >> acos). They're the only ones I use in my code and they prevent the program
            >> from compiling. I get a "undefined reference to 'pow'" error. Here is the
            >> relevant portion of my code.[/color]
            >
            >Sounds like a linker error - are you linking the math libraries? There's
            >a certain, popular compiler, especially widely used in the Linux world,
            >which has the terminally brain-dead habit of not including the math
            >libraries by default. Assuming you're using this compiler, try something
            >like this:
            >
            >gcc -lm file.c[/color]

            gcc merely follows the common Unix convention, established by a certain
            Dennis M. Ritchie.

            Dan
            --
            Dan Pop
            DESY Zeuthen, RZ group
            Email: Dan.Pop@ifh.de

            Comment

            • Dan Pop

              #7
              Re: #include &lt;math.h&g t;

              In <bpv7s2$e9q$1@n ews-reader3.wanadoo .fr> "jacob navia" <jacob@jacob.re mcomp.fr> writes:
              [color=blue]
              >I wonder how many HUNDREDS of times I have seen this question
              >pop up.
              >
              >Are there any gcc fans out here?[/color]

              Why should gcc behave differently than most other Unix compilers?
              [color=blue]
              >Wouldn't it be time to include that library as a default library????[/color]

              Nope, but it would be high time to include the contents of libm.a into
              libc.a and provide an empty libm.a, for backward compatibility purposes.
              The *good* reasons for keeping it separate have no longer been valid for
              the last 15 years or so.

              Which has precisely zilch to do with gcc or any other Unix compiler.

              Dan
              --
              Dan Pop
              DESY Zeuthen, RZ group
              Email: Dan.Pop@ifh.de

              Comment

              • Mark McIntyre

                #8
                Re: #include &lt;math.h&g t;

                On Tue, 25 Nov 2003 10:51:38 +0100, in comp.lang.c , "jacob navia"
                <jacob@jacob.re mcomp.fr> wrote:
                [color=blue]
                >I wonder how many HUNDREDS of times I have seen this question
                >pop up.[/color]

                thats why its a FAQ....

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

                Comment

                • Mark McIntyre

                  #9
                  Re: #include &lt;math.h&g t;

                  On Mon, 24 Nov 2003 16:52:07 -0800, in comp.lang.c , "Darius Fatakia"
                  <darius_fatakia @yahoo.com> wrote:
                  [color=blue]
                  > "undefined reference to 'pow'" error.[/color]

                  This is a FAQ. Please read the FAQs

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

                  Comment

                  • Joe Wright

                    #10
                    Re: #include &lt;math.h&g t;

                    jacob navia wrote:[color=blue]
                    >[/color]
                    [ snip ][color=blue]
                    > This bug is similar to the make utility bug. Tabs are used as
                    > separator
                    > in "make", and the makefile will not work if they are substituted by
                    > spaces!
                    >
                    > This makes every novice spend hours trying to find out why two
                    > makefiles that look the
                    > same do not work.[/color]

                    I'm not sure I agree that it's a bug but it is annoying. make simply
                    uses TAB as a separator between the Rule and the Command portions of a
                    statement. As I use edit.com with tabs set to 3 this annoyed the hell
                    out of me too. Reading the info file on make one day (I'm surprised how
                    little I do this and how much I should) I read that ';' semicolon is
                    also a Rule / Command separator. How 'bout this..

                    # Use GNU make to build an executable for testing GE.

                    cc=gcc
                    obj=ge.o main.o
                    exe=main.exe
                    opt=-W -Wall -ansi -pedantic -O2 -c

                    # Note that semicolon (not TAB) separates Rule from Command on a line.

                    $(exe) : $(obj) ; $(cc) -s $(obj) -o $(exe)
                    ge.o : ge.c ; $(cc) $(opt) ge.c
                    main.o : main.c ge.h ; $(cc) $(opt) main.c

                    Live and learn.
                    --
                    Joe Wright http://www.jw-wright.com
                    "Everything should be made as simple as possible, but not simpler."
                    --- Albert Einstein ---

                    Comment

                    • Dan Pop

                      #11
                      Re: #include &lt;math.h&g t;

                      In <3FC3EE89.18@ea rthlink.net> Joe Wright <joewwright@ear thlink.net> writes:
                      [color=blue]
                      >out of me too. Reading the info file on make one day (I'm surprised how
                      >little I do this and how much I should) I read that ';' semicolon is
                      >also a Rule / Command separator.[/color]

                      But is this a make feature or a GNU make feature?

                      Dan
                      --
                      Dan Pop
                      DESY Zeuthen, RZ group
                      Email: Dan.Pop@ifh.de

                      Comment

                      • Randy Howard

                        #12
                        Re: #include &lt;math.h&g t;

                        In article <bq24e6$lbl$14@ sunnews.cern.ch >, Dan.Pop@cern.ch says...[color=blue]
                        > In <3FC3EE89.18@ea rthlink.net> Joe Wright <joewwright@ear thlink.net> writes:
                        >[color=green]
                        > >out of me too. Reading the info file on make one day (I'm surprised how
                        > >little I do this and how much I should) I read that ';' semicolon is
                        > >also a Rule / Command separator.[/color]
                        >
                        > But is this a make feature or a GNU make feature?
                        >
                        > Dan
                        >[/color]

                        Is there an ANSI standard on Make and makefiles? Or, are we free to
                        assume that GNU make is the "one true make" and proceed thusly?

                        --
                        Randy Howard _o
                        2reply remove FOOBAR \<,
                        _______________ _______()/ ()_____________ _______________ _______________ ___
                        SCO Spam-magnet: postmaster@sco. com

                        Comment

                        • Peter Shaggy Haywood

                          #13
                          Re: #include &lt;math.h&g t;

                          Groovy hepcat Darius Fatakia was jivin' on Mon, 24 Nov 2003 16:52:07
                          -0800 in comp.lang.c.
                          #include <math.h>'s a cool scene! Dig it!
                          [color=blue]
                          >I seem to be having trouble with some of my math functions (pow, sqrt,
                          >acos). They're the only ones I use in my code and they prevent the program
                          >from compiling. I get a "undefined reference to 'pow'" error. Here is the
                          >relevant portion of my code.[/color]

                          It is the height of rudeness to post to a newsgroup without doing
                          both of the following:

                          1) read all posts in the newsgroup for a month or two, and
                          2) read the newsgroup's FAQ.

                          Please do these two things before posting again.

                          --

                          Dig the even newer still, yet more improved, sig!


                          "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
                          I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?

                          Comment

                          • Mark McIntyre

                            #14
                            Re: #include &lt;math.h&g t;

                            On Wed, 26 Nov 2003 13:27:04 -0600, in comp.lang.c , Randy Howard
                            <randy.howard@F OOmegapathdslBA R.net> wrote:[color=blue]
                            >
                            >Is there an ANSI standard on Make and makefiles? Or, are we free to
                            >assume that GNU make is the "one true make" and proceed thusly?[/color]

                            Its a great assumption. It will work perfectly.

                            Right up to the point at which you port your makefile to some other
                            compiler environment. Heck, ISTR that makefiles from the C compiler
                            shipped with SunOS 4.1.3 didn't work with the Sparc C++ compiler.
                            --
                            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

                            • Joe Wright

                              #15
                              Re: #include &lt;math.h&g t;

                              Dan Pop wrote:[color=blue]
                              >
                              > In <3FC3EE89.18@ea rthlink.net> Joe Wright <joewwright@ear thlink.net> writes:
                              >[color=green]
                              > >out of me too. Reading the info file on make one day (I'm surprised how
                              > >little I do this and how much I should) I read that ';' semicolon is
                              > >also a Rule / Command separator.[/color]
                              >
                              > But is this a make feature or a GNU make feature?
                              >[/color]
                              Certainly GNU make because I read it there and tried it and it worked. I
                              cannot know and don't care much about all other make programs. By the
                              same token I don't know that TAB is the separator other make programs.
                              To the extent that TAB was chosen by make's original author (Who?) it
                              seems arbitrary and badly chosen.
                              --
                              Joe Wright http://www.jw-wright.com
                              "Everything should be made as simple as possible, but not simpler."
                              --- Albert Einstein ---

                              Comment

                              Working...