Why can't I output a basic double in c++ [duplicate]
开发者_如何转开发Possible Duplicate:
Incorrect floating point math?
Using C++ console app in visual studio.
float percentCorrect;
percentCorrect = ( 7 / 5 );
printf("%f", percentCorrect);
output is 1.0000
OK so how do I get the correct decimal output??
7 and 5 are integers, so the compiler treats them as such and does integer division.
float percentCorrect;
percentCorrect = (7.0/5.0);
printf("%f", percentCorrect);
The above should print out the expected value.
You need to include a decimal: 7.0/5.0
7
and 5
are integer literals, so 7 / 5
performs integer division.
You need to have at least one floating literal for floating-point division to be performed:
float percentCorrect = 7.0 / 5.0;
精彩评论