This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

help: double precision problem


Hi,

Below is a simple code that shows a double precision problem.
( We try to check that 1987654321.11 + 0.1 = 198765432.21 )

I compiled this on the following platform: Mandrake GNU/Linux 8.1, GCC
3.0.3, AMD Athlon Thunderbird

The output is:
a
1987654321.1099998951
1987654321.110000
00111101 00001010 01000111 10101100 01001100 10011110 11011101 01000001

b
1987654321.2100000381
1987654321.210000
10100100 01110000 01001101 10101100 01001100 10011110 11011101 01000001

c <> b

c
1987654321.2099997997
1987654321.210000
10100011 01110000 01001101 10101100 01001100 10011110 11011101 01000001


The values of a and b in the code have 12 digits. I read somewhere that 15
digits are valid for an IEEE 784 double precision format.
So, did I reach the precision limit ?

I'm surprised to have such a problem with so few digits after the dot.

Does anyone have an explanation ?

(I have the same problem but with different precision on IBM AIX 4.3 with
xlC 5.0.2)

Thanks for any help

JD

===================================================
#include <iostream>

using namespace std;


// Just displays the binary representation of any given buffer
void bindis( unsigned char *adr , int len )
{
 for( int i=0; i<len; i++ )
  {
   unsigned char b = *(adr+i);
   for( int j = 0, p=128 ; j < 8 ; j++, p/=2 )
     {
      if( (b & p) == p ) printf("1");
                    else printf("0");
     }
    printf(" ");
  }
 printf("\n\n");
}




int main( void )
{
  double a = 1987654321.11;
  double b = 1987654321.21;

  printf("a\n");
  printf( "%.10f\n", a );
  printf( "%lf\n"  , a );
  bindis( (unsigned char*) &a , sizeof(double) );

  printf("b\n");
  printf( "%.10f\n", b );
  printf( "%lf\n"  , b );
  bindis( (unsigned char*) &b , sizeof(double) );

  double c = a + 0.1;

  if( c == b ) cout << "c == b" << endl << endl;
          else cout << "c <> b" << endl << endl;

  printf("c\n");
  printf( "%.10f\n", c );
  printf( "%lf\n"  , c );
  bindis( (unsigned char*) &c , sizeof(double) );

  return 0;
}






Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]