From: baf on
Why does the following program produce 0.5 for c and 0 for d?

program testdp
implicit none
integer, parameter::dp=selected_real_kind(14)
real(dp),parameter::a=1_dp,b=2_dp,c=a/b,d=1_dp/2_dp
write(*,*)c,d
end program testdp
From: m_b_metcalf on
On Mar 11, 9:42 pm, baf <b...(a)nowhere.com> wrote:
> Why does the following program produce 0.5 for c and 0 for d?
>
> program testdp
>   implicit none
>   integer, parameter::dp=selected_real_kind(14)
>   real(dp),parameter::a=1_dp,b=2_dp,c=a/b,d=1_dp/2_dp
>   write(*,*)c,d
> end program testdp

Because a real literal constant must contain at least one of: a
decimal point or an exponent part (MR&C, p. 15). Yours contain
neither. Your constants are integers.

Regards,

Mike Metcalf
From: glen herrmannsfeldt on
baf <baf(a)nowhere.com> wrote:
> Why does the following program produce 0.5 for c and 0 for d?

> program testdp
> implicit none
> integer, parameter::dp=selected_real_kind(14)
> real(dp),parameter::a=1_dp,b=2_dp,c=a/b,d=1_dp/2_dp
> write(*,*)c,d
> end program testdp

1_dp and 2_dp are still integers, even if they have
the KIND of dp.

You would have a compile time error if no appropriate
INTEGER KIND existed.

It might have been nice if it was reqiured for KINDs not
to overlap between different types.

-- glen
From: baf on


On 03/11/2010 12:42 PM, baf wrote:
> Why does the following program produce 0.5 for c and 0 for d?
>
> program testdp
> implicit none
> integer, parameter::dp=selected_real_kind(14)
> real(dp),parameter::a=1_dp,b=2_dp,c=a/b,d=1_dp/2_dp
> write(*,*)c,d
> end program testdp

Both comments make perfect sense. I did a search and replace in a
larger code for items like 4d0 changing it to 4_dp, but forgot I needed
to add either an exponent or a decimal point. Trying to clean up old
code and got bit. Thanks to both of you for your answers.
From: Richard Maine on
glen herrmannsfeldt <gah(a)ugcs.caltech.edu> wrote:

> baf <baf(a)nowhere.com> wrote:
> > Why does the following program produce 0.5 for c and 0 for d?
>
> > program testdp
> > implicit none
> > integer, parameter::dp=selected_real_kind(14)
> > real(dp),parameter::a=1_dp,b=2_dp,c=a/b,d=1_dp/2_dp
> > write(*,*)c,d
> > end program testdp
>
> 1_dp and 2_dp are still integers, even if they have
> the KIND of dp.
>
> You would have a compile time error if no appropriate
> INTEGER KIND existed.
>
> It might have been nice if it was reqiured for KINDs not
> to overlap between different types.

Yes. I personally think it would have been nicer still if kinds weren't
numeric. F90 introduced features that programmers can use to get away
from darn near everything being integer codes, but the language itself
doesn't take good advantage of those features. I sometimes think that at
least some of the people writing the language specs weren't yet
themselves fuly comfortable with some of the newer features. (Other
times I am sure of it instead of just thinking so. :-)).

Anyway, spilt milk aside, the thing to understand is that dp above is
just an integer parameter. Odds are that it is 8, though that will vary.
In any case, it is a named parameter for some integer value. That is
*ALL* it is. The fact that its value was computed using
selected_real_kind does *NOT* restrict it to being used as a real kind
parameter. That's the most obviously useful way to use it, but you can
use it anywhere else that you could use an integer parameter with the
value 8 (or whatever).

Unfortunately, one of those other places that you can use a parameter
with the value 8 is as a kind specifier for integer. Odds are that 1_dp
and 2_dp are 8-byte integers. The reason I say it is unfortunate is that
it invites exactly the kind of error that the OP had here. The code
shown is perfectly valid; it is just highly unlikely that it does what
the writer intended.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain