CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2012
    Location
    .NET 4.0 / VS 2010
    Posts
    20

    Simple multiplication and division not evaluating correctly

    At one point in my C++, non-CLR program, the following code:
    Code:
    unsigned int size = 3;
    float maxX = (float)(int(size-1))/2.0f;
    std::cout << maxX;
    outputs 107. I have no idea why; is it something about a conversion from unsigned int to float?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,925

    Re: Simple multiplication and division not evaluating correctly

    Produces 1 on my system using MSVS.

    Have you got any more cout statements following the one shown?
    Last edited by 2kaud; May 29th, 2013 at 05:31 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

    Re: Simple multiplication and division not evaluating correctly

    Yeah, it might be better to test with:
    Code:
    std::cout << maxX << std::endl;
    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

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

    Re: Simple multiplication and division not evaluating correctly

    Quote Originally Posted by fiodis View Post
    At one point in my C++, non-CLR program, the following code:
    Code:
    unsigned int size = 3;
    float maxX = (float)(int(size-1))/2.0f;
    std::cout << maxX;
    outputs 107. I have no idea why; is it something about a conversion from unsigned int to float?
    No, it more than likely has something to do with not posting a complete program, or telling us something you believe is happening but isn't.
    Code:
    #include <iostream>
    
    int main()
    {
       unsigned int size = 3;
       float maxX = (float)(int(size-1))/2.0f;
       std::cout << maxX;
    }
    This outputs 1.

    Instead of giving us a snippet, post a complete program, such as the one above.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 29th, 2013 at 10:27 PM.

  5. #5
    Join Date
    Jun 2012
    Location
    .NET 4.0 / VS 2010
    Posts
    20

    Re: Simple multiplication and division not evaluating correctly

    The complete program's several thousand lines split up among different source files. I'm never sure what to do when I need help with those; I know I should post as complete a program as possible, but snippets seem to be the only thing that's practical.

    I did find the problem; as 2kaud suggested I had far too many other cout statements following this one and the output was getting confusing. I got rid of everything but the cout I was interested in and saw that it was indeed printing 1.

    For future reference, what's the general guideline on posting complete programs vs. snippets when the program is too long to fit nicely in a single post?

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,925

    Re: Simple multiplication and division not evaluating correctly

    For future reference, what's the general guideline on posting complete programs vs. snippets when the program is too long to fit nicely in a single post?
    If the program is too long to post, then the suggestion is that a new complete program is produced that exhibits the symptoms and this program is posted. If this is not possible, then the program files can be zipped and posted as a post attachment. Although if the program is really long, I don't know how many of us would have the time to examine code of that length/complexity to the extent required to provide guidance.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

    Re: Simple multiplication and division not evaluating correctly

    Quote Originally Posted by fiodis View Post
    The complete program's several thousand lines split up among different source files. I'm never sure what to do when I need help with those; I know I should post as complete a program as possible, but snippets seem to be the only thing that's practical.
    Snippets are useless if the same snippet, when placed in a small program, doesn't produce any errors.

    If the program is several thousand lines, but what you're posting is easily condensed to a 3 or 4 line program, the first thing you should have done is write and test the small program first. If that small program runs OK, then you should debug your larger program.

    Regards,

    Paul McKenzie

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