Skip to content

Commit 8552f2c

Browse files
gunjjoshikgryte
andauthored
feat: add constants/float32/max-safe-nth-factorial
PR-URL: #2821 Ref: #649 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 536f5db commit 8552f2c

File tree

10 files changed

+506
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)