How to declare the precision of a number as a custom parameter?

In 2013, the question arose of converting large working code with double precision: " Converting working code from point-to-point accuracy to fourth: how to read four times precision numbers in FORTRAN from the input file ", and by consensus it was to declare variables using the "WP" custom parameter , which indicates "working accuracy", instead of a separate version of the program with variables declared using D +01 and another version using Q + 01. Thus, we can easily switch back and forth by defining WP = real128 or WP = real64 at the top, and the rest does not need to be changed.

But how do we do this?

I tried the sentence in the answer to this question by making a simple TEST.F90 code:

PROGRAM TEST use ISO_FORTRAN_ENV WP= real128 IMPLICIT NONE real (WP) :: X X= 5.4857990945E-4_WP END PROGRAM TEST 

compiled with:

 ~/gcc-4.6/bin/gfortran -o tst.x TEST.F90 

But he gives:

  IMPLICIT NONE 1 Error: Unexpected IMPLICIT NONE statement at (1) QLEVEL16.F90:5.12: real (WP) :: MEL 1 Error: Parameter 'wp' at (1) has not been declared or is a variable, which does not reduce to a constant expression QLEVEL16.F90:6.29: MEL= 5.4857990945E-4_WP 1 Error: Missing kind-parameter at (1) 
+1
source share
2 answers

The view specifier must be an integer parameter - and you do not declare it appropriately. In addition, implicit none must go before any declaration.

Here is a working version on both issues:

 PROGRAM TEST use ISO_FORTRAN_ENV IMPLICIT NONE integer, parameter :: WP= real128 real (WP) :: X X= 5.4857990945E-4_WP END PROGRAM TEST 
+4
source

Actually a lot of code using this WP approach. Many of them choose _ * _ a benign internal function. But I think there is a โ€œsimplerโ€ way. It should use the default precision without specifying any kind of keyword and use the compiler flag to select the default precision.

Pro - this method is simpler if you do not need accurate precision control for each variable. Con is something that will depend heavily on compiler flags, which differ for each compiler or may even be unavailable.

For gfortran, there are more flags -freal4-real8 or -freal4-real16 to advance each explicitly set lower precision to higher precision.

0
source

Source: https://habr.com/ru/post/1496058/


All Articles