Re: Rounding off double precision



glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxx> writes:

Bamm wrote:
(snip)

However I have since found where I went wrong. I was using

double precision Speed, Azimuth, V
Azimuth = 90.
parameter(Pi = 3.141592653589793238d0)
V = Speed * cos(Azimuth * Pi / 180.)
print *, V

The problem with the unusually large value of -4.37113886E-06 arose
because I didn't declare Pi to be double precision, even though I
assigned a double precision value to it.

This problem might be more likely to occur to ex-C programmers.

C uses #define is places where Fortran uses PARAMETER,
but #define is typeless.

There's a little more to it than that. In C, the literal constant "0.1"
has type double precision, in Fortran, the literal constant "0.1" has
type single precision. So

double x = 0.1;

in C gives x the double precision value

+1.600000000000000088817841970012523233890533447265625 / 16

whereas

DOUBLE PRECISION X
X = 0.1

in Fortran gives X the single precision value

+1.60000002384185791015625 / 16

Actually, now that I look at it, the OP actually had the opposite
problem, analogous to

X = 0.1D0

where X is implicitly typed default real (single precision), but the
literal constant has type double precision.

Chip

--
Charles M. "Chip" Coldwell
"Turn on, log in, tune out"
GPG Key ID: 852E052F
GPG Key Fingerprint: 77E5 2B51 4907 F08A 7E92 DE80 AFA9 9A8F 852E 052F
.



Relevant Pages

  • Object Oriented Optimization
    ... I have the following problem with re-using a fortran 77 optimization ... double precision, dimension, intent:: co ... several objectives and constraints. ...
    (comp.lang.fortran)
  • Re: help about ARPACK solver
    ... Fortran uses single precision variables by default. ... Fortran program, that is *single* precision. ... I still dont know what kind of criteria Matlab adopts to form ...
    (sci.math.num-analysis)
  • Re: help about ARPACK solver
    ... Fortran uses single precision variables by default. ... Fortran program, that is *single* precision. ...
    (sci.math.num-analysis)
  • Re: help about ARPACK solver
    ... could be as precise as, however, when I used the eigenvector ... Fortran uses single precision variables by default. ... Fortran program, that is *single* precision. ...
    (sci.math.num-analysis)
  • Re: help about ARPACK solver
    ... Matlab's eigsfunction uses ARPACK, so logically your Fortran ... Matlab uses 'double precision' variables throughout by default. ... Fortran uses single precision variables by default. ... Fortran program, that is *single* precision. ...
    (sci.math.num-analysis)