www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/05/11/19:15:20

Message-ID: <3AFC71C1.D3CDF864@jps.net>
From: Dennis Yelle <dennis51 AT jps DOT net>
X-Mailer: Mozilla 4.75 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: cout ignores the last 11 bits of long double
Lines: 84
Date: Fri, 11 May 2001 16:12:01 -0700
NNTP-Posting-Host: 216.119.26.22
X-Complaints-To: abuse AT onemain DOT com
X-Trace: nntp1.onemain.com 989622499 216.119.26.22 (Fri, 11 May 2001 19:08:19 EDT)
NNTP-Posting-Date: Fri, 11 May 2001 19:08:19 EDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019