Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,125 @@
<!--

@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.

-->

# isSameValue

> Test if two half-precision floating-point numbers are the same value.

<section class="usage">

## Usage

```javascript
var isSameValue = require( '@stdlib/number/float16/base/assert/is-same-value' );
```

#### isSameValue( a, b )

Tests if two half-precision floating-point numbers `a` and `b` are the same value.

```javascript
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );

var bool = isSameValue( toFloat16( 3.14 ), toFloat16( 3.14 ) );
// returns true

bool = isSameValue( toFloat16( 5.0 ), toFloat16( 3.0 ) );
// returns false
```

In contrast to the strict equality operator `===`, the function distinguishes between `+0` and `-0` and treats `NaNs` as the same value.

<!-- eslint-disable no-compare-neg-zero, use-isnan, @cspell/spellchecker -->

```javascript
var bool = ( 0.0 === -0.0 );
// returns true

bool = isSameValue( 0.0, -0.0 );
// returns false

bool = isSameValue( -0.0, -0.0 );
// returns true

bool = ( NaN === NaN );
// returns false

bool = isSameValue( NaN, NaN );
// returns true
```

</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 toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
var isSameValue = require( '@stdlib/number/float16/base/assert/is-same-value' );

var bool = isSameValue( toFloat16( 3.14 ), toFloat16( 3.14 ) );
// returns true

bool = isSameValue( toFloat16( 0.0 ), toFloat16( 0.0 ) );
// returns true

bool = isSameValue( toFloat16( -0.0 ), toFloat16( 0.0 ) );
// returns false

bool = isSameValue( toFloat16( NaN ), toFloat16( NaN ) );
// returns 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 -->
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 isSameValue = 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 = isSameValue( v, v );
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();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

{{alias}}( a, b )
Tests if two half-precision floating-point numbers are the same value.

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.

Returns
-------
bool: boolean
Boolean indicating whether two half-precision floating-point numbers
are the same value.

Examples
--------
> var v1 = {{alias:@stdlib/number/float64/base/to-float16}}( -0.0 );
> var bool = {{alias}}( v1, v1 )
true
> var v2 = {{alias:@stdlib/number/float64/base/to-float16}}( 0.0 );
> bool = {{alias}}( v1, v2 )
false
> v1 = {{alias:@stdlib/number/float64/base/to-float16}}( NaN );
> bool = {{alias}}( v1, v1 )
true

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* @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 the same value.
*
* ## 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
* @returns boolean indicating whether two half-precision floating-point numbers are the same value
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var bool = isSameValue( toFloat16( 3.14 ), toFloat16( 3.14 ) );
* // returns true
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var bool = isSameValue( toFloat16( -0.0 ), toFloat16( -0.0 ) );
* // returns true
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var bool = isSameValue( toFloat16( -0.0 ), toFloat16( 0.0 ) );
* // returns false
*
* @example
* var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
*
* var bool = isSameValue( toFloat16( NaN ), toFloat16( NaN ) );
* // returns true
*/
declare function isSameValue( a: number, b: number ): boolean;


// EXPORTS //

export = isSameValue;
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* @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 isSameValue = require( './index' );


// TESTS //

// The function returns a boolean...
{
isSameValue( 3.14, 3.14 ); // $ExpectType boolean
}

// The compiler throws an error if the function is not provided a first argument which is a number...
{
isSameValue( '5', 3.14 ); // $ExpectError
isSameValue( true, 3.14 ); // $ExpectError
isSameValue( false, 3.14 ); // $ExpectError
isSameValue( null, 3.14 ); // $ExpectError
isSameValue( void 0, 3.14 ); // $ExpectError
isSameValue( [], 3.14 ); // $ExpectError
isSameValue( {}, 3.14 ); // $ExpectError
isSameValue( ( x: number ): number => x, 3.14 ); // $ExpectError
}

// The compiler throws an error if the function is not provided a second argument which is a number...
{
isSameValue( 3.14, '5' ); // $ExpectError
isSameValue( 3.14, true ); // $ExpectError
isSameValue( 3.14, false ); // $ExpectError
isSameValue( 3.14, null ); // $ExpectError
isSameValue( 3.14, void 0 ); // $ExpectError
isSameValue( 3.14, [] ); // $ExpectError
isSameValue( 3.14, {} ); // $ExpectError
isSameValue( 3.14, ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
isSameValue(); // $ExpectError
isSameValue( 3.14 ); // $ExpectError
isSameValue( 3.14, 3.14, 3.14 ); // $ExpectError
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @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';

var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
var isSameValue = require( './../lib' );

console.log( isSameValue( toFloat16( 3.14 ), toFloat16( 3.14 ) ) );
// => true

console.log( isSameValue( toFloat16( 0.0 ), toFloat16( 0.0 ) ) );
// => true

console.log( isSameValue( toFloat16( -0.0 ), toFloat16( 0.0 ) ) );
// => false

console.log( isSameValue( toFloat16( NaN ), toFloat16( NaN ) ) );
// => true
Loading
Loading