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. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    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. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Whenever you use any mathematical function in math.h.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Raen View Post
    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. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Quote Originally Posted by EVOEx View Post
    ...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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,679
    Quote Originally Posted by Elysia View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM
  5. trig
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 01-17-2002, 07:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21