Skip to content

Commit 7d671b1

Browse files
gururaj1512kgrytestdlib-botgunjjoshianandkaranubc
authored
feat: add math/base/special/bernoullif
PR-URL: #3037 Ref: #649 Signed-off-by: Gururaj Gurram <[email protected]> Signed-off-by: Gunj Joshi <[email protected]> Signed-off-by: Philipp Burckhardt <[email protected]> Co-authored-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]> Co-authored-by: Gunj Joshi <[email protected]> Co-authored-by: Karan Anand <[email protected]> Co-authored-by: Philipp Burckhardt <[email protected]> Reviewed-by: Karan Anand <[email protected]> Reviewed-by: Gunj Joshi <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent f07d876 commit 7d671b1

File tree

25 files changed

+2066
-0
lines changed

25 files changed

+2066
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# bernoullif
22+
23+
> Compute the nth [Bernoulli number][bernoulli-number] as a single-precision floating-point number.
24+
25+
<section class="intro">
26+
27+
<!-- /.intro -->
28+
29+
<section class="usage">
30+
31+
## Usage
32+
33+
```javascript
34+
var bernoullif = require( '@stdlib/math/base/special/bernoullif' );
35+
```
36+
37+
#### bernoullif( n )
38+
39+
Computes the nth [Bernoulli number][bernoulli-number] as a single-precision floating-point number.
40+
41+
```javascript
42+
var v = bernoullif( 0 );
43+
// returns 1.0
44+
45+
v = bernoullif( 1 );
46+
// returns 0.5
47+
48+
v = bernoullif( 2 );
49+
// returns ~0.167
50+
51+
v = bernoullif( 3 );
52+
// returns 0.0
53+
54+
v = bernoullif( 4 );
55+
// returns ~-0.033
56+
57+
v = bernoullif( 5 );
58+
// returns 0.0
59+
60+
v = bernoullif( 20 );
61+
// returns ~-529.124
62+
```
63+
64+
For even integers `n >= 66`, the function alternates between returning positive and negative infinity, as larger [Bernoulli numbers][bernoulli-number] cannot be safely represented in [single-precision floating-point format][ieee754]
65+
66+
```javascript
67+
var v = bernoullif( 66 );
68+
// returns Infinity
69+
70+
v = bernoullif( 68 );
71+
// returns -Infinity
72+
73+
v = bernoullif( 70 );
74+
// returns Infinity
75+
```
76+
77+
If not provided a nonnegative integer value, the function returns `NaN`.
78+
79+
```javascript
80+
var v = bernoullif( 3.14 );
81+
// returns NaN
82+
83+
v = bernoullif( -1 );
84+
// returns NaN
85+
```
86+
87+
If provided `NaN`, the function returns `NaN`.
88+
89+
```javascript
90+
var v = bernoullif( NaN );
91+
// returns NaN
92+
```
93+
94+
</section>
95+
96+
<!-- /.usage -->
97+
98+
<section class="notes">
99+
100+
</section>
101+
102+
<!-- /.notes -->
103+
104+
<section class="examples">
105+
106+
## Examples
107+
108+
<!-- eslint no-undef: "error" -->
109+
110+
```javascript
111+
var logEachMap = require( '@stdlib/console/log-each-map' );
112+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
113+
var bernoullif = require( '@stdlib/math/base/special/bernoullif' );
114+
115+
var x = discreteUniform( 100, 0, 70, {
116+
'dtype': 'int32'
117+
});
118+
119+
logEachMap( 'bernoullif(%d) = %0.4f', x, bernoullif );
120+
```
121+
122+
</section>
123+
124+
<!-- /.examples -->
125+
126+
<!-- C interface documentation. -->
127+
128+
* * *
129+
130+
<section class="c">
131+
132+
## C APIs
133+
134+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
135+
136+
<section class="intro">
137+
138+
</section>
139+
140+
<!-- /.intro -->
141+
142+
<!-- C usage documentation. -->
143+
144+
<section class="usage">
145+
146+
### Usage
147+
148+
```c
149+
#include "stdlib/math/base/special/bernoullif.h"
150+
```
151+
152+
#### stdlib_base_bernoullif( n )
153+
154+
Computes the nth [Bernoulli number][bernoulli-number] as a single-precision floating-point number.
155+
156+
```c
157+
float out = stdlib_base_bernoullif( 0 );
158+
// returns 1.0f
159+
160+
out = stdlib_base_bernoullif( 1 );
161+
// returns 0.5f
162+
```
163+
164+
The function accepts the following arguments:
165+
166+
- **n**: `[in] int32_t` input value.
167+
168+
```c
169+
float stdlib_base_bernoullif( const int32_t n );
170+
```
171+
172+
</section>
173+
174+
<!-- /.usage -->
175+
176+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
177+
178+
<section class="notes">
179+
180+
</section>
181+
182+
<!-- /.notes -->
183+
184+
<!-- C API usage examples. -->
185+
186+
<section class="examples">
187+
188+
### Examples
189+
190+
```c
191+
#include "stdlib/math/base/special/bernoullif.h"
192+
#include <stdio.h>
193+
#include <stdint.h>
194+
195+
int main( void ) {
196+
int32_t i;
197+
float v;
198+
199+
for ( i = 0; i < 70; i++ ) {
200+
v = stdlib_base_bernoullif( i );
201+
printf( "bernoullif(%d) = %f\n", i, v );
202+
}
203+
}
204+
```
205+
206+
</section>
207+
208+
<!-- /.examples -->
209+
210+
</section>
211+
212+
<!-- /.c -->
213+
214+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
215+
216+
<section class="related">
217+
218+
</section>
219+
220+
<!-- /.related -->
221+
222+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
223+
224+
<section class="links">
225+
226+
[bernoulli-number]: https://en.wikipedia.org/wiki/Bernoulli_number
227+
228+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
229+
230+
</section>
231+
232+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 discreteUniform = 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 bernoullif = 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 = discreteUniform( 100, 0, 100 );
38+
39+
b.tic();
40+
for ( i = 0; i < b.iterations; i++ ) {
41+
y = bernoullif( 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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 discreteUniform = 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 bernoullif = tryRequire( resolve( __dirname, './../lib/native.js' ) );
34+
var opts = {
35+
'skip': ( bernoullif 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 = discreteUniform( 100, 0, 100 );
47+
48+
b.tic();
49+
for ( i = 0; i < b.iterations; i++ ) {
50+
y = bernoullif( 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)