Skip to content

Commit 69f50a7

Browse files
committed
feat: add math/base/special/sqrtpif
1 parent b8259d3 commit 69f50a7

34 files changed

+2294
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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 discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
70+
var sqrtpif = require( '@stdlib/math/base/special/sqrtpif' );
71+
72+
var x;
73+
var i;
74+
75+
for ( i = 0; i < 100; i++ ) {
76+
x = discreteUniform( 0, 100 );
77+
console.log( 'sqrtpif(%d) = %d', x, sqrtpif( x ) );
78+
}
79+
```
80+
81+
</section>
82+
83+
<!-- /.examples -->
84+
85+
<!-- C interface documentation. -->
86+
87+
* * *
88+
89+
<section class="c">
90+
91+
## C APIs
92+
93+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
94+
95+
<section class="intro">
96+
97+
</section>
98+
99+
<!-- /.intro -->
100+
101+
<!-- C usage documentation. -->
102+
103+
<section class="usage">
104+
105+
### Usage
106+
107+
```c
108+
#include "stdlib/math/base/special/sqrtpif.h"
109+
```
110+
111+
#### stdlib_base_sqrtpif( x )
112+
113+
Computes the principal [square root][@stdlib/math/base/special/sqrt] of the product of π and a positive single-precision floating-point number.
114+
115+
```c
116+
float x = stdlib_base_sqrtpif( 4.0 );
117+
// returns ~3.5449
118+
119+
x = stdlib_base_sqrtpif( 10.0 );
120+
// returns ~5.60499
121+
```
122+
123+
The function accepts the following arguments:
124+
125+
- **x**: `[in] float` input value.
126+
127+
```c
128+
float stdlib_base_sqrtpif( const float x );
129+
```
130+
131+
</section>
132+
133+
<!-- /.usage -->
134+
135+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="notes">
138+
139+
</section>
140+
141+
<!-- /.notes -->
142+
143+
<!-- C API usage examples. -->
144+
145+
<section class="examples">
146+
147+
### Examples
148+
149+
```c
150+
#include "stdlib/math/base/special/sqrtpif.h"
151+
#include <stdlib.h>
152+
#include <stdio.h>
153+
154+
int main( void ) {
155+
float x;
156+
float v;
157+
int i;
158+
159+
for ( i = 0; i < 100; i++ ) {
160+
x = ( (float)rand() / (float)RAND_MAX ) * 100.0;
161+
v = stdlib_base_sqrtpif( x );
162+
printf( "sqrtpif(%f) = %f\n", x, v );
163+
}
164+
}
165+
```
166+
167+
</section>
168+
169+
<!-- /.examples -->
170+
171+
</section>
172+
173+
<!-- /.c -->
174+
175+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
176+
177+
<section class="related">
178+
179+
* * *
180+
181+
## See Also
182+
183+
- <span class="package-name">[`@stdlib/math/base/special/sqrt`][@stdlib/math/base/special/sqrt]</span><span class="delimiter">: </span><span class="description">compute the principal square root of a double-precision floating-point number.</span>
184+
185+
</section>
186+
187+
<!-- /.related -->
188+
189+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
190+
191+
<section class="links">
192+
193+
[@stdlib/math/base/special/sqrt]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/sqrt
194+
195+
<!-- <related-links> -->
196+
197+
<!-- </related-links> -->
198+
199+
</section>
200+
201+
<!-- /.links -->
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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/base/randu' );
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+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
x = ( randu()*100000.0 ) - 0.0;
40+
y = sqrtpif( x );
41+
if ( isnanf( y ) ) {
42+
b.fail( 'should not return NaN' );
43+
}
44+
}
45+
b.toc();
46+
if ( isnanf( y ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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/base/randu' );
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+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
x = ( randu()*100000.0 ) - 0.0;
49+
y = sqrtpif( x );
50+
if ( isnanf( y ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
}
54+
b.toc();
55+
if ( isnanf( y ) ) {
56+
b.fail( 'should not return NaN' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});

0 commit comments

Comments
 (0)