Programming with C (math library error)

In summary, your code runs fine, but you may want to link to the math library to be able to use the sqrt() function.
  • #1
SnakeDoc
27
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
  • #2
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).
 
  • #4
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)".
 
  • #5
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
 
  • #6
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.
 
  • #7

FAQ: Programming with C (math library error)

What is a math library error in programming with C?

A math library error in programming with C refers to an error that occurs when using functions from the math library in C. This can happen when there is a mistake in the code, such as using the wrong function or passing incorrect parameters.

How do I fix a math library error in C programming?

To fix a math library error in C programming, you should carefully review your code and check for any mistakes or typos. Make sure you are using the correct function and passing in the correct parameters. You may also need to consult the documentation for the math library functions to ensure you are using them correctly.

What are some common causes of math library errors in C programming?

Some common causes of math library errors in C programming include using the wrong function, passing incorrect parameters, or not including the necessary header file for the math library. It is also possible to encounter math library errors if your code has logical errors or typos.

How can I prevent math library errors in C programming?

To prevent math library errors in C programming, it is important to carefully read and understand the documentation for the math library functions you are using. Make sure you are using the correct function and passing in the correct parameters. Additionally, writing clean and organized code can help reduce the chances of encountering math library errors.

Can I debug a math library error in C programming?

Yes, you can debug a math library error in C programming by using a debugger tool or by implementing error-handling techniques in your code. This can help you identify the source of the error and make necessary corrections to fix it. It is also helpful to use print statements to track the values of variables and functions being used in your code.

Similar threads

Replies
1
Views
1K
Replies
10
Views
2K
Replies
1
Views
2K
Replies
1
Views
9K
Replies
7
Views
1K
Back
Top