im intending to do a function that'll calculate the cosine of x like this:
1-x^2/2! + x^4/4! - x^6/6! ...
and compiler says that it doesn't support "%lf" format and "undefined" to pow and cos ? why

#include <stdio.h>
#include <math.h>

double my_cos(double c){

  int i,k;
  double result=1.0;
  for(i=1; i<10; i++){
    k= (i%2==0)?1:-1;
    result+= (k*pow(i,c))/factorial(i);
  }
    return result;
}

int main (){

  double c;
  printf("Enter the radian degree:\n");
  scanf("%lf",&c);
  printf(" my_cos : %lf\n cos: %lf\n", my_cos(c), cos(c));
    return 0;
}

the compiler's Errors:
gcc -c -Wall -ansi -pedantic -lm my_cos.c -o my_cos.o
my_cos.c: In function ‘main’:
my_cos.c:28:3: warning: ISO C90 does not support the ‘%lf’ gnu_printf format [-Wformat]
my_cos.c:28:3: warning: ISO C90 does not support the ‘%lf’ gnu_printf format [-Wformat]
gcc -g -Wall -ansi -pedantic -lm my_cos.o -o my_cos
my_cos.o: In function my_cos': my_cos.c:(.text+0x8a): undefined reference topow'
my_cos.o: In function main': my_cos.c:(.text+0xef): undefined reference tocos'
collect2: ld returned 1 exit status
make: *** [my_cos] Error 1

Recommended Answers

All 3 Replies

You have a couple of problems.

You have to write your own factorial function. However, pow, and cos are in math.h so unless one of your compiler switches is doing something weird they should be defined. I don't use gcc so I can't help there.

Your series expansion is incorrect. Your powers have to be 2, 4, 6... instead of 1, 2, 3...

Your parameters for pow are reversed. Try

double my_cos(double c) {

    int sign = -1;
    double result = 1.0;

    for (int i=2; i<=10; i+=2) {
        result += sign * pow(c,i) / factorial(i);
        sign   = -sign;
    }

    return result;
}

If you need i to be the loop counter you could do

double my_cos(double c) {

    int sign = -1;
    double result = 1.0;

    for (int i=1; i<=10; i++) {
        result += sign * pow(c,2*i) / factorial(2*i);
        sign   = -sign;
    }

    return result;
}

@Reverend jim hey thanks for your response and yea noticed the power's thing but about the pow i don't think that the parameters are reversed that's how it is, according to : https://www.tutorialspoint.com/c_standard_library/c_function_pow.htm
and i do have the factorial function already just forgot to add it in the code paradon me.
i'm using ubunto 12.04 operating system if that has somethign to do with the compiler im not sure...
about your code i tried it and got just the same thing "undefined reference to pow and main".

#include <stdio.h>
#include <math.h>

float factorial(int n){
    if (n==0)
        return 1;
    else
        return n*factorial(n-1);
}

double my_cos(double c) {
    int sign = -1;
    double result = 1.0;
    int i;
    for (i=2; i<=10; i+=2) {
        result += sign * pow(c,i) / factorial(i);
        sign   = -sign;
    }
    return result;
}

int main (){

  double c;
  printf("Enter the radian degree:\n");
  scanf("%lf",&c);
  printf(" my_cos : %lf\n cos: %lf\n", my_cos(c), cos(c));
    return 0;
}

compiler:
gcc -g -Wall -ansi -pedantic -lm my_cos.o -o my_cos
my_cos.o: In function my_cos': my_cos.c:(.text+0x78): undefined reference topow'
my_cos.o: In function main': my_cos.c:(.text+0xe0): undefined reference tocos'
collect2: ld returned 1 exit status
make: *** [my_cos] Error 1

oh and about this :
my_cos.c: In function ‘main’:
my_cos.c:28:3: warning: ISO C90 does not support the ‘%lf’ gnu_printf format [-Wformat]
my_cos.c:28:3: warning: ISO C90 does not support the ‘%lf’ gnu_printf format [-Wformat]

i just noticed that when i compile it again (2 times ) it just gone and still with the rest :
gcc -g -Wall -ansi -pedantic -lm my_cos.o -o my_cos
my_cos.o: In function my_cos': my_cos.c:(.text+0x78): undefined reference topow'
my_cos.o: In function main': my_cos.c:(.text+0xe0): undefined reference tocos'
collect2: ld returned 1 exit status
make: *** [my_cos] Error 1

EDIT
oh wow it's fixed now i just had to put the "-lm" flag at last in the makefile, and about the "ISO C90...", it seems that C90 is the name of the compiler and it reads the "%lf" as a valid parameter only in scanf , so if it's used in printf it'll trigger this warning.
in short that's nothing and can be ignorable i assume.
and that's it lol.. thanks !

Two final comments:

Factorial should probably return a long unsigned int rather than a float. And your test

if (n==0)

will cause your function to fail if passed a negative number. You might want to change it to

if (n <= 1)
    return 1;
else
    return n*factorial(n-1);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.