are nested math function allowed in C?

This is a discussion on are nested math function allowed in C? within the C Programming forums, part of the General Programming Boards category; hello I'm trying to take the exponent of negative x squared... e^-x^2 .... I was wondering if this was possible ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Squaring -x is just the same as squaring x

    How about
    exp ( - pow ( x , 2 ) ) ;
    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
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    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.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    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. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    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.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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