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 19 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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'.


```c
double stdlib_base_heaviside( const double x, const STDLIB_BASE_HEAVISIDE_CONTINUITY continuity );
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var ENUM = enumeration();
*/
function str2enum( ctype ) {
var v = ENUM[ ctype ];
return ( typeof v === 'number' ) ? v : null; // note: we include this guard to prevent walking the prototype chain
return ( typeof v === 'number' ) ? v : -1; // note: we include this guard to prevent walking the prototype chain
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ tape( 'if the `continuity` option is `right-continuous`, the function returns `1
t.end();
});

tape( 'if the `continuity` option is not valid, the function returns `NaN` if provided `+-0`', opts, function test( t ) {
var v;

v = heaviside( -0.0, 'foo' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

v = heaviside( +0.0, 'bar' );
t.strictEqual( isnan( v ), true, 'returns expected value' );

t.end();
});

tape( 'the function returns `NaN` if provided `NaN`', opts, function test( t ) {
var v = heaviside( NaN, 'left-continuous' );
t.strictEqual( isnan( v ), true, 'returns expected value' );
Expand Down