Jump to content
  • Advertisement
Sign in to follow this  
sheep19

[C++]printf problem when printing float

This topic is 2776 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

I made this C++ program which prints series of numbers:


#include <iostream>

using namespace std;

int main()
{
for(float f = 2.05f; f <= 2.95f; f += 0.1f)
printf("%f\n", f);

cout << "\n";

for(float f = 2.05f; f <= 2.95f; f += 0.1f)
cout << f << "\n";

cin.get();
}




However the output is:
printf
2.050000
2.150000
2.250000
2.350000
2.450000
2.549999
2.649999
2.749999
2.849999
2.949999

cout
2.05
2.15
2.25
2.35
2.45
2.55
2.65
2.75
2.85
2.95

Is there something wrong with printf?

Share this post


Link to post
Share on other sites
Advertisement
No, your implementation of printf() just defaults to a larger number of decimal places when printing floating point values. The actual problem is that 0.01 can't be exactly represented as a binary floating point value.

Share this post


Link to post
Share on other sites
No, printf is working fine. floats are unable to represent the number 0.1 perfectly in binary so it rounds. Don't be so shocked though. The number 1/3 can't be represented in base 2 or 10, but it can be represented perfectly in base 3. It so happens that some numbers, like 0.1, can be represented in base 10 but not base 2.

Read this paper for a bunch of info on floating-point.

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
Why does cout display it correctly though?


If you look carefully, you will see that cout is just rounding up to 2 d.p. So 2.949999 becomes 2.95. If you set the precision of cout, it will do the same thing as printf. To set cout's precision, do this: cout.precision(num);

Share this post


Link to post
Share on other sites
Quote:
Original post by sheep19
Why does cout display it correctly though?


Why do you think the way cout displays it is "correct"? What does "correct" even mean here?

The value that is stored isn't 0.01 (or whatever else), because that value can't be exactly represented. So neither cout nor printf can consistently, "correctly" display the number you have in mind, in the way that I understand the word "correct", because neither one is being given the actual number you have in mind, and in fact neither one can be given the number you have in mind.

We have to approximate.

Why would you expect any output method to read your mind and output the number of significant figures you intended it to, unless (a) you explicitly told it to or (b) by coincidence?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!