Re: Rounding off double precision



glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxx> writes:

Charles Coldwell wrote:
(snip, I wrote)
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
(snip)

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.

Well, the OP had:

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

A double precision constant as a single precision PARAMETER
used in a double precision expression. The C equivalent

#define PI 3.141592653589793238

as you say, the constant is double precision (without an f),
but PI itself is typeless.

Not true, after the pre-processor does macro expansion, all the
instances of PI in the code are replaced with a literal constant that
has type "double".

You can do things like:

#define ABS(x,y) ((x)>(y)?(x):(y))

as a typeless absolute value in C89.

That is true; after the pre-processor does macro expansion, all the
instances of ABS in the code are replaced with an expression whose
type depends on the types of "x" and "y" (e.g. if x is int and y is
double, the type of the expression "ABS(x,y)" is double).

Getting back to the OP,

parameter(Pi = 3.141592653589793238d0)

creates a literal constant with the double-precision value

400921fb54442d18 sign: 0 (+) exponent: 400 (1) mantissa: 921fb54442d18
(7074237752028440/4503599627370496)
exact value is: +1.5707963267948965579989817342720925807952880859375E0
* 2

The constant M_PI in /usr/include/math.h on my system has the value
3.14159265358979323846. Note that there are two additional digits
beyond what the OP put in his parameter. However, upon conversion to
a binary floating point value, the literal constant gets the
double-precision value

400921fb54442d18 sign: 0 (+) exponent: 400 (1) mantissa: 921fb54442d18
(7074237752028440/4503599627370496)
exact value is: +1.5707963267948965579989817342720925807952880859375E0
* 2

So, in fact, he has supplied enough decimal digits to get as close as
possible to the true value of pi in double precision.

Chip

--
Charles M. "Chip" Coldwell
"Turn on, log in, tune out"
Somerville, Massachusetts, New England
.



Relevant Pages

  • Re: Rounding off double precision
    ... "3.141592653589793238d0" and is a double precision value. ... A parameter is not a literal constant and vice-versa. ... literal constant to a single precision named parameter: ... you can assign a single precision literal constant to a double ...
    (comp.lang.fortran)
  • Re: Rounding off double precision
    ... A parameter is a symbolic constant, not a literal constant. ... support quad precision, there is no statement that will work. ... will work on any processor that uses increasing kind values for ...
    (comp.lang.fortran)
  • Re: Rounding off double precision
    ... has type double precision, in Fortran, the literal constant "0.1" has ... A double precision constant as a single precision PARAMETER ... but PI itself is typeless. ...
    (comp.lang.fortran)
  • Re: Rounding off double precision
    ... has type double precision, in Fortran, the literal constant "0.1" has ... A double precision constant as a single precision PARAMETER ... but PI itself is typeless. ...
    (comp.lang.fortran)
  • Re: Rounding off double precision
    ... because I didn't declare Pi to be double precision, ... C uses #define is places where Fortran uses PARAMETER, ... has type double precision, in Fortran, the literal constant "0.1" has ... type single precision. ...
    (comp.lang.fortran)