Skip to content

Commit aa5c7a8

Browse files
gururaj1512stdlib-botgunjjoshiPlaneshifter
authored
feat: add math/base/special/sqrtpif
PR-URL: #3116 Ref: #649 --------- Signed-off-by: Gunj Joshi <gunjjoshi8372@gmail.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com> Co-authored-by: Gunj Joshi <gunjjoshi8372@gmail.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Gunj Joshi <gunjjoshi8372@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent b8c41df commit aa5c7a8

34 files changed

+2300
-0
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# sqrtpif
22+
23+
> Compute the principal [square root][@stdlib/math/base/special/sqrt] of the product of π and a positive single-precision floating-point number.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var sqrtpif = require( '@stdlib/math/base/special/sqrtpif' );
31+
```
32+
33+
#### sqrtpif( x )
34+
35+
Computes the principal [square root][@stdlib/math/base/special/sqrt] of the product of π and a positive single-precision floating-point number.
36+
37+
```javascript
38+
var v = sqrtpif( 4.0 );
39+
// returns ~3.5449
40+
41+
v = sqrtpif( 10.0 );
42+
// returns ~5.60499
43+
44+
v = sqrtpif( 0.0 );
45+
// returns 0.0
46+
47+
v = sqrtpif( NaN );
48+
// returns NaN
49+
```
50+
51+
For negative numbers, the principal [square root][@stdlib/math/base/special/sqrt] is **not** defined.
52+
53+
```javascript
54+
var v = sqrtpif( -4.0 );
55+
// returns NaN
56+
```
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<section class="examples">
63+
64+
## Examples
65+
66+
<!-- eslint no-undef: "error" -->
67+
68+
```javascript
69+
var randu = require( '@stdlib/random/array/discrete-uniform' );
70+
var sqrtpif = require( '@stdlib/math/base/special/sqrtpif' );
71+
72+
var x = randu( 100, 0.0, 100.0 );
73+
74+
var i;
75+
for ( i = 0; i < 100; i++ ) {
76+
console.log( 'sqrtpif(%d) = %d', x[ i ], sqrtpif( x[ i ] ) );
77+
}
78+
```
79+
80+
</section>
81+
82+
<!-- /.examples -->
83+
84+
<!-- C interface documentation. -->
85+
86+
* * *
87+
88+
<section class="c">
89+
90+
## C APIs
91+
92+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
93+
94+
<section class="intro">
95+
96+
</section>
97+
98+
<!-- /.intro -->
99+
100+
<!-- C usage documentation. -->
101+
102+
<section class="usage">
103+
104+
### Usage
105+
106+
```c
107+
#include "stdlib/math/base/special/sqrtpif.h"
108+
```
109+
110+
#### stdlib_base_sqrtpif( x )
111+
112+
Computes the principal [square root][@stdlib/math/base/special/sqrt] of the product of π and a positive single-precision floating-point number.
113+
114+
```c
115+
float x = stdlib_base_sqrtpif( 4.0f );
116+
// returns ~3.5449f
117+
118+
x = stdlib_base_sqrtpif( 10.0f );
119+
// returns ~5.60499f
120+
```
121+
122+
The function accepts the following arguments:
123+
124+
- **x**: `[in] float` input value.
125+
126+
```c
127+
float stdlib_base_sqrtpif( const float x );
128+
```
129+
130+
</section>
131+
132+
<!-- /.usage -->
133+
134+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
135+
136+
<section class="notes">
137+
138+
</section>
139+
140+
<!-- /.notes -->
141+
142+
<!-- C API usage examples. -->
143+
144+
<section class="examples">
145+
146+
### Examples
147+
148+
```c
149+
#include "stdlib/math/base/special/sqrtpif.h"
150+
#include <stdlib.h>
151+
#include <stdio.h>
152+
153+
int main( void ) {
154+
const float x[] = { 4.0f, 10.0f, 3.14f, -3.14f, 0.0f, 0.0f / 0.0f };
155+
156+
float v;
157+
int i;
158+
for ( i = 0; i < 6; i++ ) {
159+
v = stdlib_base_sqrtpif( x[ i ] );
160+
printf( "sqrtpif(%f) = %f", x[ i ], v );
161+
}
162+
}
163+
```
164+
165+
</section>
166+
167+
<!-- /.examples -->
168+
169+
</section>
170+
171+
<!-- /.c -->
172+
173+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
174+
175+
<section class="related">
176+
177+
</section>
178+
179+
<!-- /.related -->
180+
181+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
182+
183+
<section class="links">
184+
185+
<!-- <related-links> -->
186+
187+
[@stdlib/math/base/special/sqrt]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/sqrt
188+
189+
<!-- </related-links> -->
190+
191+
</section>
192+
193+
<!-- /.links -->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 randu = require( '@stdlib/random/array/discrete-uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pkg = require( './../package.json' ).name;
27+
var sqrtpif = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var x;
34+
var y;
35+
var i;
36+
37+
x = randu( 100, 0.0, 100000.0 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = sqrtpif( x[ i % x.length ] );
42+
if ( isnanf( y ) ) {
43+
b.fail( 'should not return NaN' );
44+
}
45+
}
46+
b.toc();
47+
if ( isnanf( y ) ) {
48+
b.fail( 'should not return NaN' );
49+
}
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 resolve = require( 'path' ).resolve;
24+
var bench = require( '@stdlib/bench' );
25+
var randu = require( '@stdlib/random/array/discrete-uniform' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var tryRequire = require( '@stdlib/utils/try-require' );
28+
var pkg = require( './../package.json' ).name;
29+
30+
31+
// VARIABLES //
32+
33+
var sqrtpif = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( sqrtpif instanceof Error )
36+
};
37+
38+
39+
// MAIN //
40+
41+
bench( pkg+'::native', opts, function benchmark( b ) {
42+
var x;
43+
var y;
44+
var i;
45+
46+
x = randu( 100, 0.0, 100000.0 );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
y = sqrtpif( x[ i % x.length ] );
51+
if ( isnanf( y ) ) {
52+
b.fail( 'should not return NaN' );
53+
}
54+
}
55+
b.toc();
56+
if ( isnanf( y ) ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
b.pass( 'benchmark finished' );
60+
b.end();
61+
});

0 commit comments

Comments
 (0)