Thread: how do you use exp()

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

    how do you use exp()

    hello, can someone tell me how to use the exp() function
    I've tried running this:

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



    main()
    {
    double ans ;


    ans = exp ( (float) 1 ) ;

    printf("%d",ans);

    the answer it keeps giving me is: -1961601175

    it should be: 2.71828...

    what am I doing wrong?

    thank you

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You're using printf incorrectly. printf interprets the %d flag to mean "print as an integer." This causes printf to treat 'ans' as an integer.

    However, 'ans' is a double, a completely different format for storing numbers. Use %f instead of %d, so that printf interprets the contents of ans as a double.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point number comparison
    By stanlvw in forum C++ Programming
    Replies: 9
    Last Post: 04-27-2009, 01:44 PM
  2. Working with Parser Generators - Functions
    By jason_m in forum C Programming
    Replies: 1
    Last Post: 09-09-2008, 09:38 PM
  3. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM
  4. Need some help with exp()
    By Tarls in forum C++ Programming
    Replies: 3
    Last Post: 04-19-2002, 08:53 PM
  5. Replies: 2
    Last Post: 01-05-2002, 09:11 AM