floats for better precision?

Discussion in 'Mac Programming' started by iphoneGuy, Mar 30, 2008.

  1. macrumors member

    Joined:
    Oct 26, 2007
    Location:
    [[NSLocation alloc] initWithCity: Chicago]
    #1
    I am working on a new program that uses a lot of float values. However, I notice that if I set the value it will not be precise..

    ie.

    float val = 10.3

    may show up in the debugger as 10.300002 or 10.299999993

    How can I make these numbers alway return 10.3 exactly?

    Thanks,
     
  2. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    Don't use float if you can't tolerate rounding. 10.3 can't be represented in floating point without rounding.
     
  3. macrumors regular

    Joined:
    Jan 17, 2007
    #3
    Why's that?
     
  4. macrumors 6502

    Joined:
    Jun 1, 2006
    #4
    There are some numbers that just can't be represented at all in binary on a computer, regardless of precision used.

    http://www.ima.umn.edu/~arnold/disasters/patriot.html
     
  5. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #5
    What was I thinking? 3/16 = 0.1875. No big deal for a float in this case, eh?!

    It's a problem further down in the precision department. Here's C++ example.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std ; 
    
    int main(void)
    {
    	float f ; 
    	f = 10.000003 ; 
    	cout.precision(12) ; 
    	cout << "f=" << f ; 
    	return 0 ;
    }
    
    output:
    Code:
    [Session started at 2008-03-30 18:00:48 -0500.]
    f=10.000002861
    exercise35 has exited with status 0.
    
     
  6. macrumors regular

    Joined:
    Jan 17, 2007
    #6
    Sorry iphoneGuy, I've kind of hijacked your thread...

    So is there something else to use like double, or do programmers just get used to it?
     
  7. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #7
    Well, there are different ways to get around floating point rounding behavior.

    Using this example, if you knew you needed 6 digits of non-rounded precision, you could multiply all your floats by 1,000,000, do all your calculations, and then divide by 1,000,000 for the final output.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std ; 
    
    
    int main(void)
    {
    	float f ; 
    	f = 10000003.0 ; 
    	cout.precision(12) ; 
    	cout << "f=" << f/1000000.0 ; 
    	return 0 ;
    }
    Output:
    Code:
    [Session started at 2008-03-30 18:43:23 -0500.]
    f=10.000003
    exercise35 has exited with status 0.
    
    Following that logic, you could even use integers.

    Working 3D point data, it's very common to have slight inaccuracies. In those cases, a lot of programs takes EPSILON into account. http://en.wikipedia.org/wiki/Machine_epsilon

    Todd
     
  8. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    Also, doubles have greater precision, but the rounding dilemma still exists.

    The same program as above, using a double instead of float, does not round in this case.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std ; 
    
    
    int main(void)
    {
    	double d ; 
    	d = 10.000003 ; 
    	cout.precision(12) ; 
    	cout << "d=" << d ; 
    	return 0 ;
    }
    Output:
    Code:
    [Session started at 2008-03-30 18:48:07 -0500.]
    d=10.000003
    exercise35 has exited with status 0.
    
     
  9. thread starter macrumors member

    Joined:
    Oct 26, 2007
    Location:
    [[NSLocation alloc] initWithCity: Chicago]
    #9
    No problem.. I just wish there was a clean wat to represent 10.3 as exactly 10.3....
     
  10. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    You will need to invent a computer that uses base-10 instead of base-2 for numerical representations.

    There are discussions about this, but base-2 computers are so much easier to architect, that this will probably never come to be.
    http://www.madsci.org/posts/archives/2001-10/1002423001.Cs.r.html
    http://homepages.transy.edu/~jmiller/web706/chapt3.htm

    You may also wish to take this up with the IEEE, as IEEE-754 defines this standard for storing floating point numbers in base-2.

    As was mentioned earlier, if you are doing fixed precision arithmatic, using an int, long int, etc. and a fixed bias for your numbers might be a good way to go. We can exactly represent base-10 integers in base-2, so it's no problem (unless you need REALLY big integers...).

    -Lee
     
  11. macrumors newbie

    Joined:
    Feb 6, 2008
    #11
    What you want is a "decimal" data type, if available . . . .NET languages have decimal, Java has BigDecimal I believe, I think Python has a decimal as well, etc. Just as toddburch and lee1210 suggested, the idea is that you multiply your floating point value by whatever scaling factor is necessary to represent it as an integer value, do your work on that integer value, and divide by the scale to get your resulting floating point value. Those decimal types implement that behavior so you don't have to manually do it.
     
  12. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #12
    Hi,

    I know it's annoying but to be honest, it's not a problem that most applications have to worry about it too much. Just be aware of doing things like this:-
    Code:
    float value ;
    …
    …
    if ( value == 10.3f )
    {
    …
    …
    }
    
    which would produce different results depending on how 'value' was calculated.

    b e n

    EDIT: Here's an example of what I mean by 'not worrying'. Calculators suffer from exactly the same problem but most people use them in blissful ignorance and trust their results.
     
  13. thread starter macrumors member

    Joined:
    Oct 26, 2007
    Location:
    [[NSLocation alloc] initWithCity: Chicago]
    #13
    thanks for the responses... I ended up switching to ints it took a couple of minutes and all is good...
     
  14. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #14
    Google for "what every programmer should know about floating point arithmetic" and you should find a few links to the Kahan article, which on one hand contains much more than you want to know, on the other hand just barely covers what you need to know.
     

Share This Page