-
Notifications
You must be signed in to change notification settings - Fork 12
Add PWR064 #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add PWR064 #143
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # PWR064: Precision loss in floating-point constant | ||
|
|
||
| ### Issue | ||
|
|
||
| An unsuffixed real literal has default precision, which may cause precision loss | ||
| if the value is actually destined for a wider variable. | ||
|
|
||
| ### Actions | ||
|
|
||
| Suffix the floating-point literal with an appropriately precise `kind` derived | ||
| from either `selected_real_kind()` or the `iso_fortran_env` module. | ||
|
|
||
| ### Relevance | ||
|
|
||
| Floating-point literals without a `kind` suffix have default precision, which | ||
| may offer inconsistent behavior across compilers or compilation flags. The | ||
| default `kind` behavior is the same as for variables, see [PWR071: Prefer | ||
| real(kind=kind_value) for declaring consistent floating | ||
| types](../PWR071/README.md) for more details. | ||
|
|
||
| > [!WARNING] | ||
| > Avoid the use of compilation flags such as GNU's `-fdefault-real-8` to | ||
| > increase the precision of unsuffixed literals. While many compilers interpret | ||
| > these correctly, they are ultimately not part of the Fortran standard, and | ||
| > thus code relying on them may silently break on other compilers. | ||
|
|
||
| > [!NOTE] | ||
| > Unlike their C analogue, the default `kind` of unsuffixed literals is *not* | ||
| > that of `double precision`. | ||
|
|
||
| ### Code example | ||
|
|
||
| In the following example, an unsuffixed literal is used to initialize a | ||
| `parameter` with an explicitly specified `kind`: | ||
|
|
||
| ```fortran | ||
| ! example.f90 | ||
| program test_literal_without_suffix | ||
| implicit none | ||
| integer, parameter :: dp = selected_real_kind(15, 307) | ||
| real(kind=dp), parameter :: e = 2.718281828459045 | ||
| print *, e | ||
| end program test_literal_without_suffix | ||
| ``` | ||
|
|
||
| In many implementations the default real `kind` is not precise enough, so | ||
| rounding errors will be introduced to the literal before it is assigned. To | ||
| prevent this, suffix the literal with the same `kind` as the variable: | ||
|
|
||
| ```fortran | ||
| ! solution.f90 | ||
| program test_literal_with_suffix | ||
| implicit none | ||
| integer, parameter :: dp = selected_real_kind(15, 307) | ||
| real(kind=dp), parameter :: e = 2.718281828459045_dp | ||
| print *, e | ||
| end program test_literal_with_suffix | ||
| ``` | ||
|
|
||
| ### Related resources | ||
|
|
||
| - [PWR064 examples](https://github.com/codee-com/open-catalog/tree/test_literal_suffix/Checks/PWR064/) | ||
|
|
||
| - [PWR071: Prefer real(kind=kind_value) for declaring consistent floating | ||
| types](../PWR071/README.md) | ||
|
|
||
| ### References | ||
|
|
||
| - ["It Takes All KINDs - Doctor | ||
| Fortran"](https://stevelionel.com/drfortran/2017/03/27/doctor-fortran-in-it-takes-all-kinds/), | ||
| Steve Lionel. [last checked Apr 2026] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| ! PWR064: Precision loss in floating-point constant | ||
|
|
||
| program test_literal_without_suffix | ||
| implicit none | ||
| integer, parameter :: dp = selected_real_kind(15, 307) | ||
| real(kind=dp), parameter :: e = 2.718281828459045 | ||
| print *, e | ||
| end program test_literal_without_suffix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| ! PWR064: Precision loss in floating-point constant | ||
|
|
||
| program test_literal_with_suffix | ||
| implicit none | ||
| integer, parameter :: dp = selected_real_kind(15, 307) | ||
| real(kind=dp), parameter :: e = 2.718281828459045_dp | ||
| print *, e | ||
| end program test_literal_with_suffix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.