Skip to content

Commit 8297683

Browse files
feat: add math/base/special/fibonaccif
PR-URL: #6257 Ref: #649 Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent a83525e commit 8297683

31 files changed

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

0 commit comments

Comments
 (0)