Skip to content

Fix missing _RP kind suffix on floating-point literals that are not binary-representable.#302

Open
chenyu76 wants to merge 1 commit into
libprima:mainfrom
chenyu76:fix_rp
Open

Fix missing _RP kind suffix on floating-point literals that are not binary-representable.#302
chenyu76 wants to merge 1 commit into
libprima:mainfrom
chenyu76:fix_rp

Conversation

@chenyu76

Copy link
Copy Markdown

In Fortran, an unsuffixed literal like 0.1 is of type default real (usually 32-bit single precision). When RP is configured as 64-bit double precision, comparing or combining real(RP) variables with unsuffixed literals triggers an implicit type conversion. Worse, 0.1 in single precision and 0.1_RP in double precision are different binary approximations. This means a condition such as

if (rand() <= 0.1) then

may evaluate differently in rare cases.

This PR appends _RP to unsuffixed floating-point literals that is not exactly
representable in binary.

How They Were Detected

The missing suffixes were identified by fortran_float_number_extract, a simple tool that

  1. parses Fortran source files into an AST using fortran-src,
  2. traverses the AST to find all real/integer literals without a kind suffix,
  3. checks whether those literals can be exactly represented in binary form and reports them if not.

After identification, I manually checked whether each literal needed to be rewritten.

Literals that were skipped

  • The literal 0.9 in /fortran/tests/testsuite/bigprob.f90:23:43.
  • The original code by Prof. Powell in /fortran/original/.

@chenyu76 chenyu76 requested a review from zaikunzhang as a code owner June 10, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant