This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran 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]

Getting equivalent 'double' parsing in c++ and fortran?


I'm porting some numerical fortran code to C++, and I need to minimize the discrepancies.

My problem is that when parsing a string to a double, g++ and fortran appear to be producing two different doubles.

Here's the test:

foo.cpp:
   #include <iostream> 
   #include <cstdio>
   using namespace std;
   int main() {
      double x;
      cin >> x;
      printf("%80.40f\n",x);
   }

foo.for:
      PROGRAM FOO
         IMPLICIT NONE
         DOUBLE PRECISION X
         READ (*,*) X
         WRITE (*,'d80.40') X
      END PROGRAM

I ran both of these programs, and supplied the number "2.7" on the console.

The two programs printed out these numbers to stdout.  (The ellipses didn't appear in the actual output.  They show where I didn't want to type any more numerals.) :

2.70000000000000017762568294...  (c++)
2.70000000000000017762570000...  (fortran)


Assuming that the two programs really are using two different double-precision numbers to record "2.7", does anyone know what I can do to the C++ code so that, when parsing strings to doubles, it will get the same double-precision number that the fortran code yields?

Attachment: smime.p7s
Description: S/MIME cryptographic signature


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