# Thread: Trying to create an exp(x) program

1. ## Trying to create an exp(x) program

Hey all. I'm experimenting with a program that is in relation to an assignment I have. All I'm trying to do is have the user input a number, and then have the program raise "e" (as in 2.71828...) to that number. I've got something, but it's not working like I need it to. I know my program doesnt include it yet, but I want to have a printf statement saying something like "exp(x) is <answer>". Can anyone see what I'm doing wrong?

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

int main()
{
double x;
double b;

printf("Enter an exponent for base e\n");
scanf("&#37;lf", &x);

b = double exp(x);
return(b);
}

2. Once you take out the erroneous double in "b = double exp(x);" everything looks like it should work.

3. I had tried that and I get a crazy error:

Code:
Undefined                       first referenced
symbol                             in file
exp                                 /var/tmp//ccsUdNwM.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

4. And of course, somewhere along the way you should remember that to include the math library using gcc you need to add "-lm" to the end of your compilation line.

5. Forgot about that. But now there's a new problem. The program just returns exactly what I input. I.e. after it asks for a number, I input 6 and it returns the number 6.

6. Ok I figured it out:

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

main()
{
double x;
double b;

printf("Enter an exponent for base e\n");
scanf("%lf", &x);

b = exp(x);
printf("exp(%f) is equivalent to %lf\n", x, b);
}
But one last thing. The final printf (example) reads something like
Code:
exp(2.000000) is equivalent to 7.389056
Is there a way to make it so that the program doesn't spit out so many decimal places? I.e. can it just say 2.00 instead of 2.0000000...?

7. printf uses &#37;f for doubles.

you can use the field width thing:

%1.2f

8. Thanks!

9. what do you mean by add -lm to to the end of the compilation line

10. -l = GCC link 'flag', m = math library. So you're linking against the math library, ie support for math.h functions.

11. so would it be like

gcc-lm <filename>

12. More like:
gcc -lm <filename>

Without the space you are invoking the (presumably non-existent) gcc-lm instead of gcc.

13. Whenever on a Unix compliant system and need to know how to do something with a program, the first thing to do is 'man xxx' where xxx is the program you want to use. It might be a little difficult at first as man pages are mostly technical, but the first lines in the man page contain the various syntax rules to invoke xxx.