1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Programming with C please help (math library error)

  1. Sep 1, 2015 #1
    1. The problem statement, all variables and given/known data
    I have make a program that solves the distance between 2 points on a triangle.

    2. Relevant equations
    none

    3. The attempt at a solution

    Code (Text):

    /* homework 1 */

    #include <stdio.h>
    #include <math.h>

    int main(void)
    {
    // Declare and initialize variables
      float x1=1, y1=5, x2=4, y2=7;
      float dx, dy, distance;

    // Compute sides of a right triangle
      dx = x2 - x1;
      dy = y2 - y1;

      distance = sqrt (dx*dx + dy*dy);

    // Print distance
      printf("The distance between the two points is: %5.2f \n",distance);

    // Exit program
    return(0);
    }  
    I keep getting a an error about an undefined reference to sqrt
    Did I do something wrong?
     
    Last edited by a moderator: Sep 7, 2015
  2. jcsd
  3. Sep 1, 2015 #2

    Mark44

    Staff: Mentor

    Your program runs fine for me, although I get this warning:

    warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

    due to the fact that sqrt() returns a double, and you're storing the returned value in a smaller type. I suspect you don't have things set up correctly so that the compiler can't find the header files (math.h in particular).
     
  4. Sep 2, 2015 #3

    DrClaude

    User Avatar

    Staff: Mentor

  5. Sep 3, 2015 #4

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    I use Pascal rather than C++ but in Pascal, if you reference a "math" library in order to use the "sqrt" function, you would have to use "math.sqrt(dx*dx+ dy*dy)".
     
  6. Sep 3, 2015 #5

    DrClaude

    User Avatar

    Staff: Mentor

    This is C, not C++, and you simply use sqrt, without a math. attached to it. You also need to #include the math header,
    Code (C):

    #include <math.h>
     
    For historical reasons, the math functions are not part of the standard library, so the math library must be specifically linked to. For instance
    Code (Text):

    gcc -O mymathcode.c -lm
     
     
  7. Sep 3, 2015 #6

    Mark44

    Staff: Mentor

    I'm guessing that the OP doesn't have his compiler installation set up correctly, with the linker unable to find the code for the math library (DrClaude's earlier suggestion). Less likely, I think, is my earlier suggestion that the compiler can't find the headers, but this is still a possibility.
     
  8. Sep 7, 2015 #7
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Programming with C please help (math library error)
Loading...