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");

}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
CREW
  • 926
  • 3
  • 17
  • 32

2 Answers2

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;
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • 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! – CREW Feb 29 '12 at 21:32
  • 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. – Amadan Feb 29 '12 at 21:41
  • 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 – CREW Feb 29 '12 at 21:50
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.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 4
    Seriously, the same precision question comes up non-stop. The answer is always the same. It should go in FAQ. – Anycorn Feb 29 '12 at 21:23
  • 3
    I think this is about output formatting, not decimal precision. – D Stanley Feb 29 '12 at 21:26
  • @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 Feb 29 '12 at 21:36
  • 1
    @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. – D Stanley Feb 29 '12 at 21:41
  • @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. – Amadan Feb 29 '12 at 21:45