Skip to content

Commit dff27c2

Browse files
committed
PWR064: Add entry and code examples
1 parent a5f7534 commit dff27c2

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

Checks/PWR064/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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](../PWR071/README.md) for more details.
20+
21+
> [!WARNING]
22+
> Avoid the use of 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+
> [!NOTE]
28+
> Unlike their C analogue, the default `kind` of unsuffixed literals is *not*
29+
> that of `double precision`.
30+
31+
### Code example
32+
33+
In the following example, an unsuffixed literal is used to initialize a
34+
`parameter` with an explicitly specified `kind`:
35+
36+
```fortran
37+
program test_literal_without_suffix
38+
implicit none
39+
integer, parameter :: dp = selected_real_kind(15, 307)
40+
real(kind=dp), parameter :: e = 2.718281828459045
41+
print *, e
42+
end program test_literal_without_suffix
43+
```
44+
45+
In many implementations the default real `kind` is not precise enough, so
46+
rounding errors will be introduced to the literal before it is assigned. To
47+
prevent this, suffix the literal with the same `kind` as the variable:
48+
49+
```fortran
50+
program test_literal_with_suffix
51+
implicit none
52+
integer, parameter :: dp = selected_real_kind(15, 307)
53+
real(kind=dp), parameter :: e = 2.718281828459045_dp
54+
print *, e
55+
end program test_literal_with_suffix
56+
```
57+
58+
### Related resources
59+
60+
- [PWR064 examples](https://github.com/codee-com/open-catalog/tree/test_literal_suffix/Checks/PWR064/)
61+
62+
- [PWR071: Prefer real(kind=kind_value) for declaring consistent floating
63+
types](../PWR071/README.md)
64+
65+
### References
66+
67+
- ["It Takes All KINDs - Doctor
68+
Fortran"](https://stevelionel.com/drfortran/2017/03/27/doctor-fortran-in-it-takes-all-kinds/),
69+
Steve Lionel. [last checked Apr 2026]

Checks/PWR064/example.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
! PWR064: Precision loss in floating-point constant
2+
3+
program test_literal_without_suffix
4+
implicit none
5+
integer, parameter :: dp = selected_real_kind(15, 307)
6+
real(kind=dp), parameter :: e = 2.718281828459045
7+
print *, e
8+
end program test_literal_without_suffix

Checks/PWR064/solution.f90

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
! PWR064: Precision loss in floating-point constant
2+
3+
program test_literal_with_suffix
4+
implicit none
5+
integer, parameter :: dp = selected_real_kind(15, 307)
6+
real(kind=dp), parameter :: e = 2.718281828459045_dp
7+
print *, e
8+
end program test_literal_with_suffix

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ designed to demonstrate:
8585
| [PWR060](Checks/PWR060/) | Consider loop fission to separate gather memory access pattern | optimization | | | | | ✓ | ✓ | ✓ | |
8686
| [PWR062](Checks/PWR062/) | Consider loop interchange by removing accumulation on array value | optimization | | | | | ✓ | ✓ | ✓ | |
8787
| [PWR063](Checks/PWR063/) | Avoid using legacy Fortran constructs | correctness, modernization, security | [CWE-477](https://cwe.mitre.org/data/definitions/477.html), [CWE-1075](https://cwe.mitre.org/data/definitions/1075.html), [CWE-1119](https://cwe.mitre.org/data/definitions/1119.html) | [6.27](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.28](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.31](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.54](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.58](https://j3-fortran.org/doc/year/23/23-241.pdf) | | | | ✓ | | |
88+
| [PWR064](Checks/PWR064/) | PWR064: Precision loss in floating-point constant | correctness, security | [CWE-197](https://cwe.mitre.org/data/definitions/197.html) | [6.2](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.6](https://j3-fortran.org/doc/year/23/23-241.pdf) | [FLP34-C](https://wiki.sei.cmu.edu/confluence/display/c/FLP34-C.+Ensure+that+floating-point+conversions+are+within+range+of+the+new+type) | | | ✓ | | |
8889
| [PWR068](Checks/PWR068/) | Call procedures through explicit interfaces, preferably as module procedures | correctness, modernization, security | [CWE-628](https://cwe.mitre.org/data/definitions/628.html) | [6.8](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.9](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.10](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.11](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.32](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.34](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.53](https://j3-fortran.org/doc/year/23/23-241.pdf) | [DCL07-C](https://wiki.sei.cmu.edu/confluence/display/c/DCL07-C.+Include+the+appropriate+type+information+in+function+declarators), [DCL31-C](https://wiki.sei.cmu.edu/confluence/display/c/DCL31-C.+Declare+identifiers+before+using+them), [EXP37-C](https://wiki.sei.cmu.edu/confluence/display/c/EXP37-C.+Call+functions+with+the+correct+number+and+type+of+arguments) | | | ✓ | | |
8990
| [PWR069](Checks/PWR069/) | Use the keyword only to explicitly state what to import from a module | correctness, modernization, security | | [6.21](https://j3-fortran.org/doc/year/23/23-241.pdf) | [DCL23-C](https://wiki.sei.cmu.edu/confluence/display/c/DCL23-C.+Guarantee+that+mutually+visible+identifiers+are+unique) | | | ✓ | | ✓[^1] |
9091
| [PWR070](Checks/PWR070/) | Declare array dummy arguments as assumed-shape arrays | correctness, modernization, security | [CWE-130](https://cwe.mitre.org/data/definitions/130.html) | [6.8](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.9](https://j3-fortran.org/doc/year/23/23-241.pdf), [6.10](https://j3-fortran.org/doc/year/23/23-241.pdf) | [API02-C](https://wiki.sei.cmu.edu/confluence/display/c/API02-C.+Functions+that+read+or+write+to+or+from+an+array+should+take+an+argument+to+specify+the+source+or+target+size) | | | ✓ | | |

0 commit comments

Comments
 (0)