Programming with C (math library error)

  • Thread starter Thread starter SnakeDoc
  • Start date Start date
  • Tags Tags
    Error Programming
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 2K views
SnakeDoc
Messages
27
Reaction score
1

Homework Statement


I have make a program that solves the distance between 2 points on a triangle.

Homework Equations


none

The Attempt at a Solution



Code:
/* 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:
Physics news on Phys.org
SnakeDoc said:

Homework Statement


I have make a program that solves the distance between 2 points on a triangle.

Homework Equations


none

The Attempt at a Solution


/* 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?
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).
 
HallsofIvy said:
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)".
This is C, not C++, and you simply use sqrt, without a math. attached to it. You also need to #include the math header,
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:
gcc -O mymathcode.c -lm
 
DrClaude said:
This is C, not C++, and you simply use sqrt, without a math. attached to it. You also need to #include the math header,
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:
gcc -O mymathcode.c -lm
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.