• Advertisement
Sign in to follow this  

A c problem.precision on printf()

This topic is 2646 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi,guys!I met a problem
you know,printf("%.1f",123.45); output 123.5
but how to control the result 123.50?
printf("%.1f0",123.45); is just a idea,any other ways?thx!

Share this post


Link to post
Share on other sites
Advertisement
Why would you want to output deliberately imprecise values?

Share this post


Link to post
Share on other sites
i think the best solution is the one you posted... anyway, this is a strange problem, can you give us any more detail that would explain why you would want to do that?

Share this post


Link to post
Share on other sites
yeah,I had ask on irc but hadn't found any idea yet,it's a lovely problem post by my lovely professor.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Why would you want to output deliberately imprecise values?


Perhaps because the result is from a calculation using measured values with limited precision. For example, 11.5 * 10.9 = 125.35 => 125.40. On the other hand, the zero suggests an additional significant figure which carries forward the imprecision. My understanding of this is a bit rusty. I'll have to investigate further.

Share this post


Link to post
Share on other sites
Hope this helps.

int _tmain(int argc, _TCHAR* argv[])
{
float number;

printf("Input a float: ");
cin >> number;

printf("Round to 1/20: %.2f", floor(number * 20 + 1) / 20);
}


P.S: First post on Gamedev :P.

Share this post


Link to post
Share on other sites
Quote:
Original post by LessBread
On the other hand, the zero suggests an additional significant figure which carries forward the imprecision. My understanding of this is a bit rusty. I'll have to investigate further.

Common use of significant figures implies that additional zeros after a decimal point are significant. Rounding at the tenths position and placing a zero at the hundredths position would be deliberately implying a different degree of precision than being displayed.

Share this post


Link to post
Share on other sites
Yes. I went and checked a book. The appended zero would become a significant figure.

Are there any other explanations for why someone would want to do that (outside of dishonest reasons)? Is rounding to whole fractions the only one?

Share this post


Link to post
Share on other sites
Well it's a rounding issue, 5 or higher goes to the next value (123.5 in your example) so if you had put %.1f for 123.44 it would have gone to 123.4 for the output, nothing special. Usually this is only used when precision isn't too important. The number is how many decimal places are shown, as in %.5f will show 5 decimal places, the last digit rounded up or down as above.

An perfect example of when it's used is for currency. Internal it's full precision, for output you would put %.2f for 2 decimal places.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement