# Thread: are nested math function allowed in C?

1. ## are nested math function allowed in C?

hello

I'm trying to take the exponent of negative x squared...

e^-x^2 .... I was wondering if this was possible in C?

I have been playing with something like this:

x = -1 * x;

exp ( pow ( x , 2 ) ) ;

but i just get: 2.71828

for when i input a value of 1 for x.

it should be: .367879

thank you.

and thanks Rashakil Fol for helping me the %d, %f, I overlooked that.

2. Squaring -x is just the same as squaring x

exp ( - pow ( x , 2 ) ) ;

3. I'm not sure where you get your "should be" value, but work out the math in your head. Nested functions are called inside-out, so pow() is evaluated first and its return value is passed to exp().

Assuming x = 1:

pow(x, 2) = 1 squared = 1

That gets passed to exp() which is e to the xth power.

e to the first power is e....which is 2.71828. The program is giving you exactly the value it should.

4. ## whats the difference here?

Hey, thanks for the tip, it worked, but I don't understand why my original version doesnt work, here's a program, i input

x = 2

ans 1 = 54.598

ans 2 = 0.018316

ans 2 is correct but where is ans 1 coming from?

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

main()
{
float x, y, ans1, ans2 ;

printf("put in x");
scanf("%f",&x);

y = -1 * x ;

ans1 = exp (  pow (y , 2 ) )  ;

ans2 = exp ( -pow (x , 2) ) ;

printf("%f",ans1);
printf("\n\n %f",ans2);

getchar();
getchar();

}```

5. You really need to work this stuff out. y is -2 and -2 squared is 4. What exactly do you get when you calculate e to the 4th power? Hmm...right around 54.598.

ans1 is e to the 4th power, and ans2 is e to the -4th power. There's a big difference.