Skip to content

Commit 4e8074a

Browse files
feat: add number/float16/base/assert/is-positive-zero
PR-URL: #9643 Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]>
1 parent 20e08f9 commit 4e8074a

File tree

10 files changed

+528
-0
lines changed

10 files changed

+528
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# isPositiveZero
22+
23+
> Test if a half-precision floating-point number is positive zero.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isPositiveZero = require( '@stdlib/number/float16/base/assert/is-positive-zero' );
31+
```
32+
33+
#### isPositiveZero( x )
34+
35+
Tests if a half-precision floating-point number is positive zero.
36+
37+
```javascript
38+
var bool = isPositiveZero( 0.0 );
39+
// returns true
40+
41+
bool = isPositiveZero( -0.0 );
42+
// returns false
43+
```
44+
45+
</section>
46+
47+
<!-- /.usage -->
48+
49+
<section class="examples">
50+
51+
## Examples
52+
53+
<!-- eslint no-undef: "error" -->
54+
55+
```javascript
56+
var isPositiveZero = require( '@stdlib/number/float16/base/assert/is-positive-zero' );
57+
58+
var bool = isPositiveZero( 0.0 );
59+
// returns true
60+
61+
bool = isPositiveZero( -0.0 );
62+
// returns false
63+
64+
bool = isPositiveZero( 5.0 );
65+
// returns false
66+
67+
bool = isPositiveZero( -1.0 );
68+
// returns false
69+
70+
bool = isPositiveZero( NaN );
71+
// returns false
72+
```
73+
74+
</section>
75+
76+
<!-- /.examples -->
77+
78+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
79+
80+
<section class="related">
81+
82+
</section>
83+
84+
<!-- /.related -->
85+
86+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
87+
88+
<section class="links">
89+
90+
</section>
91+
92+
<!-- /.links -->
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var toFloat16 = require( '@stdlib/number/float64/base/to-float16' );
26+
var map = require( '@stdlib/array/base/map' );
27+
var naryFunction = require( '@stdlib/utils/nary-function' );
28+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
29+
var pkg = require( './../package.json' ).name;
30+
var isPositiveZero = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( pkg, function benchmark( b ) {
36+
var x;
37+
var y;
38+
var i;
39+
40+
x = map( uniform( 100, -5.0e4, 5.0e4 ), naryFunction( toFloat16, 1 ) );
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
y = isPositiveZero( x[ i%x.length ] );
45+
if ( typeof y !== 'boolean' ) {
46+
b.fail( 'should return a boolean' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isBoolean( y ) ) {
51+
b.fail( 'should return a boolean' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
{{alias}}( x )
3+
Tests if a half-precision floating-point number is positive zero.
4+
5+
Parameters
6+
----------
7+
x: number
8+
Value to test.
9+
10+
Returns
11+
-------
12+
bool: boolean
13+
Boolean indicating whether the value is positive zero.
14+
15+
Examples
16+
--------
17+
> var bool = {{alias}}( 0.0 )
18+
true
19+
> bool = {{alias}}( -0.0 )
20+
false
21+
22+
See Also
23+
--------
24+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Tests if a half-precision floating-point number is positive zero.
23+
*
24+
* @param x - value to test
25+
* @returns boolean indicating whether the value is positive zero
26+
*
27+
* @example
28+
* var bool = isPositiveZero( 0.0 );
29+
* // returns true
30+
*
31+
* @example
32+
* var bool = isPositiveZero( -0.0 );
33+
* // returns false
34+
*/
35+
declare function isPositiveZero( x: number ): boolean;
36+
37+
38+
// EXPORTS //
39+
40+
export = isPositiveZero;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import isPositiveZero = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a boolean...
25+
{
26+
isPositiveZero( 0 ); // $ExpectType boolean
27+
isPositiveZero( -0 ); // $ExpectType boolean
28+
isPositiveZero( 2 ); // $ExpectType boolean
29+
}
30+
31+
// The compiler throws an error if the function is provided a value other than a number...
32+
{
33+
isPositiveZero( true ); // $ExpectError
34+
isPositiveZero( false ); // $ExpectError
35+
isPositiveZero( null ); // $ExpectError
36+
isPositiveZero( undefined ); // $ExpectError
37+
isPositiveZero( [] ); // $ExpectError
38+
isPositiveZero( {} ); // $ExpectError
39+
isPositiveZero( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The compiler throws an error if the function is provided an unsupported number of arguments...
43+
{
44+
isPositiveZero(); // $ExpectError
45+
isPositiveZero( undefined, 123 ); // $ExpectError
46+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var isPositiveZero = require( './../lib' );
22+
23+
console.log( isPositiveZero( 0.0 ) );
24+
// => true
25+
26+
console.log( isPositiveZero( -0.0 ) );
27+
// => false
28+
29+
console.log( isPositiveZero( 5.0 ) );
30+
// => false
31+
32+
console.log( isPositiveZero( -1.0 ) );
33+
// => false
34+
35+
console.log( isPositiveZero( NaN ) );
36+
// => false
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Test if a half-precision floating-point number is positive zero.
23+
*
24+
* @module @stdlib/number/float16/base/assert/is-positive-zero
25+
*
26+
* @example
27+
* var isPositiveZero = require( '@stdlib/number/float16/base/assert/is-positive-zero' );
28+
*
29+
* var bool = isPositiveZero( 0.0 );
30+
* // returns true
31+
*
32+
* bool = isPositiveZero( -0.0 );
33+
* // returns false
34+
*/
35+
36+
// MODULES //
37+
38+
var main = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = main;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var PINF = require( '@stdlib/constants/float16/pinf' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Tests if a half-precision floating-point number is positive zero.
30+
*
31+
* @param {number} x - value to test
32+
* @returns {boolean} boolean indicating whether the value is positive zero
33+
*
34+
* @example
35+
* var bool = isPositiveZero( 0.0 );
36+
* // returns true
37+
*
38+
* @example
39+
* var bool = isPositiveZero( -0.0 );
40+
* // returns false
41+
*/
42+
function isPositiveZero( x ) {
43+
return (x === 0.0 && 1.0/x === PINF);
44+
}
45+
46+
47+
// EXPORTS //
48+
49+
module.exports = isPositiveZero;

0 commit comments

Comments
 (0)