Skip to content

Commit e6b6ffb

Browse files
authored
feat: add constants/float32/max-safe-fibonacci
PR-URL: #2904 Reviewed-by: Athan Reines <[email protected]>
1 parent a12d0f0 commit e6b6ffb

File tree

10 files changed

+517
-0
lines changed

10 files changed

+517
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
# FLOAT32_MAX_SAFE_FIBONACCI
22+
23+
> Maximum safe [Fibonacci number][fibonacci-number] when stored in [single-precision floating-point][ieee754] format.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
<!-- eslint-disable id-length -->
30+
31+
```javascript
32+
var FLOAT32_MAX_SAFE_FIBONACCI = require( '@stdlib/constants/float32/max-safe-fibonacci' );
33+
```
34+
35+
#### FLOAT32_MAX_SAFE_FIBONACCI
36+
37+
The maximum [safe][safe-integers] [Fibonacci number][fibonacci-number] when stored in [single-precision floating-point][ieee754] format.
38+
39+
<!-- eslint-disable id-length -->
40+
41+
```javascript
42+
var bool = ( FLOAT32_MAX_SAFE_FIBONACCI === 14930352 );
43+
// returns true
44+
```
45+
46+
</section>
47+
48+
<!-- /.usage -->
49+
50+
<section class="examples">
51+
52+
## Examples
53+
54+
<!-- eslint-disable id-length -->
55+
56+
<!-- eslint no-undef: "error" -->
57+
58+
```javascript
59+
var FLOAT32_MAX_SAFE_FIBONACCI = require( '@stdlib/constants/float32/max-safe-fibonacci' );
60+
61+
var v;
62+
var i;
63+
64+
function fibonacci( n ) {
65+
var a;
66+
var b;
67+
var c;
68+
var i;
69+
70+
a = 1;
71+
b = 1;
72+
for ( i = 3; i <= n; i++ ) {
73+
c = a + b;
74+
a = b;
75+
b = c;
76+
}
77+
return b;
78+
}
79+
80+
for ( i = 0; i < 50; i++ ) {
81+
v = fibonacci( i );
82+
if ( v > FLOAT32_MAX_SAFE_FIBONACCI ) {
83+
console.log( 'Unsafe: %d', v );
84+
} else {
85+
console.log( 'Safe: %d', v );
86+
}
87+
}
88+
```
89+
90+
</section>
91+
92+
<!-- /.examples -->
93+
94+
<!-- C interface documentation. -->
95+
96+
* * *
97+
98+
<section class="c">
99+
100+
## C APIs
101+
102+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
103+
104+
<section class="intro">
105+
106+
</section>
107+
108+
<!-- /.intro -->
109+
110+
<!-- C usage documentation. -->
111+
112+
<section class="usage">
113+
114+
### Usage
115+
116+
```c
117+
#include "stdlib/constants/float32/max_safe_fibonacci.h"
118+
```
119+
120+
#### STDLIB_CONSTANT_FLOAT32_MAX_SAFE_FIBONACCI
121+
122+
Macro for the maximum [safe][safe-integers] [Fibonacci number][fibonacci-number] when stored in [single-precision floating-point][ieee754] format.
123+
124+
</section>
125+
126+
<!-- /.usage -->
127+
128+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
129+
130+
<section class="notes">
131+
132+
</section>
133+
134+
<!-- /.notes -->
135+
136+
<!-- C API usage examples. -->
137+
138+
<section class="examples">
139+
140+
</section>
141+
142+
<!-- /.examples -->
143+
144+
</section>
145+
146+
<!-- /.c -->
147+
148+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
149+
150+
<section class="related">
151+
152+
</section>
153+
154+
<!-- /.related -->
155+
156+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
157+
158+
<section class="links">
159+
160+
[safe-integers]: http://www.2ality.com/2013/10/safe-integers.html
161+
162+
[fibonacci-number]: https://en.wikipedia.org/wiki/Fibonacci_number
163+
164+
[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985
165+
166+
<!-- <related-links> -->
167+
168+
<!-- </related-links> -->
169+
170+
</section>
171+
172+
<!-- /.links -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
{{alias}}
3+
Maximum safe Fibonacci number when stored in single-precision floating-point
4+
format.
5+
6+
Examples
7+
--------
8+
> {{alias}}
9+
14930352
10+
11+
See Also
12+
--------
13+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Maximum safe Fibonacci number when stored in single-precision floating-point format.
23+
*
24+
* @example
25+
* var max = FLOAT32_MAX_SAFE_FIBONACCI;
26+
* // returns 14930352
27+
*/
28+
declare const FLOAT32_MAX_SAFE_FIBONACCI: number;
29+
30+
31+
// EXPORTS //
32+
33+
export = FLOAT32_MAX_SAFE_FIBONACCI;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
import FLOAT32_MAX_SAFE_FIBONACCI = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The export is a number...
25+
{
26+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
27+
FLOAT32_MAX_SAFE_FIBONACCI; // $ExpectType number
28+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
var FLOAT32_MAX_SAFE_FIBONACCI = require( './../lib' ); // eslint-disable-line id-length
22+
23+
var v;
24+
var i;
25+
26+
function fibonacci( n ) {
27+
var a;
28+
var b;
29+
var c;
30+
var i;
31+
32+
a = 1;
33+
b = 1;
34+
for ( i = 3; i <= n; i++ ) {
35+
c = a + b;
36+
a = b;
37+
b = c;
38+
}
39+
return b;
40+
}
41+
42+
for ( i = 0; i < 50; i++ ) {
43+
v = fibonacci( i );
44+
if ( v > FLOAT32_MAX_SAFE_FIBONACCI ) {
45+
console.log( 'Unsafe: %d', v );
46+
} else {
47+
console.log( 'Safe: %d', v );
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#ifndef STDLIB_CONSTANTS_FLOAT32_MAX_SAFE_FIBONACCI_H
20+
#define STDLIB_CONSTANTS_FLOAT32_MAX_SAFE_FIBONACCI_H
21+
22+
/**
23+
* Macro for the maximum safe Fibonacci number when stored in single-precision floating-point format.
24+
*/
25+
#define STDLIB_CONSTANT_FLOAT32_MAX_SAFE_FIBONACCI 14930352
26+
27+
#endif // !STDLIB_CONSTANTS_FLOAT32_MAX_SAFE_FIBONACCI_H
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
/**
22+
* Maximum safe Fibonacci number when stored in single-precision floating-point format.
23+
*
24+
* @module @stdlib/constants/float32/max-safe-fibonacci
25+
* @type {integer}
26+
*
27+
* @example
28+
* var FLOAT32_MAX_SAFE_FIBONACCI = require( '@stdlib/constants/float32/max-safe-fibonacci' );
29+
* // returns 14930352
30+
*/
31+
32+
33+
// MAIN //
34+
35+
/**
36+
* The maximum safe Fibonacci number when stored in single-precision floating-point format.
37+
*
38+
* @constant
39+
* @type {integer}
40+
* @default 14930352
41+
* @see [Fibonacci number]{@link https://en.wikipedia.org/wiki/Fibonacci_number}
42+
* @see [IEEE 754]{@link https://en.wikipedia.org/wiki/IEEE_754-1985}
43+
*/
44+
var FLOAT32_MAX_SAFE_FIBONACCI = 14930352; // eslint-disable-line id-length
45+
46+
47+
// EXPORTS //
48+
49+
module.exports = FLOAT32_MAX_SAFE_FIBONACCI;

0 commit comments

Comments
 (0)