a simple question

Z

zl2k

hi,
In a function of a class, I have a unsigned long A (65535) and a
double B (0-1) and when I calculate a*b I should get a double 0-65535.
But, I get an unreasonable large value (e+9) if doing "cout<<A*B;".
However, I get the correct value if doing "cout<<B*A;" or
"cout<<(double)A*B". Can someone explain what's wrong with the code?
If I take the piece out of the function and run it separately, both
A*B and B*A give the same correct result. Thanks for the comments.
zl2k
 
G

Greg Herlihy

hi,
In a function of a class, I have a unsigned long A (65535) and a
double B (0-1) and when I calculate a*b I should get a double 0-65535.
But, I get an unreasonable large value (e+9) if doing "cout<<A*B;".
However, I get the correct value if doing "cout<<B*A;" or
"cout<<(double)A*B". Can someone explain what's wrong with the code?

What code?

Greg
 
J

Jim Langston

zl2k said:
hi,
In a function of a class, I have a unsigned long A (65535) and a
double B (0-1) and when I calculate a*b I should get a double 0-65535.
But, I get an unreasonable large value (e+9) if doing "cout<<A*B;".
However, I get the correct value if doing "cout<<B*A;" or
"cout<<(double)A*B". Can someone explain what's wrong with the code?
If I take the piece out of the function and run it separately, both
A*B and B*A give the same correct result. Thanks for the comments.
zl2k

Following program outputs:
32767.5
32767.5
32767.5

which is not what you describe.

Please show the code you are doing.

#include <iostream>
#include <string>

int main()
{
unsigned long A = 65535;
double B = 0.5;

std::cout << A * B << "\n";
std::cout << B * A << "\n";
std::cout << double(A) * B << "\n";

std::string wait;
std::getline( std::cin, wait );

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top