Re: Rounding off double precision
- From: Chip Coldwell <coldwell@xxxxxxxxx>
- Date: Mon, 31 Mar 2008 06:55:27 -0700 (PDT)
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"(snip)
has type double precision, in Fortran, the literal constant "0.1" has
type single precision. So
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
.
- Follow-Ups:
- Re: Rounding off double precision
- From: Dave Seaman
- Re: Rounding off double precision
- References:
- Rounding off double precision
- From: Bamm
- Re: Rounding off double precision
- From: Bamm
- Re: Rounding off double precision
- From: Richard Maine
- Re: Rounding off double precision
- From: Bamm
- Re: Rounding off double precision
- From: e p chandler
- Re: Rounding off double precision
- From: Bamm
- Re: Rounding off double precision
- From: glen herrmannsfeldt
- Re: Rounding off double precision
- From: Charles Coldwell
- Re: Rounding off double precision
- From: glen herrmannsfeldt
- Rounding off double precision
- Prev by Date: Re: data with metadata
- Next by Date: Re: F2003 standard: Can Class(*) be used in generic proogramming?
- Previous by thread: Re: Rounding off double precision
- Next by thread: Re: Rounding off double precision
- Index(es):
Relevant Pages
|
|