Thread: Floating Point Accuracy...

  1. #1
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62

    Floating Point Accuracy...

    I just wonder:

    When I run this program, why is it that the result is a little incorrect ?
    Like 0.2 * 1 == 0.20000000000000001
    and
    0.2 * 2 == 0.40000000000000002
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Why not tell the compiler to show the decimal two spaces from the decimal point using set precision?

    Code:
    #include <iostream>
    using std::fixed;
    using std::cout;
    
    #include <iomanip>
    using std::setprecision;
    
    int main ( void )
    {
       double val = 5.5;
       double res = val + val;
    
       cout << Result:  " << fixed << setprecision( 2 )  << res;
    
       return 0;
    }

  3. #3
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62
    Hi swgh

    Sure I could !

    But that didn't answer the question... ;-)


    regards,


    The SharK
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  4. #4
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Floating point numbers in computers are never going to be infinitely precise. To have that level of precision would require unlimited memory. If my memory is right, single-precision numbers are accurate to seven decimal places while double-precision are accurate to fifteen.

  5. #5
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62
    I see, thanks Frobozz ;-)
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  6. #6
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Single precision is 24 bits of precision (including leading 1) while double is 53, which yes, works out to about 7 and 15 decimal places

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    And that's all the accuracy computers provide us
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://en.wikipedia.org/wiki/IEEE_fl...point_standard
    Especially read David Goldberg's paper at the end
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could use BCD instead of binary if you want decimal precision ie. storing the number as base 10 digits instead of base 2 bits.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jafet
    You could use BCD instead of binary if you want decimal precision ie. storing the number as base 10 digits instead of base 2 bits.
    That doesn't eliminate the problem. Try to compute 1/7 (as a real value, not using integer arithmetic) and store than within a finite number of decimal places.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. floating point binary program, need help
    By ph34r me in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 07:10 AM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 10:55 AM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM