2

I have never used floats really that much before and the current project I am working on requires them. I'm getting weird issues that I learned about years ago but have forgotten why this happens.

My results after multiplying or adding floats aren't what they're supposed to be.

Here is my code:

void main ()
{
    //Example 1 - ERROR
    float a=16937.6;
    float b=112918;
    float total=b+a;
    cout<<total<<endl; //Outputs 129896 - rounds up and loses decimal (129855.6)

    //Example 2 - Error
    float c=247.82;
    float d=9995.2;
    float total2=c+d;
    cout<<total2<<endl; //Outputs 10243 - loses all decimals (10243.02)
    system ("pause");

}
2
  • 1
    en.wikipedia.org/wiki/Floating_point The section on accuracy problems is what you're looking for. Commented Feb 29, 2012 at 21:22
  • 1
    No, I think it is being outputted incorrectly in this case Commented Feb 29, 2012 at 21:33

2 Answers 2

7

Your problem isn't decimal precision - it's the format that is used to output the values.

Try:

cout << setiosflags(ios::fixed) << setprecision(2) << x;
Sign up to request clarification or add additional context in comments.

3 Comments

This works beautifully! I was attempting to use other forms of numerics but had the same results (doubles, etc). Couldn't figure it out. Thank you!
No, if at any point you are handling more than 6 digits, staying with float is not the correct answer, however you coerce the output function.
The values I am working with are never larger than 3 decimals and are pretty small. It's graphics related and they do not go past the dimensions of the window
4

What Every Programmer Should Know About Floating-Point Arithmetic, or Why don’t my numbers add up?

In short, real numbers are infinite, computers aren't.

5 Comments

Seriously, the same precision question comes up non-stop. The answer is always the same. It should go in FAQ.
I think this is about output formatting, not decimal precision.
@DStanley: I think it's connected. Output rounding exists so you wouldn't get things like 0.1 + 0.2 = 0.30000000000000004. For the first example, the result in 32-bit arithmetic comes out to about 129855.6015625, and the output function knows that only 6 digits will be certain, so it chooses to round the result. You can get more digits by setting output precision, but you have no idea if they're correct or not.
@Amadan - his question was "why does 129855.6 get displayed as 129856", not "why does 129855.6 get displayed as 129855.5999999345". That's what made me think it was formatting, not precision.
@DStanley: I know. Obviously what gets outputted is the responsibility of the formatter. But the reason the formatter is doing it sound. As long as you're using 32 bits, 6 digits is as much as you can be certain about. 7th is only probably certain; 8th is most likely not. Let me say it again here: the question is, do you just want more digits, or do you want the correct digits? If you want to be sure of the answer, you also have to bump up the data type to double, in addition to tweaking the formatter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.