Skip to content

feat: add C implementation for math/base/special/heaviside #6972

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

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from

Conversation

anandkaranubc
Copy link
Contributor

Progresses #649

Description

What is the purpose of this pull request?

This pull request:

  • Adds the complete C implementation for math/base/special/heaviside

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

@anandkaranubc anandkaranubc marked this pull request as draft May 10, 2025 19:42
@stdlib-bot stdlib-bot added Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review. and removed Needs Review A pull request which needs code review. labels May 10, 2025
@stdlib-bot
Copy link
Contributor

stdlib-bot commented May 10, 2025

Coverage Report

Package Statements Branches Functions Lines
math/base/special/heaviside $\color{green}316/316$
$\color{green}+100.00\%$
$\color{green}22/22$
$\color{green}+100.00\%$
$\color{green}4/4$
$\color{green}+100.00\%$
$\color{green}316/316$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this PR.

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: passed
  - task: lint_package_json
    status: passed
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: passed
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: passed
  - task: lint_c_benchmarks
    status: passed
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@anandkaranubc
Copy link
Contributor Author

/stdlib update-copyright-years

@stdlib-bot stdlib-bot added the bot: In Progress Pull request is currently awaiting automation. label May 10, 2025
@stdlib-bot stdlib-bot removed the bot: In Progress Pull request is currently awaiting automation. label May 10, 2025
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@anandkaranubc anandkaranubc marked this pull request as ready for review May 10, 2025 21:09
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label May 10, 2025
@anandkaranubc anandkaranubc added Feature Issue or pull request for adding a new feature. C Issue involves or relates to C. and removed Needs Review A pull request which needs code review. labels May 10, 2025
#endif

// Enumeration of function continuity:
typedef enum STDLIB_MATH_CONTINUITY {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STDLIB_BASE_HEAVISIDE_CONTINUITY

// Enumeration of function continuity:
typedef enum STDLIB_MATH_CONTINUITY {
// Half-maximum:
STDLIB_MATH_HALF_MAXIMUM = 0,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STDLIB_BASE_HEAVISIDE_CONTINUITY_HALF_MAXIMUM

STDLIB_NAPI_ARGV( env, info, argv, argc, 2 );
STDLIB_NAPI_ARGV_DOUBLE( env, x, argv, 0 );
STDLIB_NAPI_ARGV_INT32( env, continuity, argv, 1 );
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_base_heaviside( x, (STDLIB_MATH_CONTINUITY)continuity ), out );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_base_heaviside( x, (STDLIB_MATH_CONTINUITY)continuity ), out );
STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_base_heaviside( x, continuity ), out );

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: passed
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: passed
  - task: lint_c_examples
    status: passed
  - task: lint_c_benchmarks
    status: passed
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
*/
function str2enum( ctype ) {
var v = ENUM[ ctype ];
return ( typeof v === 'number' ) ? v : null; // note: we include this guard to prevent walking the prototype chain
Copy link
Member

@Planeshifter Planeshifter May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential discrepancy: when supplying an invalid continuity string, we return null here, which causes an error to be thrown when invoking the native add-on. For the native add-on to behave like the JS implementation, I propose we pass down 3 instead of null here so that any other continuity string will be treated as the discontinuity case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Planeshifter, right! Do you think it's better to return -1 here instead of null so that it triggers the default block in the switch statement?

Also, for the native add-on, don't we build it in a way that matches the C implementation? For example, in the implementation of fibonacci, let's say in test.native.js, we removed the test where the input is not an integer. Still, having said that, your point stands. A user can pass in any number in heaviside for the continuity option other than 0, 1, 2, or 3 in C, and it should return NaN.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the above looks right, I will also update the implementation, tests and docs accordingly... Thanks for the feedback!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning -1 instead of null is fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1 sounds good to me, as well. It's preferable to 3 to not have a magic value in here that could drift from the enum in case we, for whatever reason, reorder the enum values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On it! Thank you for the confirmations!

Planeshifter
Planeshifter previously approved these changes May 14, 2025
Copy link
Member

@Planeshifter Planeshifter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me overall; one question.

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: passed
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@@ -214,6 +214,8 @@ The `continuity` parameter may be one of the following values:
- `STDLIB_BASE_HEAVISIDE_CONTINUITY_RIGHT_CONTINUOUS`: if `x == 0`, the function returns `1.0`.
- `STDLIB_BASE_HEAVISIDE_CONTINUITY_DISCONTINUOUS`: if `x == 0`, the function returns `NaN`.

If not provided a valid `continuity` parameter, the function returns `NaN` for `x == 0`, behaving like the discontinuous case.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this line look correct then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you confirm that C allows the continuity parameter to be a value other than an allowed enum value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kgryte It behaves like the discontinuous case if you pass a number (other than 0,1,2,3), say, -1 as the continuity parameter. If you pass something like FOO, it throws an error: use of undeclared identifier 'FOO'.

anandkaranubc and others added 2 commits May 16, 2025 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C Issue involves or relates to C. Feature Issue or pull request for adding a new feature. Math Issue or pull request specific to math functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants