Troubleshooting C Programs | Error in math.h library

  • Thread starter DrKareem
  • Start date
In summary, Kareem was trying to compile a C program and got an error that said "cos() undefined reference to cos". Kareem can't figure out where the error is coming from, but it seems to be in the math.h library. Kareem knows that there are different include files for Linux, so he tries including that library as well. However, the problem persists. Kareem then tries running the program multiple times with different tolerances and maximum iterations, but no matter what he does, the program always ends with an error.
  • #1
DrKareem
101
1
Hello guys. I'm having some problems when compiling some C programs. I can't troubleshoot them since I'm not very proficient with C. But it seems that this error comes from the math.h library, maybe the include is different for Linux.

This is the error I'm getting:
Kareem@Kareem:~/prog/secant$ cc main.c
/tmp/cciW3fYU.o(.text+0x219): In function `F':
: undefined reference to `cos'
collect2: ld returned 1 exit status

this is the code:

Code:
/*
*   SECANT ALGORITHM 2.4
*
*   To find a solution to the equation f(x) = 0
*   given initial approximations p0 and p1:
*
*   INPUT:   initial approximation p0, p1; tolerance TOL;
*            maximum number of iterations N0.
*
*   OUTPUT:  approximate solution p or
*            a message that the algorithm fails.
*
*/

#include<stdio.h>
#include<math.h>
#define true 1
#define false 0

main()
{
   double P0,F0,P1,F1,P,FP,TOL;
   int I,NO,OK,FLAG;
   FILE *OUP[1];

   double absval(double);
   double F(double);
   void INPUT(int *, double *, double *, double *, int *);
   void OUTPUT(FILE **, int *);

   INPUT(&OK, &P0, &P1, &TOL, &NO);
   if (OK) {
      OUTPUT(OUP, &FLAG);
      /* STEP 1 */ 
      I = 2;      
      F0 = F(P0);
      F1 = F(P1);
      OK = true;                                
      /* STEP 2 */ 
      while ((I<=NO) && OK) {   
	 /* STEP 3 */ 
	 /* compute P(I) */ 
	 P = P1 - F1 * (P1 - P0) / (F1 - F0);

	 /* STEP 4 */  
	 FP = F(P);  
	 if (FLAG == 2) fprintf(*OUP,"%3d   %15.8e   %15.8e \n",I,P,FP); 
	 if (absval(P - P1) < TOL) {
	    /* procedure completed successfully */
	    fprintf(*OUP,"\nApproximate solution P = %12.8f\n",P);
	    fprintf(*OUP,"with F(P) = %12.8f\n",FP);
	    fprintf(*OUP,"Number of iterations = %3d",I);
	    fprintf(*OUP,"    Tolerance = %14.8e\n",TOL);
	    OK = false;
	 }
	 else {
	    /* STEP 5 */ 
	    I++; 
	    
	    /* STEP 6 */ 
	    /* update P0, F0, P1, F1 */ 
	    P0 = P1;
	    F0 = F1;
	    P1 = P;
	    F1 = FP;
	 }
      }
      if (OK) {
	 /* STEP 7 */ 
	 /* procedure completed unsuccessfully */       
	 fprintf(*OUP,"\nIteration number %3d",NO);
	 fprintf(*OUP," gave approximation %12.8f\n",P);
	 fprintf(*OUP,"F(P) = %12.8f not within tolerance : %15.8e\n",FP,TOL);
      }
      fclose(*OUP);
   }
   return 0;
}

/* Change function F for a new problem */
double F(double X)
{
   double f; 

   f = cos(X) - X;
   return f;
}

void INPUT(int *OK, double *P0, double *P1, double *TOL, int *NO)
{
   char AA;

   printf("This is the Secant Method.\n");
   printf("Has the function F been created in the program immediately preceding\n");
   printf("the INPUT function?\n");
   printf("Enter Y or N\n");
   scanf("%c",&AA);
   if ((AA == 'Y') || (AA == 'y')) {
      *OK = false;
      while (!(*OK)) {
	 printf("Input initial approximations P0 and P1 separated by blank\n");
	 scanf("%lf %lf", P0, P1);
	 if (*P0 == *P1) printf("P0 cannot equal P1\n");
	 else *OK = true;
      } 
      *OK = false;
      while(!(*OK)) {
	 printf("Input tolerance\n");
	 scanf("%lf", TOL);
	 if (*TOL <= 0.0) printf("Tolerance must be positive\n");
	 else *OK = true;
      }
      *OK = false;
      while (!(*OK)) {
	 printf("Input maximum number of iterations - no decimal point\n");
	 scanf("%d", NO);
	 if (*NO <= 0) printf("Must be positive integer\n");
	 else *OK = true;
      }
   }
   else {
      printf("The program will end so that the function F can be created\n");
      *OK = false;
   }
}

void OUTPUT(FILE **OUP, int *FLAG)
{
   char NAME[30];

   printf("Select output destination\n");
   printf("1. Screen\n");
   printf("2. Text file\n");
   printf("Enter 1 or 2\n");
   scanf("%d", FLAG);
   if (*FLAG == 2) {
      printf("Input the file name in the form - drive:name.ext\n");
      printf("For example:   A:OUTPUT.DTA\n");
      scanf("%s", NAME);
      *OUP = fopen(NAME, "w");
   }
   else *OUP = stdout;
   printf("Select amount of output\n");
   printf("1. Answer only\n");
   printf("2. All intermeditate approximations\n");
   printf("Enter 1 or 2\n");
   scanf("%d", FLAG);
   fprintf(*OUP,"Secant Method\n");
   if (*FLAG == 2) fprintf(*OUP, "  I                 P              F(P)\n");
}   
     

/* Absolute Value Function */
double absval(double val)
{
   if (val >= 0) return val;
   else return -val;
}
 
Technology news on Phys.org
  • #2
So is cos() in your copy of math.h? It's in my VC++ Include folder's math.h. Is the compiler complaining about anything in stdio.h? I usually see a space after the #include statement for those two .h files, and don't know if not having a space will make the compiler not like those include definitions.
 
  • #3
I've tried with different algorithms, and they all complain about some math functions like cos(),sin() and tan().

absval() might also be in the math.h library, but the compiler isn't complaining about it. It's not complaining about stdio.h. I tried including spaces betweeh # and include, still didn't work.
 
  • #4
You might need to use "-lm" or something similar when you compile.
 
  • #5
Oh yeah right it worked...used cc -lm filename.c -o filename.

thanks NateTG :)
 
  • #6
btw why is it necessary to use the -lm option?
 
  • #8
Thanks for the link, very useful. Do you guys recommend any editor other than VI? One that indents and stuff like that.
 
  • #9
vim does indenting, and colors too. I don't remember how to activate the feature, though. (these days, vi is most likely simply an alias for vim)

xemacs (and emacs) seem to be popular editors too.

(I use the above two most often. I tend to use vim for quick edits, and xemacs for more extended editing sessions)


If you're willing to go download stuff, eclipse is a very nice IDE. The trouble is that it's designed for java -- it has a plugin for C/C++, but I don't know how good it is.

There are interesting editors that maybe others can say something about.
 
  • #10
I use Eclipse's CDT plugin (for C/C++) and its pretty nice. I use Eclipse for a lot of Java development though so I was already familiar with it before I started using CDT.
 
  • #11
Thanks for the recommendations. I'm using gedit right now, it's pretty nice and simple.
 
  • #12
I use nedit.

- Warren
 

What is <math.h>?

<math.h> is a header file in the C programming language that contains functions and constants for mathematical operations. It is used to perform calculations, manipulate numbers, and work with mathematical formulas.

What are the most commonly used functions in <math.h>?

Some of the most commonly used functions in <math.h> include sqrt() for finding the square root of a number, pow() for calculating a number raised to a power, sin() and cos() for calculating trigonometric functions, and abs() for finding the absolute value of a number.

How do I use <math.h> in my code?

To use <math.h> in your code, you first need to include the header file by using the preprocessor directive #include <math.h>. This will make all the functions and constants in <math.h> available for use in your code.

Can I use <math.h> in other programming languages?

<math.h> is specific to the C programming language and may not be compatible with other programming languages. However, many other languages have their own libraries or modules that provide similar mathematical functions and operations.

Are there any limitations to using <math.h>?

One limitation of <math.h> is that it only supports basic mathematical operations and functions. It does not have advanced features such as handling complex numbers or solving equations. Additionally, some functions may have limited precision, so it is important to be aware of potential rounding errors when using <math.h>.

Similar threads

  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
22
Views
5K
Replies
2
Views
2K
  • Programming and Computer Science
Replies
21
Views
8K
Back
Top