Skip to content

Commit

Permalink
feat: add constants/float64/max-safe-nth-factorial
Browse files Browse the repository at this point in the history
PR-URL: stdlib-js#2633 

Reviewed-by: Athan Reines <[email protected]> 
Reviewed-by: Philipp Burckhardt <[email protected]>
  • Loading branch information
gunjjoshi committed Aug 21, 2024
1 parent cc637d1 commit 240e34d
Show file tree
Hide file tree
Showing 10 changed files with 504 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<!--
@license Apache-2.0
Copyright (c) 2024 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# FLOAT64_MAX_SAFE_NTH_FACTORIAL

> Maximum safe nth [factorial][factorial] when stored in [double-precision floating-point][ieee754] format.
<section class="usage">

## Usage

<!-- eslint-disable id-length -->

```javascript
var FLOAT64_MAX_SAFE_NTH_FACTORIAL = require( '@stdlib/constants/float64/max-safe-nth-factorial' );
```

#### FLOAT64_MAX_SAFE_NTH_FACTORIAL

The maximum [safe][safe-integers] nth [factorial][factorial] when stored in [double-precision floating-point][ieee754] format.

<!-- eslint-disable id-length -->

```javascript
var bool = ( FLOAT64_MAX_SAFE_NTH_FACTORIAL === 170 );
// returns true
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint-disable id-length -->

<!-- eslint no-undef: "error" -->

```javascript
var FLOAT64_MAX_SAFE_NTH_FACTORIAL = require( '@stdlib/constants/float64/max-safe-nth-factorial' );

function factorial( n ) {
var a;
var i;

a = 1;
for ( i = 2; i <= n; i++ ) {
a *= i;
}
return a;
}

for ( i = 0; i < 200; i++ ) {
v = factorial( i );
if ( i > FLOAT64_MAX_SAFE_NTH_FACTORIAL ) {
console.log( 'Unsafe: %d', v );
} else {
console.log( 'Safe: %d', v );
}
}
```

</section>

<!-- /.examples -->

<!-- C interface documentation. -->

* * *

<section class="c">

## C APIs

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- C usage documentation. -->

<section class="usage">

### Usage

```c
#include "stdlib/constants/float64/max_safe_nth_factorial.h"
```

#### STDLIB_CONSTANT_FLOAT64_MAX_SAFE_NTH_FACTORIAL

Macro for the maximum [safe][safe-integers] nth [factorial][factorial] when stored in [double-precision floating-point][ieee754] format.

</section>

<!-- /.usage -->

<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- C API usage examples. -->

<section class="examples">

</section>

<!-- /.examples -->

</section>

<!-- /.c -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[safe-integers]: http://www.2ality.com/2013/10/safe-integers.html

[factorial]: https://en.wikipedia.org/wiki/Factorial

[ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

{{alias}}
Maximum safe nth factorial when stored in double-precision floating-
point format.

Examples
--------
> {{alias}}
170

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/**
* Maximum safe nth factorial when stored in double-precision floating-point format.
*
* @example
* var max = FLOAT64_MAX_SAFE_NTH_FACTORIAL;
* // returns 170
*/
declare const FLOAT64_MAX_SAFE_NTH_FACTORIAL: number;


// EXPORTS //

export = FLOAT64_MAX_SAFE_NTH_FACTORIAL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import FLOAT64_MAX_SAFE_NTH_FACTORIAL = require( './index' );


// TESTS //

// The export is a number...
{
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
FLOAT64_MAX_SAFE_NTH_FACTORIAL; // $ExpectType number
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var FLOAT64_MAX_SAFE_NTH_FACTORIAL = require( './../lib' );

var v;
var i;

function factorial( n ) {
var a;
var i;

a = 1;
for ( i = 2; i <= n; i++ ) {
a *= i;
}
return a;
}

for ( i = 0; i < 200; i++ ) {
v = factorial( i );
if ( i > FLOAT64_MAX_SAFE_NTH_FACTORIAL ) {
console.log( 'Unsafe: %d', v );
} else {
console.log( 'Safe: %d', v );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef STDLIB_CONSTANTS_FLOAT64_MAX_SAFE_NTH_FACTORIAL_H
#define STDLIB_CONSTANTS_FLOAT64_MAX_SAFE_NTH_FACTORIAL_H

/**
* Macro for the maximum safe nth factorial when stored in double-precision floating-point format.
*/
#define STDLIB_CONSTANT_FLOAT64_MAX_SAFE_NTH_FACTORIAL 170

#endif // !STDLIB_CONSTANTS_FLOAT64_MAX_SAFE_NTH_FACTORIAL_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* Maximum safe nth factorial when stored in double-precision floating-point format.
*
* @module @stdlib/constants/float64/max-safe-nth-factorial
* @type {integer}
*
* @example
* var FLOAT64_MAX_SAFE_NTH_FACTORIAL = require( '@stdlib/constants/float64/max-safe-nth-factorial' );
* // returns 170
*/


// MAIN //

/**
* The maximum safe nth factorial when stored in double-precision floating-point format.
*
* @constant
* @type {integer}
* @default 170
* @see [factorial]{@link https://en.wikipedia.org/wiki/Factorial}
* @see [IEEE 754]{@link https://en.wikipedia.org/wiki/IEEE_754-1985}
*/
var FLOAT64_MAX_SAFE_NTH_FACTORIAL = 170|0; // asm type annotation


// EXPORTS //

module.exports = FLOAT64_MAX_SAFE_NTH_FACTORIAL;
Loading

0 comments on commit 240e34d

Please sign in to comment.