Skip to content

Commit f5b46bb

Browse files
steff456kgryte
andauthored
Add support for computing the square root of the product of pi and a positive number (stdlib-js#507)
* Add sqrtpi package * Apply suggestions from code review * Update description * Update description. Co-authored-by: Athan <kgryte@gmail.com>
1 parent 4659d20 commit f5b46bb

File tree

20 files changed

+887
-0
lines changed

20 files changed

+887
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
# sqrtpi
22+
23+
> Compute the principal [square root][@stdlib/math/base/special/sqrt] of the product of 𝛑 and a positive number.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var sqrtpi = require( '@stdlib/math/base/special/sqrtpi' );
31+
```
32+
33+
#### sqrtpi( x )
34+
35+
Computes the principal [square root][@stdlib/math/base/special/sqrt] of the product of 𝛑 and a positive number.
36+
37+
```javascript
38+
var v = sqrtpi( 4.0 );
39+
// returns ~3.5449
40+
41+
v = sqrtpi( 10.0 );
42+
// returns ~5.6049
43+
44+
v = sqrtpi( 0.0 );
45+
// returns 0.0
46+
47+
v = sqrtpi( 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 = sqrtpi( -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/base/randu' );
70+
var round = require( '@stdlib/math/base/special/round' );
71+
var sqrtpi = require( '@stdlib/math/base/special/sqrtpi' );
72+
73+
var x;
74+
var i;
75+
76+
for ( i = 0; i < 100; i++ ) {
77+
x = round( randu() * 100.0 );
78+
console.log( 'sqrtpi(%d) = %d', x, sqrtpi( x ) );
79+
}
80+
```
81+
82+
</section>
83+
84+
<!-- /.examples -->
85+
86+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
87+
88+
<section class="related">
89+
90+
* * *
91+
92+
## See Also
93+
94+
- <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>
95+
- <span class="package-name">[`@stdlib/math/base/special/rsqrt`][@stdlib/math/base/special/rsqrt]</span><span class="delimiter">: </span><span class="description">compute the reciprocal square root of a double-precision floating-point number.</span>
96+
97+
</section>
98+
99+
<!-- /.related -->
100+
101+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
102+
103+
<section class="links">
104+
105+
[@stdlib/math/base/special/sqrt]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/sqrt
106+
107+
<!-- <related-links> -->
108+
109+
[@stdlib/math/base/special/rsqrt]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/rsqrt
110+
111+
<!-- </related-links> -->
112+
113+
</section>
114+
115+
<!-- /.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) 2022 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pkg = require( './../package.json' ).name;
27+
var sqrtpi = 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 = sqrtpi( x );
41+
if ( isnan( y ) ) {
42+
b.fail( 'should not return NaN' );
43+
}
44+
}
45+
b.toc();
46+
if ( isnan( y ) ) {
47+
b.fail( 'should not return NaN' );
48+
}
49+
b.pass( 'benchmark finished' );
50+
b.end();
51+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( x )
3+
Computes the principal square root of the product of 𝛑 and a positive
4+
number.
5+
6+
For `x < 0`, the square root is not defined.
7+
8+
Parameters
9+
----------
10+
x: number
11+
Input value.
12+
13+
Returns
14+
-------
15+
y: number
16+
Principal square root of the product of 𝛑 and the input value.
17+
18+
Examples
19+
--------
20+
> var y = {{alias}}( 4.0 )
21+
~3.5449
22+
> y = {{alias}}( 10.0 )
23+
~5.6049
24+
> y = {{alias}}( 0.0 )
25+
0.0
26+
> y = {{alias}}( -4.0 )
27+
NaN
28+
> y = {{alias}}( NaN )
29+
NaN
30+
31+
See Also
32+
--------
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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: 2.0
20+
21+
/**
22+
* Computes the principal square root of the product of pi and a positive number.
23+
*
24+
* ## Notes
25+
*
26+
* - For `x < 0`, the principal square root is not defined.
27+
*
28+
* @param x - input value
29+
* @returns results
30+
*
31+
* @example
32+
* var v = sqrtpi( 4.0 );
33+
* // returns ~3.5449
34+
*
35+
* @example
36+
* var v = sqrtpi( 10.0 );
37+
* // returns ~5.6049
38+
*
39+
* @example
40+
* var v = sqrtpi( 0.0 );
41+
* // returns 0.0
42+
*
43+
* @example
44+
* var v = sqrtpi( -4.0 );
45+
* // returns NaN
46+
*
47+
* @example
48+
* var v = sqrtpi( NaN );
49+
* // returns NaN
50+
*/
51+
declare function sqrtpi( x: number ): number;
52+
53+
54+
// EXPORTS //
55+
56+
export = sqrtpi;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 sqrtpi = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
sqrtpi( 2 ); // $ExpectType number
27+
}
28+
29+
// The function does not compile if provided a value other than a number...
30+
{
31+
sqrtpi( true ); // $ExpectError
32+
sqrtpi( false ); // $ExpectError
33+
sqrtpi( null ); // $ExpectError
34+
sqrtpi( undefined ); // $ExpectError
35+
sqrtpi( '5' ); // $ExpectError
36+
sqrtpi( [] ); // $ExpectError
37+
sqrtpi( {} ); // $ExpectError
38+
sqrtpi( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The function does not compile if provided insufficient arguments...
42+
{
43+
sqrtpi(); // $ExpectError
44+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 randu = require( '@stdlib/random/base/randu' );
22+
var round = require( '@stdlib/math/base/special/round' );
23+
var sqrtpi = require( './../lib' );
24+
25+
var x;
26+
var i;
27+
28+
for ( i = 0; i < 100; i++ ) {
29+
x = round( randu() * 100.0 );
30+
console.log( 'sqrtpi(%d) = %d', x, sqrtpi( x ) );
31+
}
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) 2022 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+
* Compute the principal square root of the product of 𝛑 and a positive number.
23+
*
24+
* @module @stdlib/math/base/special/sqrtpi
25+
*
26+
* @example
27+
* var sqrtpi = require( '@stdlib/math/base/special/sqrtpi' );
28+
*
29+
* var v = sqrtpi( 4.0 );
30+
* // returns ~3.5449
31+
*
32+
* v = sqrtpi( 10.0 );
33+
* // returns ~5.6049
34+
*
35+
* v = sqrtpi( 0.0 );
36+
* // returns 0.0
37+
*
38+
* v = sqrtpi( -4.0 );
39+
* // returns NaN
40+
*
41+
* v = sqrtpi( NaN );
42+
* // returns NaN
43+
*/
44+
45+
// MODULES //
46+
47+
var main = require( './main.js' );
48+
49+
50+
// EXPORTS //
51+
52+
module.exports = main;

0 commit comments

Comments
 (0)