# pow() doesnt return a double?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-29-2003
FloatingPoint
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.
• 07-29-2003
laasunde
• 07-29-2003
major_small
Re: pow() doesnt return a double?
Quote:

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...
• 07-29-2003
Lynux-Penguin
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
• 07-30-2003
CornedBee
Or rather this:
Quote:

#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;
}
• 07-30-2003
FloatingPoint
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.
• 07-30-2003
FloatingPoint
Re: Re: pow() doesnt return a double?
Quote:

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.
• 07-30-2003
FloatingPoint
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.
• 07-30-2003
CornedBee
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.
• 07-30-2003
FloatingPoint
Included that and I'm using Dev C++ 4 but it doesnt stay.
• 07-30-2003
CornedBee
Of course it doesn't. No header file makes the app stay by itself.

system("pause");
(or std::system("pause"))
directly before the return.
• 07-30-2003
major_small
header files don't but some compilers will...
• 07-30-2003
golfinguy4
use cin.get()
• 07-30-2003
alpha
• 07-31-2003
VirtualAce
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last