At one point in my C++, non-CLR program, the following code:
outputs 107. I have no idea why; is it something about a conversion from unsigned int to float?Code:unsigned int size = 3;
float maxX = (float)(int(size-1))/2.0f;
std::cout << maxX;
Printable View
At one point in my C++, non-CLR program, the following code:
outputs 107. I have no idea why; is it something about a conversion from unsigned int to float?Code:unsigned int size = 3;
float maxX = (float)(int(size-1))/2.0f;
std::cout << maxX;
Produces 1 on my system using MSVS.
Have you got any more cout statements following the one shown?
Yeah, it might be better to test with:
Code:std::cout << maxX << std::endl;
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.
This outputs 1.Code:#include <iostream>
int main()
{
unsigned int size = 3;
float maxX = (float)(int(size-1))/2.0f;
std::cout << maxX;
}
Instead of giving us a snippet, post a complete program, such as the one above.
Regards,
Paul McKenzie
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?
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.Quote:
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?
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