fortranprecisionfortran90# Will I get more accuracy in Fortran with `real(wp)`?

## Update 1

Have I got more accuracy in my code by replacing all :

`real(dl) variable1`

by putting at the beginning of each Fortran90 source files :

```
integer, parameter :: wp = selected_real_kind(15,307)
```

and declare variable like this :

```
real(wp) variable1
```

You are right, a simple program gives :

```
program main
implicit none
integer, parameter :: dl = KIND(1.d0)
integer, parameter :: wp = selected_real_kind(15,307)
real(kind=dl) :: dl_number
real(kind=wp) :: wp_number
write(*,*) 'epsilon for dl number', epsilon(dl_number)
write(*,*) 'precision for dl number', precision(dl_number)
write(*,*) 'epsilon for wp number', epsilon(wp_number)
write(*,*) 'precision for wp number', precision(wp_number)
stop
end
```

And the output :

```
epsilon for dl number 2.2204460492503131E-016
precision for dl number 15
epsilon for wp number 2.2204460492503131E-016
precision for wp number 15
```

So there is no need to use `wp = selected_real_kind(15,307)`

instead of `dl = KIND(1.d0)`

.

Solution

In general no.

There are two issues:

Is wp of higher precision that dl ? This depends on the compiler and platform.

Is the numerical result improved by the increase in accuracy of the floating point numbers? In some case, the accuracy is limited by the algorithm or the data and you don't get anything from this except a slower program.

As a first diagnosis, anyway, execute

```
write(*,*) 'dl =', dl
write(*,*) 'wp =', wp
```

to see whether the kinds are actually the same.

Then, you can obtain some information about the kinds:

```
real(kind=dl) :: dl_number
real(kind=wp) :: wp_number
write(*,*) 'epsilon for dl number', epsilon(dl_number)
write(*,*) 'precision for dl number', precision(dl_number)
write(*,*) 'epsilon for wp number', epsilon(wp_number)
write(*,*) 'precision for wp number', precision(wp_number)
```

`epsilon`

returns the smallest representable number for the kind, and `precision`

the decimal precision.

You should anyway have an idea of what accuracy is required for your computation and work from that.

As pointed out by albert and Ian Bush, there are many other places in the code where you need to check out the issue of numerical representation.

- Literal constants need special care.
`1.0/3.0`

and`1._wp/3._wp`

have significantly different values for instance. - Some functions will also return default real value (i.e. single precision) unless requested, such as the
`real`

intrinsic. - ...

- Python Read Fortran Binary File
- In Fortran2003, is 1D Assumed shape array interoperable with C?
- Giving a generic subroutine to a structure as its containing procedure
- Why am I getting this error? <class 'TypeError'>: wrong type
- GNU Fortran - Function 'dcosd' has no IMPLICIT type
- f2py does not properly import, despite successfully compiling
- Error in Python trying to create a list of a certain length after a call to Fortran to get the length
- How to get command line arguments of unknown length in Fortran?
- implicit real - complex conversion in fortran
- Optimal way to create once, then frequently access to a large array in Fortran
- How do you iterate through an array in Fortran?
- Why Intel Fortran + Intel MPI report warn(error) when using MPI_Bcast?
- Convert c_int to default kind integer
- Program in Fortran print different results with each execution
- How does work the OpenMP "nonmonotonic:dynamic" schedule?
- GFortran error: ld: library not found for -lSystem when trying to compile
- Write unformatted (binary data) to stdout
- How to call Fortran's FINDLOC within numpy's f2py
- QR factorization in Fortran
- Distribution of for loop iterations over triangular matrix with MPI
- Why MPI_REDUCE shows different number at some array locations?
- Conditional compilation in gfortran
- Fortran with Sparse BLAS not flushing memory
- Standard conforming way to get command line arguments in FORTRAN77
- Fortran C interface for program containing non-c-interoperable derived type
- Processing a shared array by a passed index in a subroutine in a parallel loop
- Gfortran type mismatch error despite "-fallow-argument-mismatch" flag
- Removing whitespace in string
- Pointer to OpenBLAS subroutines in fortran
- Valgrind complains reading from a file