Skip to content

Commit 240e34d

Browse files
committed
feat: add constants/float64/max-safe-nth-factorial
PR-URL: stdlib-js#2633 Reviewed-by: Athan Reines <[email protected]> Reviewed-by: Philipp Burckhardt <[email protected]>
1 parent cc637d1 commit 240e34d

File tree

10 files changed

+504
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)