|
| 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 --> |
0 commit comments