|
-
May 29th, 2013, 05:15 PM
#1
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?
-
May 29th, 2013, 05:25 PM
#2
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)
-
May 29th, 2013, 09:26 PM
#3
Re: Simple multiplication and division not evaluating correctly
Yeah, it might be better to test with:
Code:
std::cout << maxX << std::endl;
-
May 29th, 2013, 10:24 PM
#4
Re: Simple multiplication and division not evaluating correctly
 Originally Posted by fiodis
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.
-
May 31st, 2013, 07:19 AM
#5
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?
-
May 31st, 2013, 07:31 AM
#6
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)
-
May 31st, 2013, 03:44 PM
#7
Re: Simple multiplication and division not evaluating correctly
 Originally Posted by fiodis
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|