CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Question does adding 1 in my code doesn't make any change??

    Code:
    #include<iostream>
    using namespace std;
    int main()
    {
        using namespace std;
        float a=2e+6;
        cout<<a<<endl;
        float b=a+1;
        cout<<b<<endl;
        cout<<b-a<<endl;
        return 0;
    }
    output is a shown in attachment..
    if b and a are same then why their subtraction gives 1??
    however i have assigned b=a+1.. if it is so then why both are shown same??
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: does adding 1 in my code doesn't make any change??

    Quote Originally Posted by vkash
    if it is so then why both are shown same?
    An approximation of the value in scientific notation was printed. You could try to avoid this by placing:
    Code:
    cout.precision(7);
    before you start printing.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: does adding 1 in my code doesn't make any change??

    Quote Originally Posted by vkash View Post
    if b and a are same then why their subtraction gives 1??
    There is a difference in what those variables are internally and how you are displaying them. Obviously b and a are not the same internally. If you use the debugger, you would see that the values are different.

    Also, what is that stuff at the bottom of the console display ("Process Returned xxx" and the time)? Are you using Visual C++?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 17th, 2012 at 10:27 PM.

  4. #4
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: does adding 1 in my code doesn't make any change??

    Quote Originally Posted by laserlight View Post
    An approximation of the value in scientific notation was printed. You could try to avoid this by placing:
    Code:
    cout.precision(7);
    before you start printing.
    thanks laser light for your cout.precision....

    There is a difference in what those variables are internally and how you are displaying them. Obviously b and a are not the same internally. If you use the debugger, you would see that the values are different.

    Also, what is that stuff at the bottom of the console display ("Process Returned xxx" and the time)? Are you using Visual C++?

    Regards,

    Paul McKenzie
    errrrrr....
    sorry it's not VC++.. recently my windows xp corrupted so i am on linux(no VC++ here) so it's codeblocks..
    after all thanks for your answer.thanks for telling me that internally both a and b are not same...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured