Is There a Better Double-Precision Assignment in Fortran 90?

前端 未结 2 1581
星月不相逢
星月不相逢 2020-11-29 13:12

In Fortran 90 (using gfortran on Mac OS X) if I assign a value to a double-precision variable without explicitly tacking on a kind, the precision doesn\'t \"take.\" What I

相关标签:
2条回答
  • 2020-11-29 13:52

    A real which isn't marked as double precision will be assumed to be single precision. Just because sometime later you assign it to a double precision variable, or convert it to double precision, that doesn't mean that the value will 'magically' be double precision. It doesn't look ahead to see how the value will be used.

    0 讨论(0)
  • 2020-11-29 13:58

    There are several questions linking here so it is good to state some details more explicitly with examples, especially for beginners.

    As stated by MRAB in his correct answer, an expression is always evaluated without any context, so

     0.12345678901234567890
    

    is a default (single) precision floating literal, no matter where does it appear. The same holds to floating point numbers in the exponential form

     0.12345678901234567890E0
    

    it is also a default precision number.

    If one want to use a double precision constant, one can use D instead of E in the above form. Even if such a double precision constant is assigned to a default precision variable, it is first treated as a double precision number and then it is converted to default precision.

    The way you are using in your question (employing the kind notation and several kind constants) is more general and more modern, but the principle is the same.

     0.12345678901234567890_sp
    

    is a number of kind sp and

     0.12345678901234567890_dp
    

    is a number of kind dp and it does not matter where do they appear.

    As your example shows, it is not only about assignment. In the line

     c = DBLE(0.12345678901234567890)
    

    first the number 0.12345678901234567890 is default precision. Then it is converted to double precision by DBLE, but that is done after some of the digits are already lost. Then this new double precision number is assigned to c.

    0 讨论(0)
提交回复
热议问题