Skip to content

[RFC]: Replace inline NaN literals with stdlib NaN macro across float32 and float64 packages #11743

@manit2004

Description

@manit2004

Instructions

  1. Read the issue description below.
  2. Review the example pull request linked below.
  3. Find one package containing inline NaN generation in a C source file (for example, 0.0/0.0) and update it according to this issue.
  4. Follow all additional guidance in this issue.

Description

This RFC proposes replacing inline NaN generation (for example, 0.0/0.0 or 0.0f/0.0f) in C source files, examples, benchmarks and README C usage snippets with the stdlib NaN constant from @stdlib/constants/float64/nan or @stdlib/constants/float32/nan.

In short, this RFC seeks to refactor code from this pattern:

return 0.0/0.0; // NaN

to this pattern:

#include "stdlib/constants/float64/nan.h"
...
return STDLIB_CONSTANT_FLOAT64_NAN;

The migration is demonstrated in this PR commit fb7122c

In particular, notice three things:

  1. We add the C header include for the NaN constant.
  2. We replace inline NaN generation with STDLIB_CONSTANT_FLOAT64_NAN.
  3. We add the dependency @stdlib/constants/float64/nan to manifest configuration entries which compile that same C source file (typically build, benchmark, and examples, when applicable).

By performing this refactoring, we facilitate:

  1. Consistent NaN handling across packages and targets.
  2. Better readability and maintainability in C implementations.

Note:

  • There are also inline Float32 NaN occurrences as 0.0f/0.0f which needs to be replaced by STDLIB_CONSTANT_FLOAT32_NAN from @stdlib/constants/float32/nan. Refer to this commit 767d45c for reference.

Steps

Given the relatively widespread use of inline NaN generation, this RFC is an open call for contributors to do the following:

  1. Study the example PR refactor: use constants/float64/nan in stats/strided/dnanmeanpn #11768 , refactor: use constants/float32/nan in stats/strided/snanmeanpn #11769 linked above, as it demonstrates the changes we are seeking.

  2. Ensure your local development environment is set up by following the contributing guide

  3. Find one package containing inline NaN generation in C source files.

  4. Update that package, and only that package, to use STDLIB_CONSTANT_FLOAT64_NAN from @stdlib/constants/float64/nan.

  5. Add the include statement for stdlib/constants/float64/nan.h in updated C sources.

  6. Update the package manifest dependencies for all task configurations that compile the updated source file.

  7. Build the Node add-on and run the example, test and benchmark C files using the following commands

    General command patterns:

    cd /home/stdlib
    # Install all repo dependencies (only needed once after cloning)
    make install
    # Build the Node add-on for the specific package
    make install-node-addons NODE_ADDONS_PATTERN="<package-path>"
    # Run C examples, benchmarks, native tests
    make EXAMPLES_FILTER=".*/<package-path>/.*" examples-c
    make BENCHMARKS_FILTER=".*/<package-path>/.*" benchmark-c
    TEST_NATIVE=1 make test TESTS_FILTER=".*/<package-path>/.*"

    For example, for stats/base/dists/normal/pdf:

    cd /home/stdlib
    # Install all repo dependencies (only needed once after cloning)
    make install
    # Build the Node add-on for the specific package
    make install-node-addons NODE_ADDONS_PATTERN="stats/strided/dnanmeanpn"
    # Run C examples, benchmarks, native tests
    make EXAMPLES_FILTER=".*/stats/strided/dnanmeanpn/.*" examples-c
    make BENCHMARKS_FILTER=".*/stats/strided/dnanmeanpn/.*" benchmark-c
    TEST_NATIVE=1 make test TESTS_FILTER=".*/stats/strided/dnanmeanpn/.*"
  8. Commit your changes.

  9. Submit a PR updating that package only.

  10. Use this PR title template:
    refactor: use constants/float64/nan in < package-name >

  11. In the PR body, use the phrase Resolves a part of , and not Resolves or Closes, because this is a tracking issue.

Related Issues

None.

Questions

None.

Other

  • For each pull request, please update only one package.
  • Do not make unrelated or stylistic changes. Only replace inline NaN generation with the shared NaN constant and make required dependency updates.
  • As this is a Good First Issue, contributors are strongly encouraged to avoid AI for their first contributions and to follow existing project conventions closely.

Checklist

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions