Skip to content

Commit 8c0fc7e

Browse files
gunjjoshikgryte
andauthored
feat: add math/base/special/rcbrtf
PR-URL: #2185 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: GUNJ JOSHI <[email protected]>
1 parent 3f1caa3 commit 8c0fc7e

40 files changed

+2731
-0
lines changed
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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+
# rcbrtf
22+
23+
> Compute the reciprocal of the principal [cube root][cube-root] of a single-precision floating-point number.
24+
25+
<section class="intro">
26+
27+
The reciprocal of the principal [cube root][cube-root] is defined as
28+
29+
<!-- <equation class="equation" label="eq:reciprocal_cube_root" align="center" raw="\operatorname{rcbrt}(x)=\frac{1}{\sqrt[3]{x}}" alt="Reciprocal cube root"> -->
30+
31+
```math
32+
\mathop{\mathrm{rcbrt}}(x)=\frac{1}{\sqrt[3]{x}}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\operatorname{rcbrt}(x)=\frac{1}{\sqrt[3]{x}}" data-equation="eq:reciprocal_cube_root">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@b569df0e375cb7d535781320bf5e2299a0fbff25/lib/node_modules/@stdlib/math/base/special/rcbrt/docs/img/equation_reciprocal_cube_root.svg" alt="Reciprocal cube root">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var rcbrtf = require( '@stdlib/math/base/special/rcbrtf' );
52+
```
53+
54+
#### rcbrtf( x )
55+
56+
Computes the reciprocal cube root of a single-precision floating-point number.
57+
58+
```javascript
59+
var v = rcbrtf( 1.0 );
60+
// returns 1.0
61+
62+
v = rcbrtf( 8.0 );
63+
// returns 0.5
64+
65+
v = rcbrtf( 1000.0 );
66+
// returns ~0.1
67+
68+
v = rcbrtf( 0.0 );
69+
// returns Infinity
70+
71+
v = rcbrtf( NaN );
72+
// returns NaN
73+
74+
v = rcbrtf( Infinity );
75+
// returns 0.0
76+
```
77+
78+
</section>
79+
80+
<!-- /.usage -->
81+
82+
<section class="examples">
83+
84+
## Examples
85+
86+
<!-- eslint no-undef: "error" -->
87+
88+
```javascript
89+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
90+
var rcbrtf = require( '@stdlib/math/base/special/rcbrtf' );
91+
92+
var x;
93+
var i;
94+
for ( i = 0; i < 100; i++ ) {
95+
x = discreteUniform( 0.0, 100.0 );
96+
console.log( 'rcbrtf(%d) = %d', x, rcbrtf( x ) );
97+
}
98+
```
99+
100+
</section>
101+
102+
<!-- /.examples -->
103+
104+
<!-- C interface documentation. -->
105+
106+
* * *
107+
108+
<section class="c">
109+
110+
## C APIs
111+
112+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
113+
114+
<section class="intro">
115+
116+
</section>
117+
118+
<!-- /.intro -->
119+
120+
<!-- C usage documentation. -->
121+
122+
<section class="usage">
123+
124+
### Usage
125+
126+
```c
127+
#include "stdlib/math/base/special/rcbrtf.h"
128+
```
129+
130+
#### stdlib_base_rcbrtf( x )
131+
132+
Computes the reciprocal [cube root][cube-root] of a single-precision floating-point number.
133+
134+
```c
135+
float y = stdlib_base_rcbrtf( 8.0f );
136+
// returns 0.5f
137+
```
138+
139+
The function accepts the following arguments:
140+
141+
- **x**: `[in] float` input value.
142+
143+
```c
144+
float stdlib_base_rcbrtf( const float x );
145+
```
146+
147+
</section>
148+
149+
<!-- /.usage -->
150+
151+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="notes">
154+
155+
</section>
156+
157+
<!-- /.notes -->
158+
159+
<!-- C API usage examples. -->
160+
161+
<section class="examples">
162+
163+
### Examples
164+
165+
```c
166+
#include "stdlib/math/base/special/rcbrtf.h"
167+
#include <stdio.h>
168+
169+
int main( void ) {
170+
const float x[] = { 3.14f, 9.0f, 0.0f, 0.0f / 0.0f };
171+
172+
float y;
173+
int i;
174+
for ( i = 0; i < 4; i++ ) {
175+
y = stdlib_base_rcbrtf( x[ i ] );
176+
printf( "rcbrt(%f) = %f\n", x[ i ], y );
177+
}
178+
}
179+
```
180+
181+
</section>
182+
183+
<!-- /.examples -->
184+
185+
</section>
186+
187+
<!-- /.c -->
188+
189+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
190+
191+
<section class="related">
192+
193+
</section>
194+
195+
<!-- /.related -->
196+
197+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
198+
199+
<section class="links">
200+
201+
[cube-root]: https://en.wikipedia.org/wiki/Cube_root
202+
203+
<!-- <related-links> -->
204+
205+
<!-- </related-links> -->
206+
207+
</section>
208+
209+
<!-- /.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 rcbrtf = 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 = rcbrtf( 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 rcbrtf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( rcbrtf 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 = rcbrtf( 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)