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]

cout ignores the last 11 bits of long double


Here is the program:

#include <cmath>
#include <cstdio>
#include <iostream>

long double f( long double x)
{
  return pow( double(x), double( x*x*x));
}

void dump( long double x)
{
  unsigned char* p = (unsigned char*)&x;
  printf( "hex:    ");
  for( int i=10; --i >=0; )
    printf( "%02x", p[i]);
  printf( "\n");
}

void show( long double x)
{
  dump(x);
  cout.precision(41);
  cout << "cout:   " << x << '\n';
  printf( "printf: %.40Lf\n", x);
}

void doit( long double low, long double high)
{
  for( int i=0; i<90; i++) {
    long double mid = low + (high-low)/2;
    cout << '\n';
    cout << "i: " << i << ':' << '\n';
    show( mid);
    if ( mid == low)
      break;
    if ( f( mid) < 3 )
      low = mid;
    else
      high = mid;
  }
}

int main()
{
  doit( 1, 2);
}
=============

Here is part of the results:

i: 51:
hex:    3fffb89ba24891f7b800
cout:   1.4422495703074085238171164746745489537716
printf: 1.4422495703074085238171164746745489537720

i: 52:
hex:    3fffb89ba24891f7b400
cout:   1.4422495703074083017725115496432408690453
printf: 1.4422495703074084127948140121588949114080
-------------------------^^^^^^^^^^^^^^^^^^^^^^^^^
i: 53:
hex:    3fffb89ba24891f7b600
cout:   1.4422495703074085238171164746745489537716
printf: 1.4422495703074084683059652434167219325900
-------------------------^^^^^^^^^^^^^^^^^^^^^^^^^
i: 54:
hex:    3fffb89ba24891f7b500
cout:   1.4422495703074085238171164746745489537716
printf: 1.4422495703074084405503896277878084219990
-------------------------^^^^^^^^^^^^^^^^^^^^^^^^^

Notice that the cout result for i: 53 is the same
as for i: 51, but the printf result is different.

It looks like cout operator<< ignores the last 11 bits of a 
long double.

Dennis Yelle
-- 
I am a computer programmer and I am looking for a job.
There is a link to my resume here:  
http://table.jps.net/~vert/


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