Skip to content

Conversation

@kaushal-kumar-it
Copy link
Contributor


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: passed
  • task: lint_javascript_src status: passed
  • task: lint_javascript_cli status: na
  • task: lint_javascript_examples status: passed
  • task: lint_javascript_tests status: passed
  • 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: na
  • task: lint_c_benchmarks status: na
  • task: lint_c_tests_fixtures status: na
  • task: lint_shell status: na
  • task: lint_typescript_declarations status: passed
  • task: lint_typescript_tests status: passed
  • task: lint_license_headers status: passed ---

Resolves none.

Description

What is the purpose of this pull request?

This pull request:

  • Adds number/float16/base/assert/is-almost-same-value

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

  • None.

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.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

@stdlib-js/reviewers

---
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: passed
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: passed
  - task: lint_javascript_tests
    status: passed
  - 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: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: passed
  - task: lint_typescript_tests
    status: passed
  - task: lint_license_headers
    status: passed
---
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Jan 10, 2026
@stdlib-bot
Copy link
Contributor

stdlib-bot commented Jan 10, 2026

Coverage Report

Package Statements Branches Functions Lines
number/float16/base/assert/is-almost-same-value $\color{green}154/154$
$\color{green}+0.00%$
$\color{green}11/11$
$\color{green}+0.00%$
$\color{green}1/1$
$\color{green}+0.00%$
$\color{green}154/154$
$\color{green}+0.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: 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: 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: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
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.

Implementation differs in behavior from float32 and float64.

Comment on lines 79 to 85
// Handle signed zeros (-0 !== +0 in SameValue):
if ( isPositiveZero( af ) && isNegativeZero( bf ) ) {
return false;
}
if ( isNegativeZero( af ) && isPositiveZero( bf ) ) {
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

This signed zero handling behaves differently from the float32 and float64 equivalents.

The float32 is-almost-same-value only distinguishes between +0 and -0 when maxULP === 0. When maxULP >= 1, it returns true for (0.0, -0.0) because they're 0 ULPs apart (per ulp-difference).

Currently this implementation always returns false for +0 vs -0 regardless of maxULP, which creates an inconsistency:

// float32 behavior:
isAlmostSameValue(0.0, -0.0, 0);  // false
isAlmostSameValue(0.0, -0.0, 1);  // true

// float16 behavior (this PR):
isAlmostSameValue(0.0, -0.0, 0);  // false
isAlmostSameValue(0.0, -0.0, 1);  // false (inconsistent!)

Check the float32 and float64 implementations for what is expected.

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 I noticed that the float32 implementation uses isSameValue as a dependency:
Should I:
Implement is-same-value for float16 first, then update this PR to match the float32 pattern exactly? This would maintain consistency across the codebase.
Keep the current self-contained implementation that uses the 1/x approch directly for signed zeros without depending on is-same-value?

Comment on lines 83 to 89
tape( 'the function returns `false` if signed zeros are provided as input values irrespective of the specified number of ULPs', function test( t ) {
t.strictEqual( isAlmostSameValue( 0.0, -0.0, 0 ), false, 'returns expected value' );
t.strictEqual( isAlmostSameValue( -0.0, 0.0, 0 ), false, 'returns expected value' );
t.strictEqual( isAlmostSameValue( 0.0, -0.0, 1 ), false, 'returns expected value' );
t.strictEqual( isAlmostSameValue( -0.0, 0.0, 1 ), false, 'returns expected value' );
t.end();
});
Copy link
Member

Choose a reason for hiding this comment

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

The test description and expectations for maxULP >= 1 don't match the float32 equivalent's behavior. The float32 version returns true when comparing +0 and -0 with maxULP >= 1, because they're 0 ULPs apart.

If aligning with float32, these tests should be:

Suggested change
tape( 'the function returns `false` if signed zeros are provided as input values irrespective of the specified number of ULPs', function test( t ) {
t.strictEqual( isAlmostSameValue( 0.0, -0.0, 0 ), false, 'returns expected value' );
t.strictEqual( isAlmostSameValue( -0.0, 0.0, 0 ), false, 'returns expected value' );
t.strictEqual( isAlmostSameValue( 0.0, -0.0, 1 ), false, 'returns expected value' );
t.strictEqual( isAlmostSameValue( -0.0, 0.0, 1 ), false, 'returns expected value' );
t.end();
});
tape( 'the function distinguishes between signed zeros when the specified number of ULPs is zero', function test( t ) {
t.strictEqual( isAlmostSameValue( 0.0, -0.0, 0 ), false, 'returns expected value' );
t.strictEqual( isAlmostSameValue( -0.0, 0.0, 0 ), false, 'returns expected value' );
t.strictEqual( isAlmostSameValue( 0.0, -0.0, 1 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( -0.0, 0.0, 1 ), true, 'returns expected value' );
t.end();
});

@Planeshifter Planeshifter added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Jan 10, 2026
---
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: 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: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Jan 10, 2026
---
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: passed
  - 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: 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: 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: passed
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---
Comment on lines +23 to +25
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var f16 = require( '@stdlib/number/float64/base/to-float16' );
var ulpdiff = require( '@stdlib/number/float16/base/ulp-difference' );
Copy link
Member

@Neerajpathak07 Neerajpathak07 Jan 12, 2026

Choose a reason for hiding this comment

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

Suggested change
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var f16 = require( '@stdlib/number/float64/base/to-float16' );
var ulpdiff = require( '@stdlib/number/float16/base/ulp-difference' );
var isnan = require( '@stdlib/number/float16/base/assert/is-nan' );
var isSameValue = require( '@stdlib/number/float16/base/assert/is-same-value' );
var ulpdiff = require( '@stdlib/number/float16/base/ulp-difference' );

Handling -+0 and isnan should come form isSameValue
Should be resolved once #9677 is merged.

Comment on lines +70 to +87
var af;
var bf;

af = f16( a );
bf = f16( b );

// Check for NaN (NaN is considered equal to NaN in SameValue):
if ( isnan( af ) && isnan( bf ) ) {
return true;
}
// Only distinguish signed zeros when maxULP is 0:
if ( maxULP === 0 && af === 0.0 && bf === 0.0 ) {
return ( 1.0 / af === 1.0 / bf );
}
// Check if exactly equal:
if ( af === bf ) {
return true;
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
var af;
var bf;
af = f16( a );
bf = f16( b );
// Check for NaN (NaN is considered equal to NaN in SameValue):
if ( isnan( af ) && isnan( bf ) ) {
return true;
}
// Only distinguish signed zeros when maxULP is 0:
if ( maxULP === 0 && af === 0.0 && bf === 0.0 ) {
return ( 1.0 / af === 1.0 / bf );
}
// Check if exactly equal:
if ( af === bf ) {
return true;
}
if ( isnan( a ) || isnan( b ) || maxULP === 0 ) {
return isSameValue( a, b );
}

Better to use the explicit float16 version of isnan rather than converting to half-precision

Comment on lines +87 to +114
var bool = isAlmostSameValue( 1.0, 1.0+EPS, 1 );
console.log( bool );
// => true

bool = isAlmostSameValue( 1.0+EPS, 1.0, 1 );
console.log( bool );
// => true

bool = isAlmostSameValue( 1.0, 1.0+EPS+EPS, 1 );
console.log( bool );
// => false

bool = isAlmostSameValue( 1.0, 1.0+EPS, 0 );
console.log( bool );
// => false

bool = isAlmostSameValue( -0.0, 0.0, 0 );
console.log( bool );
// => false

bool = isAlmostSameValue( 1.0, NaN, 1 );
console.log( bool );
// => false

bool = isAlmostSameValue( NaN, NaN, 1 );
console.log( bool );
// => true
```
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
var bool = isAlmostSameValue( 1.0, 1.0+EPS, 1 );
console.log( bool );
// => true
bool = isAlmostSameValue( 1.0+EPS, 1.0, 1 );
console.log( bool );
// => true
bool = isAlmostSameValue( 1.0, 1.0+EPS+EPS, 1 );
console.log( bool );
// => false
bool = isAlmostSameValue( 1.0, 1.0+EPS, 0 );
console.log( bool );
// => false
bool = isAlmostSameValue( -0.0, 0.0, 0 );
console.log( bool );
// => false
bool = isAlmostSameValue( 1.0, NaN, 1 );
console.log( bool );
// => false
bool = isAlmostSameValue( NaN, NaN, 1 );
console.log( bool );
// => true
```
var bool = isAlmostSameValue( 1.0, 1.0+EPS, 1 );
// returns true
bool = isAlmostSameValue( 1.0+EPS, 1.0, 1 );
// returns true
bool = isAlmostSameValue( 1.0, 1.0+EPS+EPS, 1 );
// returns false
bool = isAlmostSameValue( 1.0, 1.0+EPS, 0 );
// returns false
bool = isAlmostSameValue( -0.0, 0.0, 0 );
// returns false
bool = isAlmostSameValue( 1.0, NaN, 1 );
// returns false
bool = isAlmostSameValue( NaN, NaN, 1 );
// returns true

Let's remove the console.log in the markdown.

Copy link
Member

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Changes Pull request which needs changes before being merged. Needs Review A pull request which needs code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants