|
| 1 | +# PWR064: Precision loss in floating-point constant |
| 2 | + |
| 3 | +### Issue |
| 4 | + |
| 5 | +An unsuffixed real literal has default precision, which may cause precision loss |
| 6 | +if the value is actually destined for a wider variable. |
| 7 | + |
| 8 | +### Actions |
| 9 | + |
| 10 | +Suffix the floating-point literal with an appropriately precise `kind` derived |
| 11 | +from either `selected_real_kind()` or the `iso_fortran_env` module. |
| 12 | + |
| 13 | +### Relevance |
| 14 | + |
| 15 | +Floating-point literals without a `kind` suffix have default precision, which |
| 16 | +may offer inconsistent behavior across compilers or compilation flags. The |
| 17 | +default `kind` behavior is the same as for variables, see [PWR071: Prefer |
| 18 | +real(kind=kind_value) for declaring consistent floating |
| 19 | +types](../PWR014/README.md) for more details. |
| 20 | + |
| 21 | +> [!WARNING] |
| 22 | +> Avoid relying on compilation flags such as GNU's `-fdefault-real-8` to |
| 23 | +> increase the precision of unsuffixed literals. While many compilers interpret |
| 24 | +> these correctly, they are ultimately not part of the Fortran standard, and |
| 25 | +> thus code relying on them may silently break on other compilers. |
| 26 | +
|
| 27 | +It is worth highlighting that, unlike their C programming language analogue, the |
| 28 | +default `kind` of unsuffixed literals is *not* that of `double precision`. |
| 29 | + |
| 30 | +### Code example |
| 31 | + |
| 32 | +In the following example, an unsuffixed literal is used to initialize a |
| 33 | +`parameter` with an explicitly specified `kind`: |
| 34 | + |
| 35 | +```fortran |
| 36 | +program test_no_literal_suffix |
| 37 | + implicit none |
| 38 | + integer, parameter :: dp = selected_real_kind(15, 307) |
| 39 | + real(kind=dp), parameter :: e = 2.718281828459045 |
| 40 | + print *, e |
| 41 | +end program test_no_literal_suffix |
| 42 | +``` |
| 43 | + |
| 44 | +In many implementations the default real `kind` is not precise enough, so |
| 45 | +rounding errors will be introduced to the literal before it is assigned. To |
| 46 | +prevent this, suffix the literal with the same `kind` as the variable: |
| 47 | + |
| 48 | +```fortran |
| 49 | +program test_literal_suffix |
| 50 | + implicit none |
| 51 | + integer, parameter :: dp = selected_real_kind(15, 307) |
| 52 | + real(kind=dp), parameter :: e = 2.718281828459045_dp |
| 53 | + print *, e |
| 54 | +end program test_literal_suffix |
| 55 | +``` |
| 56 | + |
| 57 | +### Related resources |
| 58 | + |
| 59 | +- [PWR064 examples](https://github.com/codee-com/open-catalog/tree/test_literal_suffix/Checks/PWR064/) |
| 60 | + |
| 61 | +- [PWR071: Prefer real(kind=kind_value) for declaring consistent floating |
| 62 | +types](../PWR071/README.md) |
| 63 | + |
| 64 | +### References |
| 65 | + |
| 66 | +- ["It Takes All KINDs - Doctor |
| 67 | +Fortran"](https://stevelionel.com/drfortran/2017/03/27/doctor-fortran-in-it-takes-all-kinds/), |
| 68 | +Steve Lionel. [last checked Apr 2026] |
0 commit comments