Re: whole number constants



<dancerchris@xxxxxxxxxxxxx> wrote:

I still would like to understand what the best practice is. writing
global constants for whole numbers like 1 seems redundant.

It pretty much is. You will have no precision loss from expressing whole
numbers of "reasonable" magnitude in any actual compiler. It doesn't
matter whether you express it as a real or an integer, and it doesn't
matter what kind.

There are two cases where it does matter.

1. When you start doing operations. There is a big difference between
1/3.0 and 1/3.0d0. There is an even bigger difference between these and
1/3.

2. In procedure arguments, you must have the type and kind correct.
That's pretty much all there is to say about that. There is no single
type and kind that is always the best one. You just have to use the
correct one for the particular procedure in question.

I don't have a magic "best practice" other than to pay attention to
data type. Don't count on some single "best practice" to be the right
thing for all cases. Instead, make it a habit to consciously think about
the type and kind of your data. That being said, I personally do tend to
have a parameter named one because it is often convenient.

On your specific question

real(dp), parameter :: one = 1.0_dp
real(dp), parameter :: onend = 1_dp
real(dp), parameter :: onenk = 1.0
real(dp), parameter :: onei = 1

All of these are equivalent, except for the 1_dp. As noted previously,
that one is basically nonsense. The 1.0_dp form is technically the "most
correct". I also think it is the clearest; the other two valid ones seem
subject to human misreading, though the compiler will get them right. It
will implicitly convert them to the type and kind of the parameter. In
the case of whole numbers, this conversion will be exact. Note that it
is *NOT* exact in other cases. Something like

real(dp), parameter :: one_third = 1./3.

will be accurate only to single precision, and if you make the
right-hand-side 1/3, the resulting value will be just 0.

! which should I use ?

10 error = one-xnew/x
20 error = onend-xnew/x
30 error = onenk-xnew/x
40 error = onei-xnew/x

! or the more ledgable ?

50 error = 1-xnew/x

! or the slightly less ledgable ?

60 error = 1_dp-xnew/x

Other than the nonsense case of 1_dp, which was mentioned before, all
these are equivalent. You need to learn the basic rule of implicit
conversion for the intrinsic numeric operators. When a numeric operator
has operands of two different types or kinds, the operand of "lower
precision" is converted to the higher precision before performing the
operation. For the purposes of this description, integers count as
having "lower precision" than reals.

Furthermore, all your variously named parameters have the same type and
kind anyway. Once you know the type, kind, and value of the parameter,
the rest is irrelevant. It doesn't matter how that value got defined.
The compiler doesn't "remember" that your onei got defined by converting
an integer value.

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



Relevant Pages

  • Mordino beyond Johnny
    ... no matter how habitats. ... attract the precision. ... livers to brightly lower. ... Many allied ideological discharges will wistfully will the ...
    (sci.crypt)
  • Richard wrongly destroys the fashionable point
    ... districts state in view of tight districts, ... consume more stores. ... consumptions often wipe no matter how the attractive learners. ... Occasionally, go consult a precision! ...
    (comp.os.linux.misc)
  • Re: (1X)-P-(1Y)-P-(1NT)-DBL on final board of the Bermuda Bowl
    ... No. Inasmuch as I understand the Precision approach, the 1D opening is extremely loose. ... Whether your agreements leave it showing 2+, 1+ or 0+ diamonds, you open the bidding with 1D on all sorts of balanced trash, locate a major-suit fit if there is one and otherwise subside in 1NT unless partner has other ideas. ... You lose more from the hands where you have real diamonds (or real clubs for that matter but you didn't want to open 2C) and you haven't really communicated your un-balanced-ness to partner. ...
    (rec.games.bridge)
  • Re: accuracy
    ... The interface offers more precision than is actually ... timeBeginPeriod; ... That's documented as applying to "Multimedia timers", ... amazement times of some services were solid 0, no matter how long and how ...
    (comp.lang.ada)
  • Re: double to int conversion yields strange results
    ... giving me two different values for the int. ... the question raised is not about the precision of the ... is done in extended precision. ... It is the conversion to 'double' before the conversion to 'int' that ...
    (comp.lang.c)