Re: Numerical accuracy of C++ and Fortran programs on 32 bit machines



osmium wrote:
> "Thomas Smid" writes:
>
> > For C++:
> > _____________________________
> > #include <iostream.h>
> > #include <fstream.h>
> > #include <stdlib.h>
> > #include <math.h>
> >
> >
> > void main() {
> > ofstream outfile ("out.txt");
> > double x,y,z;
> > x=4.0E0;
> > y=3.0E0;
> > z=x/y;
> >
> > outfile <<z ;
> > system("PAUSE");
> > return 0;
> > }
> > _________________________
> >
> > This program prints out 1.33333 for z (which is even only 6 significant
> > digits) when compiled end executed with Dev-C++, even though all
> > variables are declared double (and as far as I am aware this compiler
> > supports only double precision anyway; there are for instance no Float
> > versions of the mathematical functions in the library)
>
> You are seeing the constraints imposed by the printing process. There is a
> variable called precision and the default value is what you are using. Look
> for something called set precision, IIRC there are both function and
> manipulator forms of the desired thingy.


Thanks a lot. When I use

outfile << setprecision (20) << z ;

it prints indeed all siginificant digits.
I still had to use

#include <iomanip.h>

additionally in the header as otherwise it would assume that
setprecision is a variable.

This still leaves the discrepancy in the Fortran example though where
it is obviously not an I/O matter.

Thomas

.



Relevant Pages

  • Re: qfloat extended precision in Cephes library
    ... for Mathematical Functions (Ellis Horwood Series in Mathematics and Its ... precision offering in particular. ... qfloat.h C++ wrapper makes it easier to write math code that looks like ... Some of Mr. Moshier's built-in routines, ...
    (sci.math.num-analysis)
  • Re: Compiler optimization problem in MSVC 2003 Release mode
    ... variable is sufficient to hold a double precision value. ... void main ... Without this flag, ... to float until the value has to be spilled from FPU register to memory). ...
    (microsoft.public.vc.language)
  • Re: Float comparison
    ... void add{ ... This is a basic building block to double the maxmimum precision ... is an approximation and not assuming that the 'c' used in the calculation of ... For this technique to work it is absolutely *not* necessary to know what the ...
    (comp.lang.c)
  • Re: Bid these hands
    ... W opens 1S (precision) and you have the rest of the auction to ...
    (rec.games.bridge)
  • %a correctly rounded to specified precision
    ... Given an IEEE 754 double implementation, ... int main (void) ... the correctly rounded value of DBL_MAX the given precision, ...
    (comp.std.c)

Loading