Jump to content
  • Advertisement
Sign in to follow this  
doNY

A c problem.precision on printf()

This topic is 2828 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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!