# Declaring trig functions?

This is a discussion on Declaring trig functions? within the C Programming forums, part of the General Programming Boards category; Hi. My teacher has given my class this assignment: Try writing a program that simulates the following scenario. A robot ...

1. ## Declaring trig functions?

Hi. My teacher has given my class this assignment:

Try writing a program that simulates the following scenario. A robot is placed at the 50 yard line of a football field. The robot travels at 0.05 yards/second towards one of the end zones (i.e. from 50 yards to zero). Every second, the robot tries to shoot a ball into a soccer goal located in the end zone. However, the robot has a slight flaw, and each ball is shot out between +30◦ and −30◦. All angles in this range are equally probable, and the soccer goal is 3 yards wide. By the time the robot reaches the end zone, how many balls have gone into the goal? Try running your simulation many times with different random number seeds. What can you say about the statistical error in your simluation?
I wrote this code (sorry about the weird tabbing, xming wouldn't let me copy so I had to take it from notepad):

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

main() {

int location = 50;
int goals = 0;
int counter = 0;
float angle, width;
float pi = M_PI;
srand48(42);

for(counter = 0; counter < 1000; counter++) {

//find a random angle between -pi/6 and pi/6
angle = ((pi/3) * drand48() - (pi/6));

//find where the ball will land
width = location * tan(angle);

//take the absolute value
width = abs(width);

//see if it made it into the goal
if (width <= 1.5) {
goals ++;
}

//move the robot
location = location - 0.05;
}

printf("Goals: %d\n", goals);
}```
I'm getting an error saying that I didn't declare the tan function, but none of the sources I've found online have agreed with how it needs to be declared. Can anybody help? Thank you!

2. Posting exact error messages would help.

But I'm going to guess it was unresolved symbol.

In which case, try
gcc prog.c -lm
That's MINUS ELL EM (in lower case) at the end of the command line.

3. That worked! Thank you so much! The exact error message had been "(.text+0x7b): Undefined reference to 'tan'."

For future reference, what does the -lm do? When is it necessary to use it?

4. Whenever you use any mathematical function in math.h.

5. Originally Posted by Raen
That worked! Thank you so much! The exact error message had been "(.text+0x7b): Undefined reference to 'tan'."

For future reference, what does the -lm do? When is it necessary to use it?
I'll elaborate on Elysia's comment...

Whenever you call functions that are not made by you (like printf, tan, whatever), the compiler doesn't really know what to do: what are those functions? Well, they've been made by other people, just lik you made some functions, and they're in different files so that your applications can call them.
For this, however, you need to "link" your application to the libraries that have those functions. For printf and many other functions this happens automatically, but for tan it does not (in C99, at least, I'm quite sure you don't need it in C++). These are in the library called "m" (for "math" I bet). By passing -lm to the compiler you say: link to library "m".

It's a simplified explanation, but that's more or less how it works .

6. Originally Posted by EVOEx
...For printf and many other functions this happens automatically, but for tan it does not (in C99, at least, I'm quite sure you don't need it in C++)...
This is compiler/IDE dependent, not language dependent. There is no need to link with some math library in either C or C++ with Visual Studio, for example.

7. Originally Posted by Elysia
This is compiler/IDE dependent, not language dependent.
At the risk of being pedantic - which I clearly hate to be - it is actually dependent on how the libraries are organised. It just so happens that some libraries are specific to a particular compiler/IDE.

There are also implementations of standard libraries that are distributed separately and may, optionally, be installed as replacements for the libraries distributed by default with some compilers. This means that, depending on what library is installed, different settings may be needed with a given compiler/IDE.

The reasoning can get a little circular because implementing a library that is compatible with multiple compilers or IDEs requires representative knowledge of all of the targeted environments. However, it is library organisation that determines need for "-lm" to link in a math library, not a property of the compiler or IDE.