a simple question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zl2k

    #1

    a simple question

    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

  • Greg Herlihy

    #2
    Re: a simple question

    On Apr 1, 10:24 pm, "zl2k" <kdsfin...@gmai l.comwrote:
    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


    Comment

    • Jim Langston

      #3
      Re: a simple question

      "zl2k" <kdsfinger@gmai l.comwrote in message
      news:1175491442 .063918.111450@ o5g2000hsb.goog legroups.com...
      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 );

      }


      Comment

      Working...