Programming with C (math library error)

  • Thread starter Thread starter SnakeDoc
  • Start date Start date
  • Tags Tags
    Error Programming
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to the use of the math library in C for calculating the distance between two points. Participants explore the error encountered when attempting to use the sqrt function, focusing on linking the math library and compiler settings.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster (OP) reports an error about an undefined reference to sqrt when running their program.
  • One participant notes that the program runs fine for them but warns about a potential data loss issue due to storing a double in a float.
  • Another participant asks if the OP linked to the math library using the -lm flag.
  • A participant with experience in Pascal compares the syntax for using the math library in Pascal to that in C, emphasizing the difference in how functions are called.
  • Another participant reiterates the need to include the math header and link the math library, providing an example of the correct gcc command.
  • One participant speculates that the OP's compiler setup might be incorrect, affecting the ability to find the math library.
  • The OP later confirms that linking the math library with the -lm flag resolved their issue, indicating they had overlooked this requirement in the instructions.

Areas of Agreement / Disagreement

There is no consensus on the exact cause of the OP's initial issue, but participants agree on the necessity of linking the math library and including the appropriate header. Multiple viewpoints on the syntax and linking requirements are presented.

Contextual Notes

Participants mention potential issues with compiler setup and linking, but do not resolve the specifics of the OP's environment or the implications of using different programming languages.

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).
 
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)".
 
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.
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
25
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 25 ·
Replies
25
Views
15K
Replies
9
Views
2K