-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add number/float16/base/assert/is-almost-same-value
#9664
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
kaushal-kumar-it
wants to merge
5
commits into
stdlib-js:develop
Choose a base branch
from
kaushal-kumar-it:feat/add-number-float16-base-assert-is-almost-same-value
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+739
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
291b426
feat: add number/float16/base/assert/is-almost-same-value
kaushal-kumar-it 9f477b4
chore: retrigger CI
kaushal-kumar-it 120e410
chore(build): fix requested changes
kaushal-kumar-it c5dfd8d
chore(build): fix requested changes
kaushal-kumar-it 8d1997d
chore(build): fixed readme
kaushal-kumar-it File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
lib/node_modules/@stdlib/number/float16/base/assert/is-almost-same-value/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 The Stdlib Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| --> | ||
|
|
||
| # isAlmostSameValue | ||
|
|
||
| > Test if two half-precision floating-point numbers are approximately the same value within a specified number of ULPs (units in the last place). | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ## Usage | ||
|
|
||
| ```javascript | ||
| var isAlmostSameValue = require( '@stdlib/number/float16/base/assert/is-almost-same-value' ); | ||
| ``` | ||
|
|
||
| #### isAlmostSameValue( a, b, maxULP ) | ||
|
|
||
| Tests if two half-precision floating-point numbers are approximately the same value within a specified number of ULPs (units in the last place). | ||
|
|
||
| ```javascript | ||
| var EPS = require( '@stdlib/constants/float16/eps' ); | ||
|
|
||
| var bool = isAlmostSameValue( 1.0, 1.0+EPS, 1 ); | ||
| // returns true | ||
|
|
||
| bool = isAlmostSameValue( 1.0, 1.0+EPS, 0 ); | ||
| // returns false | ||
| ``` | ||
|
|
||
| In contrast to the strict equality operator `===`, the function distinguishes between `+0` and `-0` and treats `NaNs` as the same value. | ||
|
|
||
| ```javascript | ||
| var bool = isAlmostSameValue( NaN, 1.0, 1 ); | ||
| // returns false | ||
|
|
||
| bool = isAlmostSameValue( 1.0, NaN, 1 ); | ||
| // returns false | ||
|
|
||
| bool = isAlmostSameValue( NaN, NaN, 1 ); | ||
| // returns true | ||
|
|
||
| bool = isAlmostSameValue( 0.0, -0.0, 0 ); | ||
| // returns false | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - The function implements the [SameValue Algorithm][ecma-262-same-value-algorithm] as specified in ECMAScript 5. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var EPS = require( '@stdlib/constants/float16/eps' ); | ||
| var isAlmostSameValue = require( '@stdlib/number/float16/base/assert/is-almost-same-value' ); | ||
|
|
||
| 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 | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
|
|
||
| <section class="related"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.related --> | ||
|
|
||
| <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="links"> | ||
|
|
||
| [ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12 | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
60 changes: 60 additions & 0 deletions
60
...de_modules/@stdlib/number/float16/base/assert/is-almost-same-value/benchmark/benchmark.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // MODULES // | ||
|
|
||
| var bench = require( '@stdlib/bench' ); | ||
| var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; | ||
| var pkg = require( './../package.json' ).name; | ||
| var isAlmostSameValue = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var values; | ||
| var bool; | ||
| var v; | ||
| var i; | ||
|
|
||
| values = [ | ||
| 5.0, | ||
| 3.14, | ||
| NaN, | ||
| -0.0, | ||
| 0.0, | ||
| -1.0 | ||
| ]; | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| v = values[ i%values.length ]; | ||
| bool = isAlmostSameValue( v, v, 1 ); | ||
| if ( typeof bool !== 'boolean' ) { | ||
| b.fail( 'should return a boolean' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !isBoolean( bool ) ) { | ||
| b.fail( 'should return a boolean' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
42 changes: 42 additions & 0 deletions
42
lib/node_modules/@stdlib/number/float16/base/assert/is-almost-same-value/docs/repl.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| {{alias}}( a, b, maxULP ) | ||
| Tests if two half-precision floating-point numbers are approximately the | ||
| same value within a specified number of ULPs (units in the last place). | ||
|
|
||
| The function differs from the `===` operator in that the function treats | ||
| `-0` and `+0` as distinct and `NaNs` as the same. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| a: number | ||
| First input value. | ||
|
|
||
| b: number | ||
| Second input value. | ||
|
|
||
| maxULP: number | ||
| Maximum allowed ULP difference. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool: boolean | ||
| Boolean indicating whether two half-precision floating-point numbers | ||
| are approximately the same value within a specified number of ULPs. | ||
|
|
||
| Examples | ||
| -------- | ||
| > var bool = {{alias}}( 1, 1+{{alias:@stdlib/constants/float16/eps}}, 1 ) | ||
| true | ||
| > bool = {{alias}}( 1+{{alias:@stdlib/constants/float16/eps}}, 1, 1 ) | ||
| true | ||
| > bool = {{alias}}( 1, 1+{{alias:@stdlib/constants/float16/eps}}*2, 1 ) | ||
| false | ||
| > bool = {{alias}}( 0.0, -0.0, 0 ) | ||
| false | ||
| > bool = {{alias}}( NaN, 1.0, 1 ) | ||
| false | ||
| > bool = {{alias}}( NaN, NaN, 0 ) | ||
| true | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
62 changes: 62 additions & 0 deletions
62
...ode_modules/@stdlib/number/float16/base/assert/is-almost-same-value/docs/types/index.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // TypeScript Version: 4.1 | ||
|
|
||
| /** | ||
| * Tests if two half-precision floating-point numbers are approximately the same value within a specified number of ULPs (units in the last place). | ||
| * | ||
| * ## Notes | ||
| * | ||
| * - The function differs from the `===` operator in that the function treats `-0` and `+0` as distinct and `NaNs` as the same. | ||
| * | ||
| * @param a - first input value | ||
| * @param b - second input value | ||
| * @param maxULP - maximum allowed ULP difference | ||
| * @returns boolean indicating whether two half-precision floating-point numbers are approximately the same value within a specified number of ULPs | ||
| * | ||
| * @example | ||
| * var EPS = require( '@stdlib/constants/float16/eps' ); | ||
| * | ||
| * 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 | ||
| */ | ||
| declare function isAlmostSameValue( a: number, b: number, maxULP: number ): boolean; | ||
|
|
||
|
|
||
| // EXPORTS // | ||
|
|
||
| export = isAlmostSameValue; |
71 changes: 71 additions & 0 deletions
71
lib/node_modules/@stdlib/number/float16/base/assert/is-almost-same-value/docs/types/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import isAlmostSameValue = require( './index' ); | ||
|
|
||
|
|
||
| // TESTS // | ||
|
|
||
| // The function returns a boolean... | ||
| { | ||
| isAlmostSameValue( 3.14, 3.14, 1 ); // $ExpectType boolean | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is not provided a first argument which is a number... | ||
| { | ||
| isAlmostSameValue( '5', 3.14, 1 ); // $ExpectError | ||
| isAlmostSameValue( true, 3.14, 1 ); // $ExpectError | ||
| isAlmostSameValue( false, 3.14, 1 ); // $ExpectError | ||
| isAlmostSameValue( null, 3.14, 1 ); // $ExpectError | ||
| isAlmostSameValue( void 0, 3.14, 1 ); // $ExpectError | ||
| isAlmostSameValue( [], 3.14, 1 ); // $ExpectError | ||
| isAlmostSameValue( {}, 3.14, 1 ); // $ExpectError | ||
| isAlmostSameValue( ( x: number ): number => x, 3.14, 1 ); // $ExpectError | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is not provided a second argument which is a number... | ||
| { | ||
| isAlmostSameValue( 3.14, '5', 1 ); // $ExpectError | ||
| isAlmostSameValue( 3.14, true, 1 ); // $ExpectError | ||
| isAlmostSameValue( 3.14, false, 1 ); // $ExpectError | ||
| isAlmostSameValue( 3.14, null, 1 ); // $ExpectError | ||
| isAlmostSameValue( 3.14, void 0, 1 ); // $ExpectError | ||
| isAlmostSameValue( 3.14, [], 1 ); // $ExpectError | ||
| isAlmostSameValue( 3.14, {}, 1 ); // $ExpectError | ||
| isAlmostSameValue( 3.14, ( x: number ): number => x, 1 ); // $ExpectError | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is not provided a third argument which is a number... | ||
| { | ||
| isAlmostSameValue( 3.14, 3.14, '5' ); // $ExpectError | ||
| isAlmostSameValue( 3.14, 3.14, true ); // $ExpectError | ||
| isAlmostSameValue( 3.14, 3.14, false ); // $ExpectError | ||
| isAlmostSameValue( 3.14, 3.14, null ); // $ExpectError | ||
| isAlmostSameValue( 3.14, 3.14, void 0 ); // $ExpectError | ||
| isAlmostSameValue( 3.14, 3.14, [] ); // $ExpectError | ||
| isAlmostSameValue( 3.14, 3.14, {} ); // $ExpectError | ||
| isAlmostSameValue( 3.14, 3.14, ( x: number ): number => x ); // $ExpectError | ||
| } | ||
|
|
||
| // The compiler throws an error if the function is provided an unsupported number of arguments... | ||
| { | ||
| isAlmostSameValue(); // $ExpectError | ||
| isAlmostSameValue( 3.14 ); // $ExpectError | ||
| isAlmostSameValue( 3.14, 3.14 ); // $ExpectError | ||
| isAlmostSameValue( 3.14, 3.14, 1, 2 ); // $ExpectError | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove the
console.login the markdown.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ref:- https://github.com/stdlib-js/stdlib/blob/develop/lib/node_modules/%40stdlib/number/float16/base/assert/is-almost-equal/README.md#examples