Thread: pow() doesnt return a double?

  1. #1
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191

    pow() doesnt return a double?

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    {
          
          double result, x = 3.0, y = 2.0;
          result = pow(x, y);
    
          cout << result << endl;
    
          system("PAUSE");
          return 0;
    }

    The above gives me a result of 9 instead of 9.0
    as the book here says.

    Same thing w/ abs() or fabs() or floor() etc. The arguments are doubles but the value returned are integers.

    I did something wrong or do I need to use the "cout.setf....cout.precision" thingy, but this book doesnt say anything abt it.

    Thanx.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    291

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    Re: pow() doesnt return a double?

    Originally posted by FloatingPoint
    The above gives me a result of 9 instead of 9.0
    as the book here says.
    the computer will truncate any extra zeroes... set precision to 1 if you want them to show up... it truncates them at a sig digit because you don't want 9.00000000000000 to show up...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <math.h>
    int main()
    {
    	double result, x=3.0,y=2.0;
    	result = pow(x,y);
    	cout.setf(ios::fixed);
    	cout.precision(5);
    	cout<<result<<endl;
    	return 0;
    }
    Try this...

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or rather this:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    using namespace std;
    int main()
    {
    double result, x=3.0,y=2.0;
    result = pow(x,y);
    cout.setf(ios::fixed);
    cout.precision(5);
    cout<<result<<endl;
    return 0;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191
    Thanx all.

    I tried

    double result = 9.0;
    cout << result << endl;


    and it gave me a 9.

    Is this is how it's supposed to be? I mean the only way is to use that "cout.setf.....precision" block?

    Thanx.

  7. #7
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191

    Re: Re: pow() doesnt return a double?

    Originally posted by major_small
    the computer will truncate any extra zeroes... set precision to 1 if you want them to show up... it truncates them at a sig digit because you don't want 9.00000000000000 to show up...
    Yea, I guess so.

    Thanx again.
    Come cast your shadow over me
    and I'll cast mine all over thee
    Take me away, into the shades
    where there is no light of day

  8. #8
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191
    Corned Bee;

    Yours flashes off the screen too fast. How do I put a "press any key to continue" there?

    #include <stdlib.h> or <stdlib> doesnt seem to work.

    Thanx.
    Come cast your shadow over me
    and I'll cast mine all over thee
    Take me away, into the shades
    where there is no light of day

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Would be <cstdlib>, but...

    a) When launching from within VC++ it gets stopped anyway.
    b) The usual console app is launched from the command line where you have a console window that stays.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User FloatingPoint's Avatar
    Join Date
    Jun 2003
    Posts
    191
    Included that and I'm using Dev C++ 4 but it doesnt stay.
    Come cast your shadow over me
    and I'll cast mine all over thee
    Take me away, into the shades
    where there is no light of day

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Of course it doesn't. No header file makes the app stay by itself.

    Add
    system("pause");
    (or std::system("pause"))
    directly before the return.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    header files don't but some compilers will...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    use cin.get()

  14. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    pow() most certainly does return a double. It leaves the result of the operation in st(0). If your not getting a double then your value is being converted to an integer via a FISTP or similar FPU opcode.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  3. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM