Skip to content

Commit b2f8db2

Browse files
anandkaranubckgrytestdlib-bot
authored
feat: add math/base/special/fibonacci-indexf
PR-URL: #6320 Ref: #649 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 98ed32a commit b2f8db2

File tree

28 files changed

+2400
-0
lines changed

28 files changed

+2400
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
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+
# Fibonacci Number Index
22+
23+
> Compute the [Fibonacci number][fibonacci-number] index of a single-precision floating-point number.
24+
25+
<section class="intro">
26+
27+
The [Fibonacci number][fibonacci-number] index is given by
28+
29+
<!-- <equation class="equation" label="eq:fibonacci_number_indexf" align="center" raw="n = \left \lfloor{\log_\varphi \biggl(F \cdot \sqrt{5} + \tfrac{1}{2}\biggr)}\right \rfloor" alt="Formula to compute the Fibonacci number index."> -->
30+
31+
```math
32+
n = \left \lfloor{\log_\varphi \biggl(F \cdot \sqrt{5} + \tfrac{1}{2}\biggr)}\right \rfloor
33+
```
34+
35+
<!-- </equation> -->
36+
37+
where `φ` is the [golden ratio][golden-ratio] and `F > 1`.
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<section class="usage">
44+
45+
## Usage
46+
47+
```javascript
48+
var fibonacciIndexf = require( '@stdlib/math/base/special/fibonacci-indexf' );
49+
```
50+
51+
#### fibonacciIndexf( F )
52+
53+
Computes the [Fibonacci number][fibonacci-number] index of a single-precision floating-point number.
54+
55+
```javascript
56+
var n = fibonacciIndexf( 2 );
57+
// returns 3
58+
59+
n = fibonacciIndexf( 3 );
60+
// returns 4
61+
62+
n = fibonacciIndexf( 5 );
63+
// returns 5
64+
```
65+
66+
If provided either a non-integer or `F_n <= 1`, the function returns `NaN`.
67+
68+
```javascript
69+
var n = fibonacciIndexf( -1 );
70+
// returns NaN
71+
72+
n = fibonacciIndexf( 3.14 );
73+
// returns NaN
74+
```
75+
76+
If provided `NaN`, the function returns `NaN`.
77+
78+
```javascript
79+
var n = fibonacciIndexf( NaN );
80+
// returns NaN
81+
```
82+
83+
</section>
84+
85+
<!-- /.usage -->
86+
87+
<section class="notes">
88+
89+
</section>
90+
91+
<!-- /.notes -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var fibonacciIndexf = require( '@stdlib/math/base/special/fibonacci-indexf' );
101+
102+
var F1;
103+
var F2;
104+
var FN;
105+
var n;
106+
var i;
107+
108+
F1 = 1;
109+
F2 = 1;
110+
for ( i = 3; i < 37; i++ ) {
111+
FN = F1 + F2;
112+
F1 = F2;
113+
F2 = FN;
114+
n = fibonacciIndexf( FN );
115+
console.log( 'n(%d) = %d', FN, n );
116+
}
117+
```
118+
119+
</section>
120+
121+
<!-- /.examples -->
122+
123+
<!-- C interface documentation. -->
124+
125+
* * *
126+
127+
<section class="c">
128+
129+
## C APIs
130+
131+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
132+
133+
<section class="intro">
134+
135+
</section>
136+
137+
<!-- /.intro -->
138+
139+
<!-- C usage documentation. -->
140+
141+
<section class="usage">
142+
143+
### Usage
144+
145+
```c
146+
#include "stdlib/math/base/special/fibonacci_indexf.h"
147+
```
148+
149+
#### stdlib_base_fibonacci_indexf( F )
150+
151+
Computes the [Fibonacci number][fibonacci-number] index of a single-precision floating-point number.
152+
153+
```c
154+
float out = stdlib_base_fibonacci_indexf( 2.0f );
155+
// returns 3.0f
156+
157+
out = stdlib_base_fibonacci_indexf( 3.0f );
158+
// returns 4.0f
159+
```
160+
161+
The function accepts the following arguments:
162+
163+
- **F**: `[in] float` input value.
164+
165+
```c
166+
float stdlib_base_fibonacci_indexf( const float F );
167+
```
168+
169+
</section>
170+
171+
<!-- /.usage -->
172+
173+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
174+
175+
<section class="notes">
176+
177+
</section>
178+
179+
<!-- /.notes -->
180+
181+
<!-- C API usage examples. -->
182+
183+
<section class="examples">
184+
185+
### Examples
186+
187+
```c
188+
#include "stdlib/math/base/special/fibonacci_indexf.h"
189+
#include <stdio.h>
190+
191+
int main( void ) {
192+
const float x[] = { 2.0f, 3.0f, 5.0f, 8.0f };
193+
194+
float y;
195+
int i;
196+
for ( i = 0; i < 4; i++ ) {
197+
y = stdlib_base_fibonacci_indexf( x[ i ] );
198+
printf( "fibonacci_indexf(%f) = %f\n", x[ i ], y );
199+
}
200+
}
201+
```
202+
203+
</section>
204+
205+
<!-- /.examples -->
206+
207+
</section>
208+
209+
<!-- /.c -->
210+
211+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
212+
213+
<section class="related">
214+
215+
</section>
216+
217+
<!-- /.related -->
218+
219+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
220+
221+
<section class="links">
222+
223+
[fibonacci-number]: https://en.wikipedia.org/wiki/Fibonacci_number
224+
225+
[golden-ratio]: https://en.wikipedia.org/wiki/Golden_ratio
226+
227+
<!-- <related-links> -->
228+
229+
<!-- </related-links> -->
230+
231+
</section>
232+
233+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 zeros = require( '@stdlib/array/base/zeros' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var fibonacci = require( '@stdlib/math/base/special/fibonacci' );
28+
var pkg = require( './../package.json' ).name;
29+
var fibonacciIndexf = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg, function benchmark( b ) {
35+
var FN;
36+
var x;
37+
var y;
38+
var i;
39+
40+
FN = zeros( 37 );
41+
for ( i = 3; i < 37; i++ ) {
42+
FN[ i ] = fibonacci( i );
43+
}
44+
45+
x = discreteUniform( 100, 3, 36 );
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
y = fibonacciIndexf( FN[ x[ i%x.length ] ] );
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 zeros = require( '@stdlib/array/base/zeros' );
27+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
28+
var fibonacci = require( '@stdlib/math/base/special/fibonacci' );
29+
var tryRequire = require( '@stdlib/utils/try-require' );
30+
var pkg = require( './../package.json' ).name;
31+
32+
33+
// VARIABLES //
34+
35+
var fibonacciIndexf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
36+
var opts = {
37+
'skip': ( fibonacciIndexf instanceof Error )
38+
};
39+
40+
41+
// MAIN //
42+
43+
bench( pkg+'::native', opts, function benchmark( b ) {
44+
var FN;
45+
var x;
46+
var y;
47+
var i;
48+
49+
FN = zeros( 37 );
50+
for ( i = 3; i < 37; i++ ) {
51+
FN[ i ] = fibonacci( i );
52+
}
53+
54+
x = discreteUniform( 100, 3, 36 );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
y = fibonacciIndexf( FN[ x[ i%x.length ] ] );
59+
if ( isnanf( y ) ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( isnanf( y ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
});

0 commit comments

Comments
 (0)